From 32876c199812ab59d48d778634b34aeba11a4d5e Mon Sep 17 00:00:00 2001 From: Federico Caselli Date: Sat, 11 Nov 2023 16:26:57 +0100 Subject: [PATCH] Typing updates to fix errors found by mypy 1.7 Change-Id: I02046a72df88a82c8bc6e40b41f9d5b0d01a163e (cherry picked from commit e45b9ffd421cd2a36208a763e20ff6c4e86aad90) --- lib/sqlalchemy/engine/cursor.py | 5 +++-- lib/sqlalchemy/engine/result.py | 4 ++-- lib/sqlalchemy/engine/row.py | 8 ++++---- lib/sqlalchemy/orm/decl_base.py | 4 ++-- lib/sqlalchemy/orm/interfaces.py | 6 +++--- lib/sqlalchemy/util/_py_collections.py | 8 ++++++-- lib/sqlalchemy/util/compat.py | 2 +- 7 files changed, 21 insertions(+), 16 deletions(-) diff --git a/lib/sqlalchemy/engine/cursor.py b/lib/sqlalchemy/engine/cursor.py index 45af49afcc..ff6e311a74 100644 --- a/lib/sqlalchemy/engine/cursor.py +++ b/lib/sqlalchemy/engine/cursor.py @@ -120,7 +120,7 @@ _CursorKeyMapRecType = Tuple[ List[Any], # MD_OBJECTS str, # MD_LOOKUP_KEY str, # MD_RENDERED_NAME - Optional["_ResultProcessorType"], # MD_PROCESSOR + Optional["_ResultProcessorType[Any]"], # MD_PROCESSOR Optional[str], # MD_UNTRANSLATED ] @@ -134,7 +134,7 @@ _NonAmbigCursorKeyMapRecType = Tuple[ List[Any], str, str, - Optional["_ResultProcessorType"], + Optional["_ResultProcessorType[Any]"], str, ] @@ -1438,6 +1438,7 @@ class CursorResult(Result[_T]): metadata = self._init_metadata(context, cursor_description) + _make_row: Any _make_row = functools.partial( Row, metadata, diff --git a/lib/sqlalchemy/engine/result.py b/lib/sqlalchemy/engine/result.py index 132ae88b66..acbe6f0923 100644 --- a/lib/sqlalchemy/engine/result.py +++ b/lib/sqlalchemy/engine/result.py @@ -64,7 +64,7 @@ _KeyMapRecType = Any _KeyMapType = Mapping[_KeyType, _KeyMapRecType] -_RowData = Union[Row, RowMapping, Any] +_RowData = Union[Row[Any], RowMapping, Any] """A generic form of "row" that accommodates for the different kinds of "rows" that different result objects return, including row, row mapping, and scalar values""" @@ -82,7 +82,7 @@ across all the result types """ -_InterimSupportsScalarsRowType = Union[Row, Any] +_InterimSupportsScalarsRowType = Union[Row[Any], Any] _ProcessorsType = Sequence[Optional["_ResultProcessorType[Any]"]] _TupleGetterType = Callable[[Sequence[Any]], Sequence[Any]] diff --git a/lib/sqlalchemy/engine/row.py b/lib/sqlalchemy/engine/row.py index 9017537ab0..d2bb2e4c9a 100644 --- a/lib/sqlalchemy/engine/row.py +++ b/lib/sqlalchemy/engine/row.py @@ -296,8 +296,8 @@ class ROMappingView(ABC): def __init__( self, mapping: Mapping["_KeyType", Any], items: Sequence[Any] ): - self._mapping = mapping - self._items = items + self._mapping = mapping # type: ignore[misc] + self._items = items # type: ignore[misc] def __len__(self) -> int: return len(self._items) @@ -321,11 +321,11 @@ class ROMappingView(ABC): class ROMappingKeysValuesView( ROMappingView, typing.KeysView["_KeyType"], typing.ValuesView[Any] ): - __slots__ = ("_items",) + __slots__ = ("_items",) # mapping slot is provided by KeysView class ROMappingItemsView(ROMappingView, typing.ItemsView["_KeyType", Any]): - __slots__ = ("_items",) + __slots__ = ("_items",) # mapping slot is provided by ItemsView class RowMapping(BaseRow, typing.Mapping["_KeyType", Any]): diff --git a/lib/sqlalchemy/orm/decl_base.py b/lib/sqlalchemy/orm/decl_base.py index d5ef3db470..0037379bd5 100644 --- a/lib/sqlalchemy/orm/decl_base.py +++ b/lib/sqlalchemy/orm/decl_base.py @@ -1130,9 +1130,9 @@ class _ClassScanMapperConfig(_MapperConfig): defaults = {} for item in field_list: if len(item) == 2: - name, tp = item # type: ignore + name, tp = item elif len(item) == 3: - name, tp, spec = item # type: ignore + name, tp, spec = item defaults[name] = spec else: assert False diff --git a/lib/sqlalchemy/orm/interfaces.py b/lib/sqlalchemy/orm/interfaces.py index a118b2aa85..fed07334fb 100644 --- a/lib/sqlalchemy/orm/interfaces.py +++ b/lib/sqlalchemy/orm/interfaces.py @@ -754,7 +754,7 @@ class PropComparator(SQLORMOperations[_T_co], Generic[_T_co], ColumnOperators): self._adapt_to_entity = adapt_to_entity @util.non_memoized_property - def property(self) -> MapperProperty[_T]: + def property(self) -> MapperProperty[_T_co]: """Return the :class:`.MapperProperty` associated with this :class:`.PropComparator`. @@ -784,7 +784,7 @@ class PropComparator(SQLORMOperations[_T_co], Generic[_T_co], ColumnOperators): def adapt_to_entity( self, adapt_to_entity: AliasedInsp[Any] - ) -> PropComparator[_T]: + ) -> PropComparator[_T_co]: """Return a copy of this PropComparator which will use the given :class:`.AliasedInsp` to produce corresponding expressions. """ @@ -846,7 +846,7 @@ class PropComparator(SQLORMOperations[_T_co], Generic[_T_co], ColumnOperators): ) -> ColumnElement[Any]: ... - def of_type(self, class_: _EntityType[Any]) -> PropComparator[_T]: + def of_type(self, class_: _EntityType[Any]) -> PropComparator[_T_co]: r"""Redefine this object in terms of a polymorphic subclass, :func:`_orm.with_polymorphic` construct, or :func:`_orm.aliased` construct. diff --git a/lib/sqlalchemy/util/_py_collections.py b/lib/sqlalchemy/util/_py_collections.py index 4f52d3bce6..7dba5092bc 100644 --- a/lib/sqlalchemy/util/_py_collections.py +++ b/lib/sqlalchemy/util/_py_collections.py @@ -148,12 +148,16 @@ class immutabledict(ImmutableDictBase[_KT, _VT]): def __or__( # type: ignore[override] self, __value: Mapping[_KT, _VT] ) -> immutabledict[_KT, _VT]: - return immutabledict(super().__or__(__value)) + return immutabledict( + super().__or__(__value), # type: ignore[call-overload] + ) def __ror__( # type: ignore[override] self, __value: Mapping[_KT, _VT] ) -> immutabledict[_KT, _VT]: - return immutabledict(super().__ror__(__value)) + return immutabledict( + super().__ror__(__value), # type: ignore[call-overload] + ) class OrderedSet(Set[_T]): diff --git a/lib/sqlalchemy/util/compat.py b/lib/sqlalchemy/util/compat.py index 98a0b65ec9..5bbb58f4af 100644 --- a/lib/sqlalchemy/util/compat.py +++ b/lib/sqlalchemy/util/compat.py @@ -173,7 +173,7 @@ else: def importlib_metadata_get(group): ep = importlib_metadata.entry_points() - if not typing.TYPE_CHECKING and hasattr(ep, "select"): + if hasattr(ep, "select"): return ep.select(group=group) else: return ep.get(group, ()) -- 2.47.2