-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
bpo-40397: Remove __args__ and __parameters__ from _SpecialGenericAlias #19984
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
Merged
serhiy-storchaka
merged 2 commits into
python:master
from
serhiy-storchaka:typing-special-generic-alias-remove-args
May 10, 2020
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
bpo-40397: Remove __args__ and __parameters__ from _SpecialGenericAlias
- Loading branch information
commit 1f9c24935d26b8e879293b95b78f0317507d023d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,14 +186,13 @@ def _collect_type_vars(types): | |
return tuple(tvars) | ||
|
||
|
||
def _check_generic(cls, parameters): | ||
def _check_generic(cls, parameters, elen): | ||
"""Check correct count for parameters of a generic cls (internal helper). | ||
This gives a nice error message in case of count mismatch. | ||
""" | ||
if not cls.__parameters__: | ||
if not elen: | ||
raise TypeError(f"{cls} is not a generic class") | ||
alen = len(parameters) | ||
elen = len(cls.__parameters__) | ||
if alen != elen: | ||
raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};" | ||
f" actual {alen}, expected {elen}") | ||
|
@@ -592,17 +591,6 @@ def __reduce__(self): | |
return self.__name__ | ||
|
||
|
||
# Special typing constructs Union, Optional, Generic, Callable and Tuple | ||
# use three special attributes for internal bookkeeping of generic types: | ||
# * __parameters__ is a tuple of unique free type parameters of a generic | ||
# type, for example, Dict[T, T].__parameters__ == (T,); | ||
# * __origin__ keeps a reference to a type that was subscripted, | ||
# e.g., Union[T, int].__origin__ == Union, or the non-generic version of | ||
# the type. | ||
# * __args__ is a tuple of all arguments used in subscripting, | ||
# e.g., Dict[T, int].__args__ == (T, int). | ||
|
||
|
||
def _is_dunder(attr): | ||
return attr.startswith('__') and attr.endswith('__') | ||
|
||
|
@@ -615,28 +603,11 @@ class _BaseGenericAlias(_Final, _root=True): | |
have 'name' always set. If 'inst' is False, then the alias can't be instantiated, | ||
this is used by e.g. typing.List and typing.Dict. | ||
""" | ||
def __init__(self, origin, params, *, inst=True, name=None): | ||
def __init__(self, origin, *, inst=True, name=None): | ||
self._inst = inst | ||
self._name = name | ||
if not isinstance(params, tuple): | ||
params = (params,) | ||
self.__origin__ = origin | ||
self.__args__ = tuple(... if a is _TypingEllipsis else | ||
() if a is _TypingEmpty else | ||
a for a in params) | ||
self.__parameters__ = _collect_type_vars(params) | ||
self.__slots__ = None # This is not documented. | ||
if not name: | ||
self.__module__ = origin.__module__ | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, _BaseGenericAlias): | ||
return NotImplemented | ||
return (self.__origin__ == other.__origin__ | ||
and self.__args__ == other.__args__) | ||
|
||
def __hash__(self): | ||
return hash((self.__origin__, self.__args__)) | ||
|
||
def __call__(self, *args, **kwargs): | ||
if not self._inst: | ||
|
@@ -669,7 +640,7 @@ def __getattr__(self, attr): | |
raise AttributeError(attr) | ||
|
||
def __setattr__(self, attr, val): | ||
if _is_dunder(attr) or attr in ('_name', '_inst'): | ||
if _is_dunder(attr) or attr in ('_name', '_inst', '_nparams'): | ||
super().__setattr__(attr, val) | ||
else: | ||
setattr(self.__origin__, attr, val) | ||
|
@@ -682,7 +653,38 @@ def __subclasscheck__(self, cls): | |
" class and instance checks") | ||
|
||
|
||
# Special typing constructs Union, Optional, Generic, Callable and Tuple | ||
# use three special attributes for internal bookkeeping of generic types: | ||
# * __parameters__ is a tuple of unique free type parameters of a generic | ||
# type, for example, Dict[T, T].__parameters__ == (T,); | ||
# * __origin__ keeps a reference to a type that was subscripted, | ||
# e.g., Union[T, int].__origin__ == Union, or the non-generic version of | ||
# the type. | ||
# * __args__ is a tuple of all arguments used in subscripting, | ||
# e.g., Dict[T, int].__args__ == (T, int). | ||
|
||
|
||
class _GenericAlias(_BaseGenericAlias, _root=True): | ||
def __init__(self, origin, params, *, inst=True, name=None): | ||
super().__init__(origin, inst=inst, name=name) | ||
if not isinstance(params, tuple): | ||
params = (params,) | ||
self.__args__ = tuple(... if a is _TypingEllipsis else | ||
() if a is _TypingEmpty else | ||
a for a in params) | ||
self.__parameters__ = _collect_type_vars(params) | ||
if not name: | ||
self.__module__ = origin.__module__ | ||
|
||
def __eq__(self, other): | ||
if not isinstance(other, _GenericAlias): | ||
return NotImplemented | ||
return (self.__origin__ == other.__origin__ | ||
and self.__args__ == other.__args__) | ||
|
||
def __hash__(self): | ||
return hash((self.__origin__, self.__args__)) | ||
|
||
@_tp_cache | ||
def __getitem__(self, params): | ||
if self.__origin__ in (Generic, Protocol): | ||
|
@@ -692,14 +694,14 @@ def __getitem__(self, params): | |
params = (params,) | ||
msg = "Parameters to generic types must be types." | ||
params = tuple(_type_check(p, msg) for p in params) | ||
_check_generic(self, params) | ||
_check_generic(self, params, len(self.__parameters__)) | ||
|
||
subst = dict(zip(self.__parameters__, params)) | ||
new_args = [] | ||
for arg in self.__args__: | ||
if isinstance(arg, TypeVar): | ||
arg = subst[arg] | ||
elif isinstance(arg, (_BaseGenericAlias, GenericAlias)): | ||
elif isinstance(arg, (_GenericAlias, GenericAlias)): | ||
subargs = tuple(subst[x] for x in arg.__parameters__) | ||
arg = arg[subargs] | ||
new_args.append(arg) | ||
|
@@ -740,10 +742,11 @@ def __mro_entries__(self, bases): | |
|
||
|
||
class _SpecialGenericAlias(_BaseGenericAlias, _root=True): | ||
def __init__(self, origin, params, *, inst=True, name=None): | ||
def __init__(self, origin, nparams, *, inst=True, name=None): | ||
if name is None: | ||
name = origin.__name__ | ||
super().__init__(origin, params, inst=inst, name=name) | ||
super().__init__(origin, inst=inst, name=name) | ||
self._nparams = nparams | ||
self.__doc__ = f'A generic version of {origin.__module__}.{origin.__qualname__}' | ||
|
||
@_tp_cache | ||
|
@@ -752,8 +755,7 @@ def __getitem__(self, params): | |
params = (params,) | ||
msg = "Parameters to generic types must be types." | ||
params = tuple(_type_check(p, msg) for p in params) | ||
_check_generic(self, params) | ||
assert self.__args__ == self.__parameters__ | ||
_check_generic(self, params, self._nparams) | ||
return self.copy_with(params) | ||
|
||
def copy_with(self, params): | ||
|
@@ -912,7 +914,7 @@ def __class_getitem__(cls, params): | |
f"Parameters to {cls.__name__}[...] must all be unique") | ||
else: | ||
# Subscripting a regular Generic subclass. | ||
_check_generic(cls, params) | ||
_check_generic(cls, params, len(cls.__parameters__)) | ||
return _GenericAlias(cls, params) | ||
|
||
def __init_subclass__(cls, *args, **kwargs): | ||
|
@@ -1571,18 +1573,18 @@ class Other(Leaf): # Error reported by type checker | |
# Various ABCs mimicking those in collections.abc. | ||
_alias = _SpecialGenericAlias | ||
|
||
Hashable = _alias(collections.abc.Hashable, ()) # Not generic. | ||
Awaitable = _alias(collections.abc.Awaitable, T_co) | ||
Coroutine = _alias(collections.abc.Coroutine, (T_co, T_contra, V_co)) | ||
AsyncIterable = _alias(collections.abc.AsyncIterable, T_co) | ||
AsyncIterator = _alias(collections.abc.AsyncIterator, T_co) | ||
Iterable = _alias(collections.abc.Iterable, T_co) | ||
Iterator = _alias(collections.abc.Iterator, T_co) | ||
Reversible = _alias(collections.abc.Reversible, T_co) | ||
Sized = _alias(collections.abc.Sized, ()) # Not generic. | ||
Container = _alias(collections.abc.Container, T_co) | ||
Collection = _alias(collections.abc.Collection, T_co) | ||
Callable = _CallableType(collections.abc.Callable, ()) | ||
Hashable = _alias(collections.abc.Hashable, 0) # Not generic. | ||
Awaitable = _alias(collections.abc.Awaitable, 1) | ||
Coroutine = _alias(collections.abc.Coroutine, 3) | ||
AsyncIterable = _alias(collections.abc.AsyncIterable, 1) | ||
AsyncIterator = _alias(collections.abc.AsyncIterator, 1) | ||
Iterable = _alias(collections.abc.Iterable, 1) | ||
Iterator = _alias(collections.abc.Iterator, 1) | ||
Reversible = _alias(collections.abc.Reversible, 1) | ||
Sized = _alias(collections.abc.Sized, 0) # Not generic. | ||
Container = _alias(collections.abc.Container, 1) | ||
Collection = _alias(collections.abc.Collection, 1) | ||
Callable = _CallableType(collections.abc.Callable, -1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment about meaning of |
||
Callable.__doc__ = \ | ||
"""Callable type; Callable[[int], str] is a function of (int) -> str. | ||
|
||
|
@@ -1593,15 +1595,15 @@ class Other(Leaf): # Error reported by type checker | |
There is no syntax to indicate optional or keyword arguments, | ||
such function types are rarely used as callback types. | ||
""" | ||
AbstractSet = _alias(collections.abc.Set, T_co, name='AbstractSet') | ||
MutableSet = _alias(collections.abc.MutableSet, T) | ||
AbstractSet = _alias(collections.abc.Set, 1, name='AbstractSet') | ||
MutableSet = _alias(collections.abc.MutableSet, 1) | ||
# NOTE: Mapping is only covariant in the value type. | ||
Mapping = _alias(collections.abc.Mapping, (KT, VT_co)) | ||
MutableMapping = _alias(collections.abc.MutableMapping, (KT, VT)) | ||
Sequence = _alias(collections.abc.Sequence, T_co) | ||
MutableSequence = _alias(collections.abc.MutableSequence, T) | ||
ByteString = _alias(collections.abc.ByteString, ()) # Not generic | ||
Tuple = _TupleType(tuple, (), inst=False, name='Tuple') | ||
Mapping = _alias(collections.abc.Mapping, 2) | ||
MutableMapping = _alias(collections.abc.MutableMapping, 2) | ||
Sequence = _alias(collections.abc.Sequence, 1) | ||
MutableSequence = _alias(collections.abc.MutableSequence, 1) | ||
ByteString = _alias(collections.abc.ByteString, 0) # Not generic | ||
Tuple = _TupleType(tuple, -1, inst=False, name='Tuple') | ||
Tuple.__doc__ = \ | ||
"""Tuple type; Tuple[X, Y] is the cross-product type of X and Y. | ||
|
||
|
@@ -1611,24 +1613,24 @@ class Other(Leaf): # Error reported by type checker | |
|
||
To specify a variable-length tuple of homogeneous type, use Tuple[T, ...]. | ||
""" | ||
List = _alias(list, T, inst=False, name='List') | ||
Deque = _alias(collections.deque, T, name='Deque') | ||
Set = _alias(set, T, inst=False, name='Set') | ||
FrozenSet = _alias(frozenset, T_co, inst=False, name='FrozenSet') | ||
MappingView = _alias(collections.abc.MappingView, T_co) | ||
KeysView = _alias(collections.abc.KeysView, KT) | ||
ItemsView = _alias(collections.abc.ItemsView, (KT, VT_co)) | ||
ValuesView = _alias(collections.abc.ValuesView, VT_co) | ||
ContextManager = _alias(contextlib.AbstractContextManager, T_co, name='ContextManager') | ||
AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, T_co, name='AsyncContextManager') | ||
Dict = _alias(dict, (KT, VT), inst=False, name='Dict') | ||
DefaultDict = _alias(collections.defaultdict, (KT, VT), name='DefaultDict') | ||
OrderedDict = _alias(collections.OrderedDict, (KT, VT)) | ||
Counter = _alias(collections.Counter, T) | ||
ChainMap = _alias(collections.ChainMap, (KT, VT)) | ||
Generator = _alias(collections.abc.Generator, (T_co, T_contra, V_co)) | ||
AsyncGenerator = _alias(collections.abc.AsyncGenerator, (T_co, T_contra)) | ||
Type = _alias(type, CT_co, inst=False, name='Type') | ||
List = _alias(list, 1, inst=False, name='List') | ||
Deque = _alias(collections.deque, 1, name='Deque') | ||
Set = _alias(set, 1, inst=False, name='Set') | ||
FrozenSet = _alias(frozenset, 1, inst=False, name='FrozenSet') | ||
MappingView = _alias(collections.abc.MappingView, 1) | ||
KeysView = _alias(collections.abc.KeysView, 1) | ||
ItemsView = _alias(collections.abc.ItemsView, 2) | ||
ValuesView = _alias(collections.abc.ValuesView, 1) | ||
ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager') | ||
AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager') | ||
Dict = _alias(dict, 2, inst=False, name='Dict') | ||
DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict') | ||
OrderedDict = _alias(collections.OrderedDict, 2) | ||
Counter = _alias(collections.Counter, 1) | ||
ChainMap = _alias(collections.ChainMap, 2) | ||
Generator = _alias(collections.abc.Generator, 3) | ||
AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2) | ||
Type = _alias(type, 1, inst=False, name='Type') | ||
Type.__doc__ = \ | ||
"""A special construct usable to annotate class objects. | ||
|
||
|
@@ -2122,8 +2124,8 @@ class io: | |
io.__name__ = __name__ + '.io' | ||
sys.modules[io.__name__] = io | ||
|
||
Pattern = _alias(stdlib_re.Pattern, AnyStr) | ||
Match = _alias(stdlib_re.Match, AnyStr) | ||
Pattern = _alias(stdlib_re.Pattern, 1) | ||
Match = _alias(stdlib_re.Match, 1) | ||
|
||
class re: | ||
"""Wrapper namespace for re type aliases.""" | ||
|
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2020-05-07-21-22-04.bpo-40397.PVWFAn.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Removed attributes ``__args__`` and ``__parameters__`` from special generic | ||
aliases like ``typing.List`` (not subscripted). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a docstring/comment explaining the role of
nparams
/self._nparams
?