Skip to content

Commit

Permalink
Fixed a couple of mypy errors 🥧
Browse files Browse the repository at this point in the history
  • Loading branch information
todofixthis committed Oct 20, 2024
1 parent df0777e commit e931f63
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/filters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,23 @@ def __copy__(cls, the_filter: "BaseFilter[T]") -> "BaseFilter[T]":

return new_filter

def __or__(self, next_filter: "BaseFilter[U] | None") -> "FilterChain[T]":
def __or__(self, next_filter: "BaseFilter[U] | None") -> "FilterChain[U]":
"""
Chains another filter with this one.
"""
normalized = self.resolve(next_filter)

if normalized:
#
# Officially, we should return ``FilterChain(self) | next_filter``
#
# But that wastes some CPU cycles by creating an extra
# FilterChain instance that gets thrown away almost
# immediately. It's a bit faster just to create a single
# FilterChain instance and modify it in-place.
#
# noinspection PyProtectedMember
return FilterChain(self)._add(next_filter)
else:
return self if isinstance(self, FilterChain) else FilterChain(self)
# Scrub type info from the chain, so we can modify it later.
chain = FilterChain[typing.Any](self)

#
# Officially, we should return ``FilterChain(self) | next_filter``
#
# But that wastes some CPU cycles by creating an extra
# FilterChain instance that gets thrown away almost
# immediately. It's a bit faster just to create a single
# FilterChain instance and modify it in-place.
#
# noinspection PyProtectedMember
return typing.cast(FilterChain[U], chain._add(chain.resolve(next_filter)))

def __str__(self) -> str:
"""
Expand Down Expand Up @@ -189,9 +187,9 @@ def handler(self) -> "BaseInvalidValueHandler":
"""
if self._handler is None:
# Attempt to return the parent filter's handler...
try:
if self.parent:
return self.parent.handler
except AttributeError:
else:
#
# ... unless this filter has no parent, in which case
# it should use the default.
Expand All @@ -211,15 +209,15 @@ def handler(self, handler: "BaseInvalidValueHandler") -> None:
"""
self._handler = handler

def set_handler(self, handler: "BaseInvalidValueHandler") -> "BaseFilter":
def set_handler(self, handler: "BaseInvalidValueHandler") -> "BaseFilter[T]":
"""
Cascading method for setting the filter's invalid value
handler.
"""
self.handler = handler
return self

def apply(self, value: typing.Any) -> typing.Union[T, None]:
def apply(self, value: typing.Any) -> T | None:
"""
Applies the filter to a value.
"""
Expand Down Expand Up @@ -291,7 +289,7 @@ def _invalid_value(
context: typing.MutableMapping[str, typing.Any] | None = None,
sub_key: str | None = None,
template_vars: typing.Mapping[str, str] | None = None,
) -> typing.Any:
) -> T | None:
"""
Handles an invalid value.
Expand Down

0 comments on commit e931f63

Please sign in to comment.