Vlad Serebrennikov
6 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with
10 additions and
2 deletions
-
Lib/test/test_dataclasses.py
-
Lib/test/test_typing.py
-
Lib/typing.py
-
Misc/NEWS.d/next/Library/2020-04-25-20-00-58.bpo-40389.FPA6f0.rst
|
|
|
@ -2028,7 +2028,7 @@ class TestDocString(unittest.TestCase): |
|
|
|
class C: |
|
|
|
x: Union[int, type(None)] = None |
|
|
|
|
|
|
|
self.assertDocStrEqual(C.__doc__, "C(x:Union[int, NoneType]=None)") |
|
|
|
self.assertDocStrEqual(C.__doc__, "C(x:Optional[int]=None)") |
|
|
|
|
|
|
|
def test_docstring_list_field(self): |
|
|
|
@dataclass |
|
|
|
|
|
|
|
@ -1750,7 +1750,7 @@ class GenericTests(BaseTestCase): |
|
|
|
self.assertEqual(repr(Union[Tuple, Tuple[int]]).replace('typing.', ''), |
|
|
|
'Union[Tuple, Tuple[int]]') |
|
|
|
self.assertEqual(repr(Callable[..., Optional[T]][int]).replace('typing.', ''), |
|
|
|
'Callable[..., Union[int, NoneType]]') |
|
|
|
'Callable[..., Optional[int]]') |
|
|
|
self.assertEqual(repr(Callable[[], List[T]][int]).replace('typing.', ''), |
|
|
|
'Callable[[], List[int]]') |
|
|
|
|
|
|
|
|
|
|
|
@ -691,6 +691,13 @@ class _GenericAlias(_Final, _root=True): |
|
|
|
return _GenericAlias(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 != 'Callable' or |
|
|
|
len(self.__args__) == 2 and self.__args__[0] is Ellipsis): |
|
|
|
if self._name: |
|
|
|
|
|
|
|
@ -0,0 +1 @@ |
|
|
|
``repr()`` now returns ``typing.Optional[T]`` when called for ``typing.Union`` of two types, one of which is ``NoneType``. |