Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vendor typing._SpecialForm to fool typing._type_check #966

Merged
merged 4 commits into from Nov 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions typing_extensions/src/test_typing_extensions.py
Expand Up @@ -2199,6 +2199,12 @@ def test_no_isinstance(self):
with self.assertRaises(TypeError):
issubclass(int, Self)

def test_alias(self):
TupleSelf = Tuple[Self, Self]
class Alias:
def return_tuple(self) -> TupleSelf:
return (self, self)

class AllTests(BaseTestCase):

def test_typing_extensions_includes_standard(self):
Expand Down
64 changes: 43 additions & 21 deletions typing_extensions/src/typing_extensions.py
Expand Up @@ -2048,40 +2048,61 @@ def __eq__(self, other):

TypeGuard = _TypeGuard(_root=True)

if not hasattr(typing, "Self") and sys.version_info[:2] >= (3, 7):
# Vendored from cpython typing._SpecialFrom
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit hard to follow the if chains here, but can't this just be together with the appropriate branch for 3.7-3.10 so we don't have to duplicate the conditions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I refactor the uses of typing._SpecialForm to use this vendored verion too?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's change Self only for simplicity.

class _SpecialForm(typing._Final, _root=True):
__slots__ = ('_name', '__doc__', '_getitem')

if hasattr(typing, "Self"):
Self = typing.Self
def __init__(self, getitem):
self._getitem = getitem
self._name = getitem.__name__
self.__doc__ = getitem.__doc__

def __getattr__(self, item):
if item in {'__name__', '__qualname__'}:
return self._name

raise AttributeError(item)

def __mro_entries__(self, bases):
raise TypeError(f"Cannot subclass {self!r}")

elif sys.version_info[:2] >= (3, 9):
class _SelfForm(typing._SpecialForm, _root=True):
def __repr__(self):
return 'typing_extensions.' + self._name
return 'typing.' + self._name
CharString marked this conversation as resolved.
Show resolved Hide resolved

@_SelfForm
def Self(self, params):
"""Used to spell the type of "self" in classes.
def __reduce__(self):
return self._name

Example::
def __call__(self, *args, **kwds):
raise TypeError(f"Cannot instantiate {self!r}")

from typing import Self
def __or__(self, other):
return Union[self, other]

class ReturnsSelf:
def parse(self, data: bytes) -> Self:
...
return self
def __ror__(self, other):
return Union[other, self]

"""
def __instancecheck__(self, obj):
raise TypeError(f"{self} cannot be used with isinstance()")

raise TypeError(f"{self} is not subscriptable")
def __subclasscheck__(self, cls):
raise TypeError(f"{self} cannot be used with issubclass()")

@typing._tp_cache
def __getitem__(self, parameters):
return self._getitem(self, parameters)

if hasattr(typing, "Self"):
Self = typing.Self

elif sys.version_info[:2] >= (3, 7):
class _SelfForm(typing._SpecialForm, _root=True):
class _SelfForm(_SpecialForm, _root=True):
def __repr__(self):
return 'typing_extensions.' + self._name

Self = _SelfForm(
"Self",
doc="""Used to spell the type of "self" in classes.
@_SelfForm
def Self(self, params):
"""Used to spell the type of "self" in classes.

Example::

Expand All @@ -2093,7 +2114,8 @@ def parse(self, data: bytes) -> Self:
return self

"""
)

raise TypeError(f"{self} is not subscriptable")
else:
class _Self(typing._FinalTypingBase, _root=True):
"""Used to spell the type of "self" in classes.
Expand Down