From: Maksim Latysh Date: Wed, 14 Dec 2022 14:13:29 +0000 (+0100) Subject: Runtime types collected X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=67f9509ec75a2c12f4c3264be7792751e444e4b3;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Runtime types collected --- diff --git a/lib/sqlalchemy/ext/asyncio/base.py b/lib/sqlalchemy/ext/asyncio/base.py index 13d5e40b24..fce8160131 100644 --- a/lib/sqlalchemy/ext/asyncio/base.py +++ b/lib/sqlalchemy/ext/asyncio/base.py @@ -178,7 +178,7 @@ class GeneratorStartableContext(StartableContext[_T_co]): if not is_ctxmanager: await self.gen.aclose() - return start_value + return start_value # type: ignore[no-any-return] async def __aexit__( self, typ: Any, value: Any, traceback: Any diff --git a/lib/sqlalchemy/orm/dynamic.py b/lib/sqlalchemy/orm/dynamic.py index be31af1e9f..f4b8abf3e3 100644 --- a/lib/sqlalchemy/orm/dynamic.py +++ b/lib/sqlalchemy/orm/dynamic.py @@ -4,7 +4,6 @@ # # This module is part of SQLAlchemy and is released under # the MIT License: https://www.opensource.org/licenses/mit-license.php -# mypy: ignore-errors """Dynamic collection API. @@ -39,17 +38,39 @@ from .writeonly import WriteOnlyLoader from .. import util from ..engine import result + if TYPE_CHECKING: + from typing import Callable + from typing import Optional + from typing import Tuple + from typing import Type + from typing import Union + + from . import QueryableAttribute + from .mapper import Mapper from .session import Session + from .state import InstanceState + from .util import AliasedClass + from .writeonly import WriteOnlyCollection + from ..event import _Dispatch + from ..util.langhelpers import symbol _T = TypeVar("_T", bound=Any) class DynamicCollectionHistory(WriteOnlyHistory): - def __init__(self, attr, state, passive, apply_to=None): + def __init__( + self, + attr: DynamicAttributeImpl, + state: InstanceState[_T], + passive: Union[int, symbol], + apply_to: Optional[DynamicCollectionHistory] = None, + ) -> None: if apply_to: - coll = AppenderQuery(attr, state).autoflush(False) + coll: AppenderQuery[_T] = AppenderQuery(attr, state).autoflush( + False + ) self.unchanged_items = util.OrderedIdentitySet(coll) self.added_items = apply_to.added_items self.deleted_items = apply_to.deleted_items @@ -67,15 +88,17 @@ class DynamicAttributeImpl(WriteOnlyAttributeImpl): def __init__( self, - class_, - key, - typecallable, - dispatch, - target_mapper, - order_by, - query_class=None, - **kw, - ): + class_: Union[Type[_T], AliasedClass[_T]], + key: str, + typecallable: type, + dispatch: _Dispatch[QueryableAttribute[_T]], + target_mapper: Mapper[_T], + order_by: Tuple[()], + query_class: Optional[ + Union[Type[AppenderQuery[_T]], Type[WriteOnlyCollection[_T]]] + ] = None, + **kw: Any, + ) -> None: attributes.AttributeImpl.__init__( self, class_, key, typecallable, dispatch, **kw ) @@ -83,11 +106,13 @@ class DynamicAttributeImpl(WriteOnlyAttributeImpl): if order_by: self.order_by = tuple(order_by) if not query_class: - self.query_class = AppenderQuery + self.query_class = AppenderQuery # type: ignore[assignment] elif AppenderMixin in query_class.mro(): - self.query_class = query_class + self.query_class = query_class # type: ignore[assignment] else: - self.query_class = mixin_user_query(query_class) + self.query_class = mixin_user_query( + query_class + ) # type: ignore[assignment] @relationships.RelationshipProperty.strategy_for(lazy="dynamic") @@ -103,17 +128,22 @@ class AppenderMixin(AbstractCollectionWriter[_T]): """ query_class = None + autoflush: Callable[[_T, bool], AppenderQuery[_T]] - def __init__(self, attr, state): - Query.__init__(self, attr.target_mapper, None) + def __init__( + self, attr: DynamicAttributeImpl, state: InstanceState[_T] + ) -> None: + Query.__init__( + self, attr.target_mapper, None # type: ignore[arg-type] + ) super().__init__(attr, state) @property - def session(self) -> Session: + def session(self) -> Optional[Session]: sess = object_session(self.instance) if ( sess is not None - and self.autoflush + and self.autoflush # type: ignore[truthy-function] and sess.autoflush and self.instance in sess ): @@ -127,7 +157,7 @@ class AppenderMixin(AbstractCollectionWriter[_T]): def session(self, session: Session) -> None: self.sess = session - def _iter(self): + def _iter(self) -> Union[result.ScalarResult[_T], result.Result[_T]]: sess = self.session if sess is None: state = attributes.instance_state(self.instance) @@ -143,7 +173,7 @@ class AppenderMixin(AbstractCollectionWriter[_T]): result.SimpleResultMetaData([self.attr.class_.__name__]), self.attr._get_collection_history( attributes.instance_state(self.instance), - attributes.PASSIVE_NO_INITIALIZE, + attributes.PASSIVE_NO_INITIALIZE, # type: ignore ).added_items, _source_supports_scalars=True, ).scalars() @@ -155,12 +185,12 @@ class AppenderMixin(AbstractCollectionWriter[_T]): def __iter__(self) -> Iterator[_T]: ... - def __getitem__(self, index: Any) -> _T: + def __getitem__(self, index: Any) -> Any: sess = self.session if sess is None: return self.attr._get_collection_history( attributes.instance_state(self.instance), - attributes.PASSIVE_NO_INITIALIZE, + attributes.PASSIVE_NO_INITIALIZE, # type: ignore[attr-defined] ).indexed(index) else: return self._generate(sess).__getitem__(index) @@ -171,13 +201,16 @@ class AppenderMixin(AbstractCollectionWriter[_T]): return len( self.attr._get_collection_history( attributes.instance_state(self.instance), - attributes.PASSIVE_NO_INITIALIZE, + attributes.PASSIVE_NO_INITIALIZE, # type: ignore ).added_items ) else: return self._generate(sess).count() - def _generate(self, sess=None): + def _generate( + self, + sess: Optional[Session] = None, + ) -> Union[Query[_T], Any]: # note we're returning an entirely new Query class instance # here without any assignment capabilities; the class of this # query is determined by the session. @@ -199,7 +232,7 @@ class AppenderMixin(AbstractCollectionWriter[_T]): query._where_criteria = self._where_criteria query._from_obj = self._from_obj - query._order_by_clauses = self._order_by_clauses + query._order_by_clauses = self._order_by_clauses # type: ignore return query @@ -259,7 +292,7 @@ class AppenderMixin(AbstractCollectionWriter[_T]): self._remove_impl(item) -class AppenderQuery(AppenderMixin[_T], Query[_T]): +class AppenderQuery(AppenderMixin[_T], Query[_T]): # type: ignore[misc] """A dynamic query that supports basic collection storage operations. Methods on :class:`.AppenderQuery` include all methods of @@ -270,7 +303,7 @@ class AppenderQuery(AppenderMixin[_T], Query[_T]): """ -def mixin_user_query(cls): +def mixin_user_query(cls: Any) -> type: """Return a new class with AppenderQuery functionality layered over.""" name = "Appender" + cls.__name__ return type(name, (AppenderMixin, cls), {"query_class": cls}) diff --git a/lib/sqlalchemy/orm/writeonly.py b/lib/sqlalchemy/orm/writeonly.py index 5814cef658..b62964d50c 100644 --- a/lib/sqlalchemy/orm/writeonly.py +++ b/lib/sqlalchemy/orm/writeonly.py @@ -50,6 +50,7 @@ from ..sql import update from ..sql.dml import Delete from ..sql.dml import Insert from ..sql.dml import Update +from ..util import symbol from ..util.typing import Literal if TYPE_CHECKING: @@ -340,7 +341,7 @@ class WriteOnlyAttributeImpl( c = self._get_collection_history(state, passive) return [(attributes.instance_state(x), x) for x in c.all_items] - def _get_collection_history(self, state, passive): + def _get_collection_history(self, state: InstanceState[_T], passive: PassiveFlag): if self.key in state.committed_state: c = state.committed_state[self.key] else: @@ -444,7 +445,7 @@ class AbstractCollectionWriter(Generic[_T]): if not TYPE_CHECKING: __slots__ = () - def __init__(self, attr, state): + def __init__(self, attr: WriteOnlyAttributeImpl, state: InstanceState[_T]) -> None: self.instance = instance = state.obj() self.attr = attr diff --git a/lib/sqlalchemy/util/compat.py b/lib/sqlalchemy/util/compat.py index 0f212fad52..3d3ee31d65 100644 --- a/lib/sqlalchemy/util/compat.py +++ b/lib/sqlalchemy/util/compat.py @@ -114,7 +114,7 @@ else: if py310: - anext_ = anext + anext_ = anext # type: ignore[name-defined] else: _NOT_PROVIDED = object() diff --git a/lib/sqlalchemy/util/langhelpers.py b/lib/sqlalchemy/util/langhelpers.py index 8df4950a39..b146905e20 100644 --- a/lib/sqlalchemy/util/langhelpers.py +++ b/lib/sqlalchemy/util/langhelpers.py @@ -65,7 +65,7 @@ _HM = TypeVar("_HM", bound="hybridmethod[Any]") if compat.py310: def get_annotations(obj: Any) -> Mapping[str, Any]: - return inspect.get_annotations(obj) + return inspect.get_annotations(obj) # type: ignore else: diff --git a/lib/sqlalchemy/util/typing.py b/lib/sqlalchemy/util/typing.py index b1ef87db17..873dfa8632 100644 --- a/lib/sqlalchemy/util/typing.py +++ b/lib/sqlalchemy/util/typing.py @@ -61,7 +61,7 @@ Self = TypeVar("Self", bound=Any) if compat.py310: # why they took until py310 to put this in stdlib is beyond me, # I've been wanting it since py27 - from types import NoneType as NoneType + from types import NoneType as NoneType # type: ignore[attr-defined] else: NoneType = type(None) # type: ignore