]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Fix typing errors
authorYurii Karabas <1998uriyyo@gmail.com>
Tue, 7 Feb 2023 22:25:24 +0000 (00:25 +0200)
committerYurii Karabas <1998uriyyo@gmail.com>
Tue, 7 Feb 2023 22:25:34 +0000 (00:25 +0200)
lib/sqlalchemy/ext/associationproxy.py
lib/sqlalchemy/orm/properties.py
lib/sqlalchemy/sql/annotation.py
lib/sqlalchemy/sql/base.py
lib/sqlalchemy/sql/schema.py
lib/sqlalchemy/sql/selectable.py

index 49d5edf415c3e5015d4b85f6ea92943b3c0eff43..621712617c4445acfbae52bdc8ee0818244a9797 100644 (file)
@@ -542,6 +542,9 @@ class AssociationProxy(
         )
 
 
+_Self = TypeVar("_Self", bound="AssociationProxyInstance[Any]")
+
+
 class AssociationProxyInstance(SQLORMOperations[_T]):
     """A per-class object that serves class- and object-specific results.
 
@@ -835,7 +838,7 @@ class AssociationProxyInstance(SQLORMOperations[_T]):
         return self.parent.info
 
     @overload
-    def get(self, obj: Literal[None]) -> Self:
+    def get(self: _Self, obj: Literal[None]) -> _Self:
         ...
 
     @overload
index 677f8ac286a80ce644de35eb7cda2b74740238ba..60b1611acaef40462bbee56eddb7c7af50b59e6b 100644 (file)
@@ -588,7 +588,7 @@ class MappedColumn(
         util.set_creation_order(self)
 
     def _copy(self, **kw: Any) -> Self:
-        new = cast(Self, self.__class__.__new__(self.__class__))
+        new = self.__class__.__new__(self.__class__)
         new.column = self.column._copy(**kw)
         new.deferred = self.deferred
         new.foreign_keys = new.column.foreign_keys
index e83d6bdefd8caabd5a1443768afedb989396df4f..046c9bc791c79ae1ac4ea3476101234ab99edaba 100644 (file)
@@ -39,7 +39,6 @@ from .visitors import ExternallyTraversible
 from .visitors import InternalTraversal
 from .. import util
 from ..util.typing import Literal
-from ..util.typing import Self
 
 if TYPE_CHECKING:
     from .base import _EntityNamespace
@@ -50,6 +49,11 @@ _AnnotationDict = Mapping[str, Any]
 EMPTY_ANNOTATIONS: util.immutabledict[str, Any] = util.EMPTY_DICT
 
 
+SelfSupportsAnnotations = TypeVar(
+    "SelfSupportsAnnotations", bound="SupportsAnnotations"
+)
+
+
 class SupportsAnnotations(ExternallyTraversible):
     __slots__ = ()
 
@@ -59,15 +63,17 @@ class SupportsAnnotations(ExternallyTraversible):
 
     _is_immutable: bool
 
-    def _annotate(self, values: _AnnotationDict) -> Self:
+    def _annotate(
+        self: SelfSupportsAnnotations, values: _AnnotationDict
+    ) -> SelfSupportsAnnotations:
         raise NotImplementedError()
 
     @overload
     def _deannotate(
-        self,
+        self: SelfSupportsAnnotations,
         values: Literal[None] = ...,
         clone: bool = ...,
-    ) -> Self:
+    ) -> SelfSupportsAnnotations:
         ...
 
     @overload
@@ -119,7 +125,9 @@ class SupportsCloneAnnotations(SupportsAnnotations):
         ("_annotations", InternalTraversal.dp_annotations_key)
     ]
 
-    def _annotate(self, values: _AnnotationDict) -> Self:
+    def _annotate(
+        self: SelfSupportsAnnotations, values: _AnnotationDict
+    ) -> SelfSupportsAnnotations:
         """return a copy of this ClauseElement with annotations
         updated by the given dictionary.
 
@@ -130,7 +138,9 @@ class SupportsCloneAnnotations(SupportsAnnotations):
         new.__dict__.pop("_generate_cache_key", None)
         return new
 
-    def _with_annotations(self, values: _AnnotationDict) -> Self:
+    def _with_annotations(
+        self: SelfSupportsAnnotations, values: _AnnotationDict
+    ) -> SelfSupportsAnnotations:
         """return a copy of this ClauseElement with annotations
         replaced by the given dictionary.
 
@@ -143,10 +153,10 @@ class SupportsCloneAnnotations(SupportsAnnotations):
 
     @overload
     def _deannotate(
-        self,
+        self: SelfSupportsAnnotations,
         values: Literal[None] = ...,
         clone: bool = ...,
-    ) -> Self:
+    ) -> SelfSupportsAnnotations:
         ...
 
     @overload
@@ -192,14 +202,18 @@ class SupportsWrappingAnnotations(SupportsAnnotations):
         def entity_namespace(self) -> _EntityNamespace:
             ...
 
-    def _annotate(self, values: _AnnotationDict) -> Self:
+    def _annotate(
+        self: SelfSupportsAnnotations, values: _AnnotationDict
+    ) -> SelfSupportsAnnotations:
         """return a copy of this ClauseElement with annotations
         updated by the given dictionary.
 
         """
         return Annotated._as_annotated_instance(self, values)  # type: ignore
 
-    def _with_annotations(self, values: _AnnotationDict) -> Self:
+    def _with_annotations(
+        self: SelfSupportsAnnotations, values: _AnnotationDict
+    ) -> SelfSupportsAnnotations:
         """return a copy of this ClauseElement with annotations
         replaced by the given dictionary.
 
@@ -208,10 +222,10 @@ class SupportsWrappingAnnotations(SupportsAnnotations):
 
     @overload
     def _deannotate(
-        self,
+        self: SelfSupportsAnnotations,
         values: Literal[None] = ...,
         clone: bool = ...,
-    ) -> Self:
+    ) -> SelfSupportsAnnotations:
         ...
 
     @overload
@@ -242,6 +256,9 @@ class SupportsWrappingAnnotations(SupportsAnnotations):
             return self
 
 
+SelfAnnotated = TypeVar("SelfAnnotated", bound="Annotated")
+
+
 class Annotated(SupportsAnnotations):
     """clones a SupportsAnnotations and applies an 'annotations' dictionary.
 
@@ -278,7 +295,7 @@ class Annotated(SupportsAnnotations):
     __element: SupportsWrappingAnnotations
     _hash: int
 
-    def __new__(cls, *args: Any) -> Self:
+    def __new__(cls: Type[SelfAnnotated], *args: Any) -> SelfAnnotated:
         return object.__new__(cls)
 
     def __init__(
@@ -291,13 +308,15 @@ class Annotated(SupportsAnnotations):
         self._annotations = util.immutabledict(values)
         self._hash = hash(element)
 
-    def _annotate(self, values: _AnnotationDict) -> Self:
+    def _annotate(
+        self: SelfAnnotated, values: _AnnotationDict
+    ) -> SelfAnnotated:
         _values = self._annotations.union(values)
-        new: Self = self._with_annotations(_values)  # type: ignore
+        new: SelfAnnotated = self._with_annotations(_values)  # type: ignore
         return new
 
     def _with_annotations(
-        self, values: _AnnotationDict
+        self: SelfAnnotated, values: _AnnotationDict
     ) -> SupportsAnnotations:
         clone = self.__class__.__new__(self.__class__)
         clone.__dict__ = self.__dict__.copy()
@@ -308,10 +327,10 @@ class Annotated(SupportsAnnotations):
 
     @overload
     def _deannotate(
-        self,
+        self: SelfAnnotated,
         values: Literal[None] = ...,
         clone: bool = ...,
-    ) -> Self:
+    ) -> SelfAnnotated:
         ...
 
     @overload
@@ -351,7 +370,7 @@ class Annotated(SupportsAnnotations):
         def _constructor(self):
             return self.__element._constructor
 
-    def _clone(self, **kw: Any) -> Self:
+    def _clone(self: SelfAnnotated, **kw: Any) -> SelfAnnotated:
         clone = self.__element._clone(**kw)
         if clone is self.__element:
             # detect immutable, don't change anything
index 23ceca7b861c7992ffa8aaefd0420b2faccc7f13..1752a4dc1ab68856a31d924b36ef774d2f48d5b5 100644 (file)
@@ -134,6 +134,10 @@ def _is_has_entity_namespace(element: Any) -> TypeGuard[_HasEntityNamespace]:
     return hasattr(element, "entity_namespace")
 
 
+# Remove when https://github.com/python/mypy/issues/14640 will be fixed
+_Self = TypeVar("_Self", bound=Any)
+
+
 class Immutable:
     """mark a ClauseElement as 'immutable' when expressions are cloned.
 
@@ -157,7 +161,7 @@ class Immutable:
     def params(self, *optionaldict, **kwargs):
         raise NotImplementedError("Immutable objects do not support copying")
 
-    def _clone(self, **kw: Any) -> Self:
+    def _clone(self: _Self, **kw: Any) -> _Self:
         return self
 
     def _copy_internals(
index 474b42828f7662c8e65b055015de67a124645da5..976432721c2719dff5c4a4ce542c63727a128da7 100644 (file)
@@ -3702,7 +3702,7 @@ class FetchedValue(SchemaEventTarget):
     def _copy(self) -> FetchedValue:
         return FetchedValue(self.for_update)
 
-    def _clone(self, for_update: bool) -> Any:
+    def _clone(self, for_update: bool) -> Self:
         n = self.__class__.__new__(self.__class__)
         n.__dict__.update(self.__dict__)
         n.__dict__.pop("column", None)
index f856abcf381f1a559a191aa9161a52cf23641071..21b83d556e1b6ab68e2ceadd09be800e233eff96 100644 (file)
@@ -1612,7 +1612,7 @@ class AliasedReturnsRows(NoInit, NamedFromClause):
 
     @classmethod
     def _construct(
-        cls: Type,
+        cls,
         selectable: Any,
         *,
         name: Optional[str] = None,