Skip to content

bpo-40397: Refactor typing._GenericAlias #19719

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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add _UnionGenericAlias.
  • Loading branch information
serhiy-storchaka committed May 2, 2020
commit 26d44c35bfb570e8847be2ff0aaf66e62045fd8c
39 changes: 23 additions & 16 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def _remove_dups_flatten(parameters):
# Flatten out Union[Union[...], ...].
params = []
for p in parameters:
if isinstance(p, _GenericAlias) and p.__origin__ is Union:
if isinstance(p, _UnionGenericAlias):
params.extend(p.__args__)
elif isinstance(p, tuple) and len(p) > 0 and p[0] is Union:
params.extend(p[1:])
Expand Down Expand Up @@ -444,7 +444,7 @@ def Union(self, parameters):
parameters = _remove_dups_flatten(parameters)
if len(parameters) == 1:
return parameters[0]
return _GenericAlias(self, parameters)
return _UnionGenericAlias(self, parameters)

@_SpecialForm
def Optional(self, parameters):
Expand Down Expand Up @@ -668,15 +668,10 @@ def __getitem__(self, params):
def __eq__(self, other):
if not isinstance(other, _BaseGenericAlias):
return NotImplemented
if self.__origin__ != other.__origin__:
return False
if self.__origin__ is Union and other.__origin__ is Union:
return frozenset(self.__args__) == frozenset(other.__args__)
return self.__args__ == other.__args__
return (self.__origin__ == other.__origin__
and self.__args__ == other.__args__)

def __hash__(self):
if self.__origin__ is Union:
return hash((Union, frozenset(self.__args__)))
return hash((self.__origin__, self.__args__))

def __call__(self, *args, **kwargs):
Expand Down Expand Up @@ -728,13 +723,6 @@ def copy_with(self, params):
return self.__class__(self.__origin__, params, name=self._name, inst=self._inst)

def __repr__(self):
if (self.__origin__ == Union and len(self.__args__) == 2
and type(None) in self.__args__):
if self.__args__[0] is not type(None):
arg = self.__args__[0]
else:
arg = self.__args__[1]
return (f'typing.Optional[{_type_repr(arg)}]')
if self._name:
name = 'typing.' + self._name
else:
Expand Down Expand Up @@ -852,6 +840,25 @@ def __getitem__(self, params):
return self.copy_with(params)


class _UnionGenericAlias(_GenericAlias, _root=True):
def __eq__(self, other):
if not isinstance(other, _UnionGenericAlias):
return NotImplemented
return set(self.__args__) == set(other.__args__)

def __hash__(self):
return hash(frozenset(self.__args__))

def __repr__(self):
args = self.__args__
if len(args) == 2:
if args[0] is type(None):
return f'typing.Optional[{_type_repr(args[1])}]'
elif args[1] is type(None):
return f'typing.Optional[{_type_repr(args[0])}]'
return super().__repr__()


class Generic:
"""Abstract base class for generic types.

Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy