from .visitors import InternalTraversal
from .. import util
from ..util.typing import Literal
-from ..util.typing import Self
if TYPE_CHECKING:
from .base import _EntityNamespace
EMPTY_ANNOTATIONS: util.immutabledict[str, Any] = util.EMPTY_DICT
+SelfSupportsAnnotations = TypeVar(
+ "SelfSupportsAnnotations", bound="SupportsAnnotations"
+)
+
+
class SupportsAnnotations(ExternallyTraversible):
__slots__ = ()
_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
("_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.
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.
@overload
def _deannotate(
- self,
+ self: SelfSupportsAnnotations,
values: Literal[None] = ...,
clone: bool = ...,
- ) -> Self:
+ ) -> SelfSupportsAnnotations:
...
@overload
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.
@overload
def _deannotate(
- self,
+ self: SelfSupportsAnnotations,
values: Literal[None] = ...,
clone: bool = ...,
- ) -> Self:
+ ) -> SelfSupportsAnnotations:
...
@overload
return self
+SelfAnnotated = TypeVar("SelfAnnotated", bound="Annotated")
+
+
class Annotated(SupportsAnnotations):
"""clones a SupportsAnnotations and applies an 'annotations' dictionary.
__element: SupportsWrappingAnnotations
_hash: int
- def __new__(cls, *args: Any) -> Self:
+ def __new__(cls: Type[SelfAnnotated], *args: Any) -> SelfAnnotated:
return object.__new__(cls)
def __init__(
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()
@overload
def _deannotate(
- self,
+ self: SelfAnnotated,
values: Literal[None] = ...,
clone: bool = ...,
- ) -> Self:
+ ) -> SelfAnnotated:
...
@overload
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