Skip to content

Commit

Permalink
Dropped FilterCompatible (for now) 🔨
Browse files Browse the repository at this point in the history
  • Loading branch information
todofixthis committed Oct 19, 2024
1 parent c2c881b commit df0777e
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 40 deletions.
4 changes: 2 additions & 2 deletions docs/complex_filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ Conditionally invokes a filter based on the output of a function.
* ``getter: Callable[[Any], Hashable]`` - a function that extracts the
comparison value from the incoming value. Whatever this function returns
will be matched against the keys in ``cases``.
* ``cases: Mapping[Hashable, FilterCompatible]`` - a mapping of possible return
* ``cases: Mapping[Hashable, BaseFilter]`` - a mapping of possible return
values from ``getter`` and the corresponding filter chains.
* ``default: Optional[FilterCompatible]`` - Filter chain that will be used if
* ``default: Optional[BaseFilter]`` - Filter chain that will be used if
the return value from ``getter`` doesn't match any keys in ``cases``.

When a ``FilterSwitch`` is applied to an incoming ``value``:
Expand Down
27 changes: 8 additions & 19 deletions src/filters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"BaseInvalidValueHandler",
"ExceptionHandler",
"FilterChain",
"FilterCompatible",
"FilterError",
"NoOp",
"Type",
Expand All @@ -17,16 +16,6 @@
T = typing.TypeVar("T")
U = typing.TypeVar("U")

FilterCompatible = typing.Union[
"BaseFilter[T]",
"typing.Callable[..., BaseFilter[T]]",
None,
]
"""
Used in PEP-484 type hints to indicate a value that can be normalized
into an instance of a :py:class:`filters.base.BaseFilter` subclass.
"""


class BaseFilter(typing.Generic[T]):
"""
Expand Down Expand Up @@ -90,7 +79,7 @@ def __copy__(cls, the_filter: "BaseFilter[T]") -> "BaseFilter[T]":

return new_filter

def __or__(self, next_filter: FilterCompatible) -> "FilterChain[T]":
def __or__(self, next_filter: "BaseFilter[U] | None") -> "FilterChain[T]":
"""
Chains another filter with this one.
"""
Expand Down Expand Up @@ -262,9 +251,9 @@ def _apply_none(self) -> None:
def _filter(
self,
value: typing.Any,
filter_chain: FilterCompatible,
filter_chain: "BaseFilter[U]",
sub_key: str | None = None,
) -> typing.Any:
) -> U:
"""
Applies another filter to a value in the same context as the
current filter.
Expand Down Expand Up @@ -446,10 +435,10 @@ class FilterChain(BaseFilter[T]):
treated as a single filter.
"""

def __init__(self, start_filter: FilterCompatible = None) -> None:
def __init__(self, start_filter: BaseFilter[T] | None = None) -> None:
super().__init__()

self._filters: list[BaseFilter] = []
self._filters: list[BaseFilter[typing.Any]] = []

self._add(start_filter)

Expand All @@ -462,7 +451,7 @@ def __str__(self):
@typing.overload
def __or__(self, next_filter: None) -> "FilterChain[T]": ...

def __or__(self, next_filter: FilterCompatible) -> "FilterChain[U]":
def __or__(self, next_filter: BaseFilter[U]) -> "FilterChain[U]":
"""
Chains a filter with this one.
Expand All @@ -479,7 +468,7 @@ def __or__(self, next_filter: FilterCompatible) -> "FilterChain[U]":
return self

@classmethod
def __copy__(cls, the_filter: "FilterChain") -> "FilterChain":
def __copy__(cls, the_filter: "FilterChain[T]") -> "FilterChain[T]":
"""
Creates a shallow copy of the object.
"""
Expand All @@ -488,7 +477,7 @@ def __copy__(cls, the_filter: "FilterChain") -> "FilterChain":
# noinspection PyTypeChecker
return new_filter

def _add(self, next_filter: FilterCompatible) -> "FilterChain":
def _add(self, next_filter: BaseFilter[U]) -> "FilterChain[U]":
"""
Adds a Filter to the collection directly.
"""
Expand Down
30 changes: 16 additions & 14 deletions src/filters/complex.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import typing

from filters.base import BaseFilter, FilterCompatible, FilterError, Type
from filters.base import BaseFilter, FilterError, Type
from filters.simple import Length
from filters.string import Choice, Unicode

Expand All @@ -11,8 +11,10 @@
"NamedTuple",
]

T = typing.TypeVar("T")

class FilterRepeater(BaseFilter):

class FilterRepeater(BaseFilter[T]):
"""
Applies a filter to every value in an Iterable.
Expand All @@ -34,7 +36,7 @@ class FilterRepeater(BaseFilter):

def __init__(
self,
filter_chain: FilterCompatible,
filter_chain: BaseFilter[T],
restrict_keys: typing.Optional[typing.Iterable] = None,
) -> None:
"""
Expand Down Expand Up @@ -137,8 +139,8 @@ def _apply_item(
self,
key: str,
value: typing.Any,
filter_chain: FilterCompatible,
) -> typing.Any:
filter_chain: BaseFilter[T],
) -> T:
"""
Applies filters to a single value in the iterable.
Expand All @@ -162,7 +164,7 @@ def unicodify_key(key: typing.Any) -> str:
return repr(key)


class FilterMapper(BaseFilter):
class FilterMapper(BaseFilter[dict[str, typing.Any]]):
"""
Given a dict of filters, applies each filter to the corresponding value in
incoming mappings.
Expand All @@ -184,7 +186,7 @@ class FilterMapper(BaseFilter):

def __init__(
self,
filter_map: typing.Mapping[str, FilterCompatible],
filter_map: typing.Mapping[str, BaseFilter[typing.Any]],
allow_missing_keys: typing.Union[bool, typing.Iterable[str]] = True,
allow_extra_keys: typing.Union[bool, typing.Iterable[str]] = True,
) -> None:
Expand Down Expand Up @@ -309,8 +311,8 @@ def _apply_item(
self,
key: str,
value: typing.Any,
filter_chain: FilterCompatible,
) -> typing.Any:
filter_chain: BaseFilter[T],
) -> T:
"""
Applies filters to a single item in the mapping.
Expand Down Expand Up @@ -359,16 +361,16 @@ def unicodify_key(key: typing.Any) -> str:
return repr(key)


class FilterSwitch(BaseFilter):
class FilterSwitch(BaseFilter[T]):
"""
Chooses the next filter to apply based on the output of a callable.
"""

def __init__(
self,
getter: typing.Callable[[typing.Any], typing.Hashable],
cases: typing.Mapping[typing.Hashable, FilterCompatible],
default: typing.Optional[FilterCompatible] = None,
cases: typing.Mapping[typing.Hashable, BaseFilter[T]],
default: typing.Optional[BaseFilter[T]] = None,
) -> None:
"""
:param getter:
Expand Down Expand Up @@ -405,15 +407,15 @@ def _apply(self, value):
return self._filter(value, self.default)


class NamedTuple(BaseFilter):
class NamedTuple(BaseFilter[T]):
"""
Attempts to convert the incoming value into a namedtuple.
"""

def __init__(
self,
type_: typing.Type[typing.NamedTuple],
filter_map: typing.Optional[typing.Mapping[str, FilterCompatible]] = None,
filter_map: typing.Optional[typing.Mapping[str, BaseFilter[T]]] = None,
) -> None:
"""
:param type_:
Expand Down
6 changes: 3 additions & 3 deletions src/filters/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from traceback import format_exc
from types import TracebackType

from filters.base import BaseFilter, BaseInvalidValueHandler, FilterCompatible
from filters.base import BaseFilter, BaseInvalidValueHandler

__all__ = [
"FilterMessage",
Expand Down Expand Up @@ -152,7 +152,7 @@ def handle_exception(
return super().handle_exception(message, exc)


class FilterRunner(object):
class FilterRunner:
"""
Wrapper for a filter that provides an API similar to what you would expect
from a Django form -- at least, when it comes to methods related to data
Expand All @@ -164,7 +164,7 @@ class FilterRunner(object):

def __init__(
self,
starting_filter: FilterCompatible,
starting_filter: BaseFilter[typing.Any],
incoming_data: typing.Any = None,
capture_exc_info: bool = False,
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/filters/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from traceback import format_exception
from unittest import TestCase

from filters.base import FilterCompatible
from filters.base import BaseFilter
from filters.handlers import FilterRunner

__all__ = [
Expand Down Expand Up @@ -40,7 +40,7 @@ class BaseFilterTestCase(TestCase):
``assertFilterErrors`` to check use cases.
"""

filter_type: FilterCompatible = None
filter_type: BaseFilter[typing.Any] | typing.Callable[[], BaseFilter[typing.Any]]

class unmodified(object):
"""
Expand Down

0 comments on commit df0777e

Please sign in to comment.