From: Mike Bayer Date: Wed, 11 Oct 2023 15:54:26 +0000 (-0400) Subject: updates for mypy 1.6.0 X-Git-Tag: rel_2_0_22~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a342b3d503f968bbf43f3b2de1f4f623b03a6310;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git updates for mypy 1.6.0 Change-Id: I33b8e2f55b9049ed412dbe1d0ccd5c9e83e4d1d5 --- diff --git a/doc/build/changelog/unreleased_20/mypy160.rst b/doc/build/changelog/unreleased_20/mypy160.rst new file mode 100644 index 0000000000..c69e5b4f36 --- /dev/null +++ b/doc/build/changelog/unreleased_20/mypy160.rst @@ -0,0 +1,4 @@ +.. change:: + :tags: bug, typing + + Updates to the codebase to support Mypy 1.6.0. diff --git a/lib/sqlalchemy/engine/base.py b/lib/sqlalchemy/engine/base.py index 72012ff9d9..0000e28103 100644 --- a/lib/sqlalchemy/engine/base.py +++ b/lib/sqlalchemy/engine/base.py @@ -205,7 +205,11 @@ class Connection(ConnectionEventsTarget, inspection.Inspectable["Inspector"]): @property def _schema_translate_map(self) -> Optional[SchemaTranslateMapType]: - return self._execution_options.get("schema_translate_map", None) + schema_translate_map: Optional[ + SchemaTranslateMapType + ] = self._execution_options.get("schema_translate_map", None) + + return schema_translate_map def schema_for_object(self, obj: HasSchemaAttr) -> Optional[str]: """Return the schema name for the given schema item taking into diff --git a/lib/sqlalchemy/orm/session.py b/lib/sqlalchemy/orm/session.py index 7f77a9bb77..e1d2c4947b 100644 --- a/lib/sqlalchemy/orm/session.py +++ b/lib/sqlalchemy/orm/session.py @@ -511,7 +511,8 @@ class ORMExecuteState(util.MemoizedSlots): """ - return self.bind_arguments.get("mapper", None) + mp: Optional[Mapper[Any]] = self.bind_arguments.get("mapper", None) + return mp @property def all_mappers(self) -> Sequence[Mapper[Any]]: @@ -719,9 +720,14 @@ class ORMExecuteState(util.MemoizedSlots): "This ORM execution is not against a SELECT statement " "so there are no load options." ) - return self.execution_options.get( + + lo: Union[ + context.QueryContext.default_load_options, + Type[context.QueryContext.default_load_options], + ] = self.execution_options.get( "_sa_orm_load_options", context.QueryContext.default_load_options ) + return lo @property def update_delete_options( @@ -738,10 +744,14 @@ class ORMExecuteState(util.MemoizedSlots): "This ORM execution is not against an UPDATE or DELETE " "statement so there are no update options." ) - return self.execution_options.get( + uo: Union[ + bulk_persistence.BulkUDCompileState.default_update_options, + Type[bulk_persistence.BulkUDCompileState.default_update_options], + ] = self.execution_options.get( "_sa_orm_update_options", bulk_persistence.BulkUDCompileState.default_update_options, ) + return uo @property def _non_compile_orm_options(self) -> Sequence[ORMOption]: diff --git a/lib/sqlalchemy/orm/util.py b/lib/sqlalchemy/orm/util.py index 573cc2c436..feb82a648e 100644 --- a/lib/sqlalchemy/orm/util.py +++ b/lib/sqlalchemy/orm/util.py @@ -1630,12 +1630,18 @@ class Bundle( ) @property - def mapper(self) -> Mapper[Any]: - return self.exprs[0]._annotations.get("parentmapper", None) + def mapper(self) -> Optional[Mapper[Any]]: + mp: Optional[Mapper[Any]] = self.exprs[0]._annotations.get( + "parentmapper", None + ) + return mp @property - def entity(self) -> _InternalEntityType[Any]: - return self.exprs[0]._annotations.get("parententity", None) + def entity(self) -> Optional[_InternalEntityType[Any]]: + ie: Optional[_InternalEntityType[Any]] = self.exprs[ + 0 + ]._annotations.get("parententity", None) + return ie @property def entity_namespace( diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index ca389a9a71..c464d7eb0e 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -5526,7 +5526,7 @@ class MetaData(HasSchemaAttr): def _remove_table(self, name: str, schema: Optional[str]) -> None: key = _get_table_key(name, schema) - removed = dict.pop(self.tables, key, None) # type: ignore + removed = dict.pop(self.tables, key, None) if removed is not None: for fk in removed.foreign_keys: fk._remove_from_metadata(self) diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py index c3f3c2b9f8..a0b1977ee5 100644 --- a/lib/sqlalchemy/util/_collections.py +++ b/lib/sqlalchemy/util/_collections.py @@ -532,8 +532,8 @@ class LRUCache(typing.MutableMapping[_KT, _VT]): def get( self, key: _KT, default: Optional[Union[_VT, _T]] = None ) -> Optional[Union[_VT, _T]]: - item = self._data.get(key, default) - if item is not default and item is not None: + item = self._data.get(key) + if item is not None: item[2][0] = self._inc_counter() return item[1] else: diff --git a/tox.ini b/tox.ini index f56ad891c0..ed68dbfcf7 100644 --- a/tox.ini +++ b/tox.ini @@ -174,7 +174,7 @@ commands= deps= greenlet != 0.4.17 importlib_metadata; python_version < '3.8' - mypy >= 1.1.1 + mypy >= 1.6.0 commands = mypy {env:MYPY_COLOR} ./lib/sqlalchemy # pyright changes too often with not-exactly-correct errors