]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Runtime types collected
authorMaksim Latysh <m.latysh@godeltech.com>
Wed, 14 Dec 2022 14:13:29 +0000 (15:13 +0100)
committerMaksim Latysh <m.latysh@godeltech.com>
Mon, 19 Dec 2022 13:06:23 +0000 (14:06 +0100)
lib/sqlalchemy/ext/asyncio/base.py
lib/sqlalchemy/orm/dynamic.py
lib/sqlalchemy/orm/writeonly.py
lib/sqlalchemy/util/compat.py
lib/sqlalchemy/util/langhelpers.py
lib/sqlalchemy/util/typing.py

index 13d5e40b24d3cf8283e4d692890ef867a748b98f..fce81601316af803f178d8c4244f1da659537906 100644 (file)
@@ -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
index be31af1e9fd1b45b36e89f98c40e2b72ca6b1693..f4b8abf3e3ce5f854440198f0c691cf338531394 100644 (file)
@@ -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})
index 5814cef65845b0b9adf14dafd12cae7c8fbcc6b5..b62964d50c2eb80bf2f04e3e8dc10b43d2b2baa6 100644 (file)
@@ -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
index 0f212fad52627ae4716e56d3046739209761f33f..3d3ee31d65c8b3c9329faccb67f3406b08e9f12f 100644 (file)
@@ -114,7 +114,7 @@ else:
 
 
 if py310:
-    anext_ = anext
+    anext_ = anext  # type: ignore[name-defined]
 else:
 
     _NOT_PROVIDED = object()
index 8df4950a39aaa52004c1be82b0fc1bb6fefb60c9..b146905e20ae0c278262df2bc8d9834de980f7ba 100644 (file)
@@ -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:
 
index b1ef87db1719029e305e0e2770d6d0f8533e9004..873dfa863279a2e041e6bc2a77d0a3cacbd1e284 100644 (file)
@@ -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