bpo-46642: Explicitly disallow subclassing of instaces of TypeVar, ParamSpec, etc (GH-31148)

The existing test covering this case passed only incidentally. We
explicitly disallow doing this and add a proper error message.

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
Gregory Beauregard 2022-06-25 01:35:33 -05:00 committed by GitHub
parent 605e9c66ad
commit 81e91c95a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 39 deletions

View file

@ -954,6 +954,9 @@ class _BoundVarianceMixin:
prefix = '~'
return prefix + self.__name__
def __mro_entries__(self, bases):
raise TypeError(f"Cannot subclass an instance of {type(self).__name__}")
class TypeVar(_Final, _Immutable, _BoundVarianceMixin, _PickleUsingNameMixin,
_root=True):
@ -1101,6 +1104,9 @@ class TypeVarTuple(_Final, _Immutable, _PickleUsingNameMixin, _root=True):
*args[alen - right:],
)
def __mro_entries__(self, bases):
raise TypeError(f"Cannot subclass an instance of {type(self).__name__}")
class ParamSpecArgs(_Final, _Immutable, _root=True):
"""The args for a ParamSpec object.
@ -1125,6 +1131,9 @@ class ParamSpecArgs(_Final, _Immutable, _root=True):
return NotImplemented
return self.__origin__ == other.__origin__
def __mro_entries__(self, bases):
raise TypeError(f"Cannot subclass an instance of {type(self).__name__}")
class ParamSpecKwargs(_Final, _Immutable, _root=True):
"""The kwargs for a ParamSpec object.
@ -1149,6 +1158,9 @@ class ParamSpecKwargs(_Final, _Immutable, _root=True):
return NotImplemented
return self.__origin__ == other.__origin__
def __mro_entries__(self, bases):
raise TypeError(f"Cannot subclass an instance of {type(self).__name__}")
class ParamSpec(_Final, _Immutable, _BoundVarianceMixin, _PickleUsingNameMixin,
_root=True):