Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/34-generics' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
todofixthis committed Oct 19, 2024
2 parents 334181f + 4222f35 commit 39c6bcb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 38 deletions.
28 changes: 14 additions & 14 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.. image:: https://github.com/todofixthis/filters/actions/workflows/build.yml/badge.svg
:target: https://github.com/todofixthis/filters/actions/workflows/build.yml
.. image:: https://readthedocs.org/projects/filters/badge/?version=latest
:target: http://filters.readthedocs.io/
:target: https://filters.readthedocs.io/

Filters
=======
Expand Down Expand Up @@ -32,14 +32,14 @@ Validate a latitude position and round to manageable precision:
f.Decimal |
f.Min(Decimal(-90)) |
f.Max(Decimal(90)) |
f.Round(to_nearest='0.000001')
).apply('-12.0431842')
f.Round(to_nearest="0.000001")
).apply("-12.0431842")
Parse an incoming value as a datetime, convert to UTC and strip tzinfo:

.. code-block:: python
f.Datetime(naive=True).apply('2015-04-08T15:11:22-05:00')
f.Datetime(naive=True).apply("2015-04-08T15:11:22-05:00")
Convert every value in an iterable (e.g., list) to unicode and strip
leading/trailing whitespace.
Expand All @@ -49,10 +49,10 @@ normalizes line endings automatically.
.. code-block:: python
f.FilterRepeater(f.Unicode | f.Strip).apply([
b'\xe2\x99\xaa ',
b'\xe2\x94\x8f(\xc2\xb0.\xc2\xb0)\xe2\x94\x9b ',
b'\xe2\x94\x97(\xc2\xb0.\xc2\xb0)\xe2\x94\x93 ',
b'\xe2\x99\xaa ',
b"\xe2\x99\xaa ",
b"\xe2\x94\x8f(\xc2\xb0.\xc2\xb0)\xe2\x94\x9b ",
b"\xe2\x94\x97(\xc2\xb0.\xc2\xb0)\xe2\x94\x93 ",
b"\xe2\x99\xaa ",
])
Parse a JSON string and check that it has correct structure:
Expand All @@ -63,14 +63,14 @@ Parse a JSON string and check that it has correct structure:
f.JsonDecode |
f.FilterMapper(
{
'birthday': f.Date,
'gender': f.CaseFold | f.Choice(choices={'f', 'm', 'n'}),
"birthday": f.Date,
"gender": f.CaseFold | f.Choice(choices={"f", "m", "n"}),
'utcOffset':
"utcOffset":
f.Decimal |
f.Min(Decimal('-15')) |
f.Max(Decimal('+15')) |
f.Round(to_nearest='0.25'),
f.Min(Decimal("-15")) |
f.Max(Decimal("+15")) |
f.Round(to_nearest="0.25"),
},
allow_extra_keys = False,
Expand Down
26 changes: 13 additions & 13 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ Validate a latitude position and round to manageable precision:
f.Decimal |
f.Min(Decimal(-90)) |
f.Max(Decimal(90)) |
f.Round(to_nearest='0.000001')
).apply('-12.0431842')
f.Round(to_nearest="0.000001")
).apply("-12.0431842")
Parse an incoming value as a datetime, convert to UTC and strip tzinfo:

.. code-block:: python
f.Datetime(naive=True).apply('2015-04-08T15:11:22-05:00')
f.Datetime(naive=True).apply("2015-04-08T15:11:22-05:00")
Convert every value in an iterable (e.g., list) to unicode and strip
leading/trailing whitespace.
Expand All @@ -57,10 +57,10 @@ normalizes line endings automatically.
.. code-block:: python
f.FilterRepeater(f.Unicode | f.Strip).apply([
b'\xe2\x99\xaa ',
b'\xe2\x94\x8f(\xc2\xb0.\xc2\xb0)\xe2\x94\x9b ',
b'\xe2\x94\x97(\xc2\xb0.\xc2\xb0)\xe2\x94\x93 ',
b'\xe2\x99\xaa ',
b"\xe2\x99\xaa ",
b"\xe2\x94\x8f(\xc2\xb0.\xc2\xb0)\xe2\x94\x9b ",
b"\xe2\x94\x97(\xc2\xb0.\xc2\xb0)\xe2\x94\x93 ",
b"\xe2\x99\xaa ",
])
Parse a JSON string and check that it has correct structure:
Expand All @@ -71,14 +71,14 @@ Parse a JSON string and check that it has correct structure:
f.JsonDecode |
f.FilterMapper(
{
'birthday': f.Date,
'gender': f.CaseFold | f.Choice(choices={'m', 'f', 'x'}),
"birthday": f.Date,
"gender": f.CaseFold | f.Choice(choices={"m", "f", "n"}),
'utcOffset':
"utcOffset":
f.Decimal |
f.Min(Decimal('-15')) |
f.Max(Decimal('+15')) |
f.Round(to_nearest='0.25'),
f.Min(Decimal("-15")) |
f.Max(Decimal("+15")) |
f.Round(to_nearest="0.25"),
},
allow_extra_keys = False,
Expand Down
12 changes: 9 additions & 3 deletions src/filters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
]

FilterCompatible = typing.Optional[
typing.Union["BaseFilter", "FilterMeta", typing.Callable[..., "BaseFilter"]]
typing.Union[
"BaseFilter",
"FilterMeta",
typing.Callable[[], "BaseFilter"],
]
]
"""
Used in PEP-484 type hints to indicate a value that can be normalized
Expand Down Expand Up @@ -450,6 +454,8 @@ def resolve_filter(

return resolved

return None

@staticmethod
def _make_key(key_parts: typing.Iterable[str]) -> str:
"""
Expand Down Expand Up @@ -540,7 +546,7 @@ def handle_invalid_value(
self,
message: str,
exc_info: bool,
context: typing.MutableMapping,
context: typing.MutableMapping[str, typing.Any],
) -> typing.Any:
"""
Handles an invalid value.
Expand Down Expand Up @@ -596,7 +602,7 @@ def handle_invalid_value(
self,
message: str,
exc_info: bool,
context: typing.MutableMapping,
context: typing.MutableMapping[str, typing.Any],
) -> None:
error = FilterError(message)
error.context = context
Expand Down
2 changes: 1 addition & 1 deletion src/filters/complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def __str__(self):
type=type(self).__name__,
filters=", ".join(
"{key}={filter}".format(key=key, filter=filter_chain)
for key, filter_chain in self._filters.items()
for key, filter_chain in self._filters.items()
),
)

Expand Down
11 changes: 7 additions & 4 deletions src/filters/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ class Call(BaseFilter[T]):
"""

def __init__(
self, callable_: typing.Callable[..., T], *extra_args, **extra_kwargs
self,
callable_: typing.Callable[..., T],
*extra_args: typing.Any,
**extra_kwargs: typing.Any,
) -> None:
"""
:param callable_:
Expand Down Expand Up @@ -555,7 +558,7 @@ def __str__(self):
def _apply(self, value):
if len(value) > self.max_length:
if self.truncate:
return value[0: self.max_length]
return value[0 : self.max_length]

return self._invalid_value(
value=value,
Expand Down Expand Up @@ -773,7 +776,7 @@ def __init__(
self.default = (
staticmethod(default).__wrapped__
if self.call_default is True
or (self.call_default is None and callable(default))
or (self.call_default is None and callable(default))
else default
)

Expand Down Expand Up @@ -807,7 +810,7 @@ def _get_default(self):
return (
self.default()
if self.call_default is True
or (self.call_default is None and callable(self.default))
or (self.call_default is None and callable(self.default))
else self.default
)

Expand Down
6 changes: 3 additions & 3 deletions src/filters/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ class MaxBytes(BaseFilter[bytes]):

templates = {
CODE_TOO_LONG: "Value is too long (must be < {max_bytes} "
"bytes when encoded using {encoding}).",
"bytes when encoded using {encoding}).",
}

def __init__(
Expand Down Expand Up @@ -505,7 +505,7 @@ def truncate_bytes(self, bytes_value: bytes) -> bytes:
# Progressively chop bytes off the end of the string until we
# have something that can be successfully decoded using the
# specified encoding.
truncated = bytes_value[0: target_bytes - trim]
truncated = bytes_value[0 : target_bytes - trim]

try:
truncated.decode(self.encoding)
Expand Down Expand Up @@ -603,7 +603,7 @@ def _apply(self, value):
# Edge case where ``self.max_chars`` isn't big enough to fit
# even ``self.suffix`` by itself.
if target_chars < 0:
return self.suffix[0: self.max_chars]
return self.suffix[0 : self.max_chars]

return (self.prefix + value)[0:target_chars] + self.suffix

Expand Down

0 comments on commit 39c6bcb

Please sign in to comment.