pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python-validators/validators/commit/7abacca0d12a4147c4e13127cb95cbe21ee8a591

nk crossorigen="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/global-0bd78641c0a1f3e0.css" /> maint: improve type annotations · python-validators/validators@7abacca · GitHub
Skip to content

Commit 7abacca

Browse files
committed
maint: improve type annotations
- prefer type inference over explicit typing for function returns - removes unsettling code in comments from `between.py` - uses `local` instead of `project` to refer local imports
1 parent 04bac38 commit 7abacca

File tree

5 files changed

+17
-21
lines changed

5 files changed

+17
-21
lines changed

tests/test_between.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,17 @@
88
# external
99
import pytest
1010

11-
# project
11+
# local
1212
from validators import between, ValidationFailure
1313

14-
1514
T = TypeVar("T", int, float, str, datetime)
1615

1716

1817
@pytest.mark.parametrize(
1918
("value", "min_val", "max_val"),
2019
[(12, 11, 13), (12, None, 14), (12, 11, None), (12, 12, 12)],
2120
)
22-
def test_returns_true_on_valid_range(value: T, min_val: T, max_val: T) -> None:
21+
def test_returns_true_on_valid_range(value: T, min_val: T, max_val: T):
2322
"""Test returns true on valid range."""
2423
assert between(value, min_val=min_val, max_val=max_val)
2524

@@ -28,7 +27,7 @@ def test_returns_true_on_valid_range(value: T, min_val: T, max_val: T) -> None:
2827
("value", "min_val", "max_val"),
2928
[(12, 13, 12), (12, None, None)],
3029
)
31-
def test_raises_assertion_error_for_invalid_args(value: T, min_val: T, max_val: T) -> None:
30+
def test_raises_assertion_error_for_invalid_args(value: T, min_val: T, max_val: T):
3231
"""Test raises assertion error for invalid args."""
3332
with pytest.raises(AssertionError):
3433
assert between(value, min_val=min_val, max_val=max_val)
@@ -43,7 +42,7 @@ def test_raises_assertion_error_for_invalid_args(value: T, min_val: T, max_val:
4342
(30, 40, "string"),
4443
],
4544
)
46-
def test_raises_type_error_for_invalid_args(value: T, min_val: T, max_val: T) -> None:
45+
def test_raises_type_error_for_invalid_args(value: T, min_val: T, max_val: T):
4746
"""Test raises type error for invalid args."""
4847
with pytest.raises(TypeError):
4948
assert between(value, min_val=min_val, max_val=max_val)
@@ -53,7 +52,7 @@ def test_raises_type_error_for_invalid_args(value: T, min_val: T, max_val: T) ->
5352
("value", "min_val", "max_val"),
5453
[(12, 13, 14), (12, None, 11), (12, 13, None)],
5554
)
56-
def test_returns_failed_validation_on_invalid_range(value: T, min_val: T, max_val: T) -> None:
55+
def test_returns_failed_validation_on_invalid_range(value: T, min_val: T, max_val: T):
5756
"""Test returns failed validation on invalid range."""
5857
result = between(value, min_val=min_val, max_val=max_val)
5958
assert isinstance(result, ValidationFailure)

validators/_extremes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class AbsMax:
2828
.. versionadded:: 0.2
2929
"""
3030

31-
def __ge__(self, other: Any) -> bool:
31+
def __ge__(self, other: Any):
3232
"""GreaterThanOrEqual."""
3333
return other is not AbsMax
3434

@@ -55,6 +55,6 @@ class AbsMin:
5555
.. versionadded:: 0.2
5656
"""
5757

58-
def __le__(self, other: Any) -> bool:
58+
def __le__(self, other: Any):
5959
"""LessThanOrEqual."""
6060
return other is not AbsMin

validators/between.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import TypeVar, Union
66
from datetime import datetime
77

8-
# project
8+
# local
99
from ._extremes import AbsMax, AbsMin
1010
from .utils import validator
1111

@@ -19,7 +19,7 @@ def between(
1919
*,
2020
min_val: Union[T, AbsMin, None] = None,
2121
max_val: Union[T, AbsMax, None] = None,
22-
) -> bool:
22+
):
2323
"""Validate that a number is between minimum and/or maximum value.
2424
2525
This will work with any comparable type, such as floats, decimals and dates
@@ -80,9 +80,6 @@ def between(
8080
if min_val is None:
8181
min_val = AbsMin()
8282

83-
# if isinstance(min_val, AbsMin) and isinstance(max_val, AbsMax):
84-
# return min_val <= value <= max_val
85-
8683
if isinstance(min_val, AbsMin):
8784
if type(value) is not type(max_val):
8885
raise TypeError("`value` and `max_val` must be of same type")

validators/length.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Length."""
22
# -*- coding: utf-8 -*-
33

4-
# project
4+
# local
55
from .between import between
66
from .utils import validator
77

validators/utils.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
# standard
5-
from typing import Any, Callable, Dict, Literal, Union
5+
from typing import Any, Callable, Dict
66
from inspect import getfullargspec
77
from itertools import chain
88

@@ -15,23 +15,23 @@ def __init__(self, function: Callable[..., Any], arg_dict: Dict[str, Any]):
1515
self.func = function
1616
self.__dict__.update(arg_dict)
1717

18-
def __repr__(self) -> str:
18+
def __repr__(self):
1919
"""Repr Validation Failure."""
2020
return (
2121
f"ValidationFailure(func={self.func.__name__}, "
2222
+ f"args={({k: v for (k, v) in self.__dict__.items() if k != 'func'})})"
2323
)
2424

25-
def __str__(self) -> str:
25+
def __str__(self):
2626
"""Str Validation Failure."""
2727
return repr(self)
2828

29-
def __bool__(self) -> Literal[False]:
29+
def __bool__(self):
3030
"""Bool Validation Failure."""
3131
return False
3232

3333

34-
def _func_args_as_dict(func: Callable[..., Any], *args: Any, **kwargs: Any) -> Dict[str, Any]:
34+
def _func_args_as_dict(func: Callable[..., Any], *args: Any, **kwargs: Any):
3535
"""Return function's positional and key value arguments as an ordered dictionary."""
3636
# TODO: find more efficient way to do it
3737
return dict(
@@ -40,7 +40,7 @@ def _func_args_as_dict(func: Callable[..., Any], *args: Any, **kwargs: Any) -> D
4040
)
4141

4242

43-
def validator(func: Callable[..., Any]) -> Callable[..., Union[Literal[True], ValidationFailure]]:
43+
def validator(func: Callable[..., Any]):
4444
"""A decorator that makes given function validator.
4545
4646
Whenever the given function is called and returns ``False`` value
@@ -65,7 +65,7 @@ def validator(func: Callable[..., Any]) -> Callable[..., Union[Literal[True], Va
6565
Wrapper function as a decorator.
6666
"""
6767

68-
def wrapper(*args: Any, **kwargs: Any) -> Union[Literal[True], ValidationFailure]:
68+
def wrapper(*args: Any, **kwargs: Any):
6969
return (
7070
True
7171
if func(*args, **kwargs)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy