FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-44524: Fix an issue wherein `_GenericAlias._name` was not properly set for specialforms by BvB93 · Pull Request #27614 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
Prev Previous commit
Reorder types in test_special_attrs, add more tests
  • Loading branch information
ambv committed Aug 6, 2021
commit 7ec545d5cffcec63b9fa7053096dd11626e074f1
96 changes: 83 additions & 13 deletions Lib/test/test_typing.py
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
Expand Up @@ -4791,9 +4791,13 @@ def test_no_isinstance(self):
issubclass(int, TypeGuard)


SpecialAttrsP = typing.ParamSpec('SpecialAttrsP')
SpecialAttrsT = typing.TypeVar('SpecialAttrsT', int, float, complex)


class SpecialAttrsTests(BaseTestCase):

def test_special_attrs(self):
P = typing.ParamSpec('P')
cls_to_check = {
# ABC classes
typing.AbstractSet: 'AbstractSet',
Comment thread
ambv marked this conversation as resolved.
Expand Down Expand Up @@ -4873,33 +4877,99 @@ def test_special_attrs(self):
typing.Type[Any]: 'Type',
typing.ValuesView[Any]: 'ValuesView',
# Special Forms
typing.Annotated: 'Annotated',
typing.Any: 'Any',
typing.NoReturn: 'NoReturn',
typing.ClassVar: 'ClassVar',
typing.Concatenate: 'Concatenate',
typing.Final: 'Final',
typing.Union: 'Union',
typing.Optional: 'Optional',
typing.ForwardRef: 'ForwardRef',
typing.Literal: 'Literal',
typing.NewType: 'NewType',
typing.NoReturn: 'NoReturn',
typing.Optional: 'Optional',
typing.TypeAlias: 'TypeAlias',
typing.Concatenate: 'Concatenate',
typing.TypeGuard: 'TypeGuard',
typing.TypeVar: 'TypeVar',
typing.Union: 'Union',
# Subscribed special forms
typing.Annotated[Any, "Annotation"]: 'Annotated',
typing.ClassVar[Any]: 'ClassVar',
typing.Concatenate[Any, SpecialAttrsP]: 'Concatenate',
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.Optional[Any]: 'Optional',
typing.TypeGuard[Any]: 'TypeGuard',
typing.Union[Any]: 'Any',
typing.Union[int, float]: 'Union',
# Incompatible special forms (tested in test_special_attrs2)
# - typing.ForwardRef('set[Any]')
# - typing.NewType('TypeName', Any)
# - typing.ParamSpec('SpecialAttrsP')
# - typing.TypeVar('T')
}

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')
self.assertEqual(cls.__name__, name, str(cls))
self.assertEqual(cls.__qualname__, name, str(cls))
self.assertEqual(cls.__module__, 'typing', str(cls))
self.assertEqual(getattr(cls, '_name', name), name, str(cls))
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(cls, proto)
loaded = pickle.loads(s)
self.assertIs(cls, loaded)

TypeName = typing.NewType('SpecialAttrsTests.TypeName', Any)

def test_special_attrs2(self):
# Forward refs provide a different introspection API. __name__ and
# __qualname__ make little sense for forward refs as they can store
# complex typing expressions.
fr = typing.ForwardRef('set[Any]')
self.assertFalse(hasattr(fr, '__name__'))
self.assertFalse(hasattr(fr, '__qualname__'))
self.assertEqual(fr.__module__, 'typing')
# Forward refs are currently unpicklable.
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
with self.assertRaises(TypeError) as exc:
pickle.dumps(fr, proto)

self.assertEqual(SpecialAttrsTests.TypeName.__name__, 'TypeName')
self.assertEqual(
SpecialAttrsTests.TypeName.__qualname__,
'SpecialAttrsTests.TypeName',
)
self.assertEqual(
SpecialAttrsTests.TypeName.__module__,
'test.test_typing',
)
# NewTypes are picklable assuming correct qualname information.
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(SpecialAttrsTests.TypeName, proto)
loaded = pickle.loads(s)
self.assertIs(SpecialAttrsTests.TypeName, loaded)

# Type variables don't support non-global instantiation per PEP 484
# restriction that "The argument to TypeVar() must be a string equal
# to the variable name to which it is assigned". Thus, providing
# __qualname__ is unnecessary.
self.assertEqual(SpecialAttrsT.__name__, 'SpecialAttrsT')
self.assertFalse(hasattr(SpecialAttrsT, '__qualname__'))
self.assertEqual(SpecialAttrsT.__module__, 'test.test_typing')
# Module-level type variables are picklable.
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(SpecialAttrsT, proto)
loaded = pickle.loads(s)
self.assertIs(SpecialAttrsT, loaded)

self.assertEqual(SpecialAttrsP.__name__, 'SpecialAttrsP')
self.assertFalse(hasattr(SpecialAttrsP, '__qualname__'))
self.assertEqual(SpecialAttrsP.__module__, 'test.test_typing')
# Module-level ParamSpecs are picklable.
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
s = pickle.dumps(SpecialAttrsP, proto)
loaded = pickle.loads(s)
self.assertIs(SpecialAttrsP, loaded)

class AllTests(BaseTestCase):
"""Tests for __all__."""
Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
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
Expand Up @@ -1575,7 +1575,7 @@ def __init__(self, origin, metadata):
if isinstance(origin, _AnnotatedAlias):
metadata = origin.__metadata__ + metadata
origin = origin.__origin__
super().__init__(origin, origin)
super().__init__(origin, origin, name="Annotated")
self.__metadata__ = metadata

def copy_with(self, params):
Expand Down

Back | FazBrowse Home | New Git URL