Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix an issue wherein _GenericAlias._name was not properly set for s…
…pecialforms
  • Loading branch information
BvB93 committed Aug 5, 2021
commit 6d3d1d3187312a5d160193df92d29082d8fa722c
151 changes: 100 additions & 51 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4793,61 +4793,110 @@ def test_no_isinstance(self):

class SpecialAttrsTests(BaseTestCase):
def test_special_attrs(self):
cls_to_check = (
P = typing.ParamSpec('P')
cls_to_check = {
# ABC classes
typing.AbstractSet,
typing.AsyncContextManager,
typing.AsyncGenerator,
typing.AsyncIterable,
typing.AsyncIterator,
typing.Awaitable,
typing.ByteString,
typing.Callable,
typing.ChainMap,
typing.Collection,
typing.Container,
typing.ContextManager,
typing.Coroutine,
typing.Counter,
typing.DefaultDict,
typing.Deque,
typing.Dict,
typing.FrozenSet,
typing.Generator,
typing.Hashable,
typing.ItemsView,
typing.Iterable,
typing.Iterator,
typing.KeysView,
typing.List,
typing.Mapping,
typing.MappingView,
typing.MutableMapping,
typing.MutableSequence,
typing.MutableSet,
typing.OrderedDict,
typing.Reversible,
typing.Sequence,
typing.Set,
typing.Sized,
typing.Tuple,
typing.Type,
typing.ValuesView,
typing.AbstractSet: 'AbstractSet',
Comment thread
ambv marked this conversation as resolved.
typing.AsyncContextManager: 'AsyncContextManager',
typing.AsyncGenerator: 'AsyncGenerator',
typing.AsyncIterable: 'AsyncIterable',
typing.AsyncIterator: 'AsyncIterator',
typing.Awaitable: 'Awaitable',
typing.ByteString: 'ByteString',
typing.Callable: 'Callable',
typing.ChainMap: 'ChainMap',
typing.Collection: 'Collection',
typing.Container: 'Container',
typing.ContextManager: 'ContextManager',
typing.Coroutine: 'Coroutine',
typing.Counter: 'Counter',
typing.DefaultDict: 'DefaultDict',
typing.Deque: 'Deque',
typing.Dict: 'Dict',
typing.FrozenSet: 'FrozenSet',
typing.Generator: 'Generator',
typing.Hashable: 'Hashable',
typing.ItemsView: 'ItemsView',
typing.Iterable: 'Iterable',
typing.Iterator: 'Iterator',
typing.KeysView: 'KeysView',
typing.List: 'List',
typing.Mapping: 'Mapping',
typing.MappingView: 'MappingView',
typing.MutableMapping: 'MutableMapping',
typing.MutableSequence: 'MutableSequence',
typing.MutableSet: 'MutableSet',
typing.OrderedDict: 'OrderedDict',
typing.Reversible: 'Reversible',
typing.Sequence: 'Sequence',
typing.Set: 'Set',
typing.Sized: 'Sized',
typing.Tuple: 'Tuple',
typing.Type: 'Type',
typing.ValuesView: 'ValuesView',
# Subscribed ABC classes
typing.AbstractSet[Any]: 'AbstractSet',
typing.AsyncContextManager[Any]: 'AsyncContextManager',
typing.AsyncGenerator[Any, Any]: 'AsyncGenerator',
typing.AsyncIterable[Any]: 'AsyncIterable',
typing.AsyncIterator[Any]: 'AsyncIterator',
typing.Awaitable[Any]: 'Awaitable',
typing.Callable[[], Any]: 'Callable',
typing.Callable[..., Any]: 'Callable',
typing.ChainMap[Any, Any]: 'ChainMap',
typing.Collection[Any]: 'Collection',
typing.Container[Any]: 'Container',
typing.ContextManager[Any]: 'ContextManager',
typing.Coroutine[Any, Any, Any]: 'Coroutine',
typing.Counter[Any]: 'Counter',
typing.DefaultDict[Any, Any]: 'DefaultDict',
typing.Deque[Any]: 'Deque',
typing.Dict[Any, Any]: 'Dict',
typing.FrozenSet[Any]: 'FrozenSet',
typing.Generator[Any, Any, Any]: 'Generator',
typing.ItemsView[Any, Any]: 'ItemsView',
typing.Iterable[Any]: 'Iterable',
typing.Iterator[Any]: 'Iterator',
typing.KeysView[Any]: 'KeysView',
typing.List[Any]: 'List',
typing.Mapping[Any, Any]: 'Mapping',
typing.MappingView[Any]: 'MappingView',
typing.MutableMapping[Any, Any]: 'MutableMapping',
typing.MutableSequence[Any]: 'MutableSequence',
typing.MutableSet[Any]: 'MutableSet',
typing.OrderedDict[Any, Any]: 'OrderedDict',
typing.Reversible[Any]: 'Reversible',
typing.Sequence[Any]: 'Sequence',
typing.Set[Any]: 'Set',
typing.Tuple[Any]: 'Tuple',
typing.Tuple[Any, ...]: 'Tuple',
typing.Type[Any]: 'Type',
typing.ValuesView[Any]: 'ValuesView',
# Special Forms
typing.Any,
typing.NoReturn,
typing.ClassVar,
typing.Final,
typing.Union,
typing.Optional,
typing.Literal,
typing.TypeAlias,
typing.Concatenate,
typing.TypeGuard,
)
typing.Any: 'Any',
typing.NoReturn: 'NoReturn',
typing.ClassVar: 'ClassVar',
typing.Final: 'Final',
typing.Union: 'Union',
typing.Optional: 'Optional',
typing.Literal: 'Literal',
typing.TypeAlias: 'TypeAlias',
typing.Concatenate: 'Concatenate',
typing.TypeGuard: 'TypeGuard',
# Subscribed special forms
typing.ClassVar[Any]: 'ClassVar',
typing.Final[Any]: 'Final',
typing.Union[Any]: 'Any',
typing.Union[int, float]: 'Union',
typing.Optional[Any]: 'Optional',
typing.Literal[Any]: 'Literal',
typing.Concatenate[Any, P]: 'Concatenate',
typing.TypeGuard[Any]: 'TypeGuard',
}

for cls in cls_to_check:
for cls, name in cls_to_check.items():
with self.subTest(cls=cls):
self.assertEqual(name, cls._name)
self.assertEqual(cls.__name__, cls._name)
self.assertEqual(cls.__qualname__, cls._name)
self.assertEqual(cls.__module__, 'typing')
Expand Down
23 changes: 17 additions & 6 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class Starship:
be used with isinstance() or issubclass().
"""
item = _type_check(parameters, f'{self} accepts only single type.')
return _GenericAlias(self, (item,))
return _GenericAlias(self, (item,), name="ClassVar")

@_SpecialForm
def Final(self, parameters):
Expand All @@ -482,7 +482,7 @@ class FastConnector(Connection):
There is no runtime checking of these properties.
"""
item = _type_check(parameters, f'{self} accepts only single type.')
return _GenericAlias(self, (item,))
return _GenericAlias(self, (item,), name="Final")

@_SpecialForm
def Union(self, parameters):
Expand Down Expand Up @@ -520,7 +520,12 @@ def Union(self, parameters):
parameters = _remove_dups_flatten(parameters)
if len(parameters) == 1:
return parameters[0]
return _UnionGenericAlias(self, parameters)

if len(parameters) == 2 and type(None) in parameters:
name = "Optional"
else:
name = "Union"
return _UnionGenericAlias(self, parameters, name=name)

@_SpecialForm
def Optional(self, parameters):
Expand Down Expand Up @@ -565,7 +570,7 @@ def open_helper(file: str, mode: MODE) -> str:
except TypeError: # unhashable parameters
pass

return _LiteralGenericAlias(self, parameters)
return _LiteralGenericAlias(self, parameters, name="Literal")


@_SpecialForm
Expand Down Expand Up @@ -604,7 +609,7 @@ def Concatenate(self, parameters):
"ParamSpec variable.")
msg = "Concatenate[arg, ...]: each arg must be a type."
parameters = tuple(_type_check(p, msg) for p in parameters)
return _ConcatenateGenericAlias(self, parameters)
return _ConcatenateGenericAlias(self, parameters, name="Concatenate")


@_SpecialForm
Expand Down Expand Up @@ -652,7 +657,7 @@ def is_str(val: Union[str, float]):
PEP 647 (User-Defined Type Guards).
"""
item = _type_check(parameters, f'{self} accepts only single type.')
return _GenericAlias(self, (item,))
return _GenericAlias(self, (item,), name="TypeGuard")


class ForwardRef(_Final, _root=True):
Expand Down Expand Up @@ -1237,6 +1242,12 @@ def __subclasscheck__(self, cls):
if issubclass(cls, arg):
return True

def __reduce__(self):
func, (origin, args) = super().__reduce__()
if self._name == "Optional":
args = args[1] if args[0] is type(None) else args[0]
return func, (origin, args)
Comment thread
BvB93 marked this conversation as resolved.
Outdated


def _value_and_type_iter(parameters):
return ((p, type(p)) for p in parameters)
Expand Down