diff --git a/pytools/tag.py b/pytools/tag.py index dcc170c3b6b7c0038ea7102d51839fc018db9984..d58cda5a7b23c97fe01faaa3f1b4ec0a2187dee9 100644 --- a/pytools/tag.py +++ b/pytools/tag.py @@ -22,6 +22,10 @@ Internal stuff that is only here because the documentation tool wants it .. class:: TagT A type variable with lower bound :class:`Tag`. + +.. class:: _Self_Taggable + + A type variable with lower bound :class:`Taggable`. """ import sys @@ -31,12 +35,6 @@ from typing import (Tuple, Set, Any, FrozenSet, Union, Iterable, # noqa: F401 from pytools import memoize, memoize_method -try: - from typing import Self # type: ignore[attr-defined] -except ImportError: - from typing_extensions import Self - - __copyright__ = """ Copyright (C) 2020 Andreas Klöckner Copyright (C) 2020 Matt Wala @@ -168,6 +166,8 @@ class UniqueTag(Tag): ToTagSetConvertible = Union[Iterable[Tag], Tag, None] TagT = TypeVar("TagT", bound="Tag") +# FIXME: Replace by Self type +_Self_Taggable = TypeVar("_Self_Taggable", bound="Taggable") # {{{ UniqueTag rules checking @@ -265,7 +265,7 @@ class Taggable: """ self.tags = tags - def _with_new_tags(self, tags: FrozenSet[Tag]) -> Self: + def _with_new_tags(self: _Self_Taggable, tags: FrozenSet[Tag]) -> _Self_Taggable: """ Returns a copy of *self* with the specified tags. This method should be overridden by subclasses. @@ -282,7 +282,7 @@ class Taggable: # abstract method. return self.copy(tags=tags) # type: ignore[attr-defined] # pylint: disable=no-member # noqa: E501 - def tagged(self, tags: ToTagSetConvertible) -> Self: + def tagged(self: _Self_Taggable, tags: ToTagSetConvertible) -> _Self_Taggable: """ Return a copy of *self* with the specified tag or tags added to the set of tags. If the resulting set of @@ -295,8 +295,9 @@ class Taggable: return self._with_new_tags( tags=check_tag_uniqueness(normalize_tags(tags) | self.tags)) - def without_tags(self, - tags: ToTagSetConvertible, verify_existence: bool = True) -> Self: + def without_tags(self: _Self_Taggable, + tags: ToTagSetConvertible, verify_existence: bool = True + ) -> _Self_Taggable: """ Return a copy of *self* without the specified tags.