mirror of
https://github.com/python/cpython.git
synced 2025-10-07 15:42:02 +00:00
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:
parent
605e9c66ad
commit
81e91c95a5
3 changed files with 81 additions and 39 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue