Skip to content

Commit

Permalink
Created type alias and fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksim Latysh committed Jan 18, 2023
1 parent 6b5c6d9 commit e0a6cac
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/sqlalchemy/orm/mapped_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
_KT = TypeVar("_KT", bound=Any)
_VT = TypeVar("_VT", bound=Any)

_AnyCallable = TypeVar("_AnyCallable", bound=Callable[[Any], Any])


class _PlainColumnGetter:
"""Plain column getter, stores collection of Column objects
Expand Down Expand Up @@ -70,14 +72,14 @@ def __call__(self, value: _KT) -> Union[_KT, Tuple[_KT, ...]]:
state = base.instance_state(value)
m = base._state_mapper(state)

key = [
key: List[_KT] = [
m._get_state_attr_by_column(state, state.dict, col)
for col in self._cols(m)
]
if self.composite:
return tuple(key)
else:
return key[0] # type: ignore
return key[0]


class _SerializableColumnGetterV2(_PlainColumnGetter):
Expand Down Expand Up @@ -256,7 +258,7 @@ def attribute_keyed_dict(


def keyfunc_mapping(
keyfunc: Callable[[_KT], _KT],
keyfunc: _AnyCallable,
*,
ignore_unpopulated_attribute: bool = False,
) -> Type[KeyFuncDict[_KT, Any]]:
Expand Down Expand Up @@ -332,7 +334,7 @@ class KeyFuncDict(Dict[_KT, _VT]):

def __init__(
self,
keyfunc: Callable[[_KT], _KT],
keyfunc: _AnyCallable,
*,
ignore_unpopulated_attribute: bool = False,
) -> None:
Expand All @@ -354,7 +356,7 @@ def __init__(

@classmethod
def _unreduce(
cls, keyfunc: Callable[[_KT], _KT], values: Dict[_KT, _KT]
cls, keyfunc: _AnyCallable, values: Dict[_KT, _KT]
) -> "KeyFuncDict[_KT, _KT]":
mp: KeyFuncDict[_KT, _KT] = KeyFuncDict(keyfunc)
mp.update(values)
Expand Down Expand Up @@ -440,7 +442,7 @@ def remove(


def _mapped_collection_cls(
keyfunc: Callable[[_KT], _KT], ignore_unpopulated_attribute: bool
keyfunc: _AnyCallable, ignore_unpopulated_attribute: bool
) -> type[KeyFuncDict[_KT, _KT]]:
class _MKeyfuncMapped(KeyFuncDict[_KT, _KT]):
def __init__(self) -> None:
Expand Down

0 comments on commit e0a6cac

Please sign in to comment.