From: Yurii Karabas <1998uriyyo@gmail.com> Date: Tue, 7 Feb 2023 22:25:24 +0000 (+0200) Subject: Fix typing errors X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e830f9273ab997277518f9ebda7b73ce4144af5;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Fix typing errors --- diff --git a/lib/sqlalchemy/ext/associationproxy.py b/lib/sqlalchemy/ext/associationproxy.py index 49d5edf415..621712617c 100644 --- a/lib/sqlalchemy/ext/associationproxy.py +++ b/lib/sqlalchemy/ext/associationproxy.py @@ -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 diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 677f8ac286..60b1611aca 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -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 diff --git a/lib/sqlalchemy/sql/annotation.py b/lib/sqlalchemy/sql/annotation.py index e83d6bdefd..046c9bc791 100644 --- a/lib/sqlalchemy/sql/annotation.py +++ b/lib/sqlalchemy/sql/annotation.py @@ -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 diff --git a/lib/sqlalchemy/sql/base.py b/lib/sqlalchemy/sql/base.py index 23ceca7b86..1752a4dc1a 100644 --- a/lib/sqlalchemy/sql/base.py +++ b/lib/sqlalchemy/sql/base.py @@ -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( diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 474b42828f..976432721c 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -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) diff --git a/lib/sqlalchemy/sql/selectable.py b/lib/sqlalchemy/sql/selectable.py index f856abcf38..21b83d556e 100644 --- a/lib/sqlalchemy/sql/selectable.py +++ b/lib/sqlalchemy/sql/selectable.py @@ -1612,7 +1612,7 @@ class AliasedReturnsRows(NoInit, NamedFromClause): @classmethod def _construct( - cls: Type, + cls, selectable: Any, *, name: Optional[str] = None,