Skip to content

Commit

Permalink
Fix mypy error for field_for_type function. Fixes robinhood#672
Browse files Browse the repository at this point in the history
  • Loading branch information
forsberg committed Oct 20, 2020
1 parent 01b4c0a commit e2f14ea
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 7 additions & 2 deletions faust/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import (
Any,
Callable,
Hashable,
Iterable,
Mapping,
Optional,
Expand Down Expand Up @@ -544,7 +545,10 @@ def prepare_value(self, value: Any, *,

@lru_cache(maxsize=2048)
def field_for_type(
typ: Type) -> Tuple[Type[FieldDescriptorT], Optional[Type[Tag]]]:
htyp: Hashable) -> Tuple[Type[FieldDescriptorT], Optional[Type[Tag]]]:
# This is a way to make mypy >= 0.790 happy, as lru_cache
# expects a Hashable
typ = cast(Type, htyp)
try:
# 1) Check if type is in fast index.
return TYPE_TO_FIELD[typ], None
Expand All @@ -557,7 +561,8 @@ def field_for_type(
else:
try:
if origin is not None and issubclass(origin, Tag):
return field_for_type(typ.__args__[0])[0], typ
return field_for_type(
cast(Hashable, typ.__args__[0]))[0], typ
except TypeError:
pass

Expand Down
5 changes: 4 additions & 1 deletion faust/models/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Callable,
Dict,
FrozenSet,
Hashable,
List,
Mapping,
MutableMapping,
Expand Down Expand Up @@ -253,7 +254,9 @@ def add_related_to_tagged_indices(field: str,
else:
target_type = typ
if descr is None or not isinstance(descr, FieldDescriptorT):
DescriptorType, tag = field_for_type(target_type)
# Make mypy happy
hashed_target_type = cast(Hashable, target_type)
DescriptorType, tag = field_for_type(hashed_target_type)
if tag:
add_to_tagged_indices(field, tag)
descr = DescriptorType(
Expand Down

0 comments on commit e2f14ea

Please sign in to comment.