bpo-46066: Deprecate kwargs syntax for TypedDict definitions (GH-31126)

Closes python/typing#981

https://bugs.python.org/issue46066
This commit is contained in:
97littleleaf11 2022-02-17 11:26:07 +08:00 committed by GitHub
parent 6f1efd19a7
commit de6043e596
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 35 additions and 10 deletions

View file

@ -4380,7 +4380,8 @@ class TypedDictTests(BaseTestCase):
self.assertEqual(Emp.__total__, True)
def test_basics_keywords_syntax(self):
Emp = TypedDict('Emp', name=str, id=int)
with self.assertWarns(DeprecationWarning):
Emp = TypedDict('Emp', name=str, id=int)
self.assertIsSubclass(Emp, dict)
self.assertIsSubclass(Emp, typing.MutableMapping)
self.assertNotIsSubclass(Emp, collections.abc.Sequence)
@ -4395,7 +4396,8 @@ class TypedDictTests(BaseTestCase):
self.assertEqual(Emp.__total__, True)
def test_typeddict_special_keyword_names(self):
TD = TypedDict("TD", cls=type, self=object, typename=str, _typename=int, fields=list, _fields=dict)
with self.assertWarns(DeprecationWarning):
TD = TypedDict("TD", cls=type, self=object, typename=str, _typename=int, fields=list, _fields=dict)
self.assertEqual(TD.__name__, 'TD')
self.assertEqual(TD.__annotations__, {'cls': type, 'self': object, 'typename': str, '_typename': int, 'fields': list, '_fields': dict})
a = TD(cls=str, self=42, typename='foo', _typename=53, fields=[('bar', tuple)], _fields={'baz', set})
@ -4451,7 +4453,7 @@ class TypedDictTests(BaseTestCase):
def test_pickle(self):
global EmpD # pickle wants to reference the class by name
EmpD = TypedDict('EmpD', name=str, id=int)
EmpD = TypedDict('EmpD', {'name': str, 'id': int})
jane = EmpD({'name': 'jane', 'id': 37})
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
z = pickle.dumps(jane, proto)
@ -4463,7 +4465,7 @@ class TypedDictTests(BaseTestCase):
self.assertEqual(EmpDnew({'name': 'jane', 'id': 37}), jane)
def test_optional(self):
EmpD = TypedDict('EmpD', name=str, id=int)
EmpD = TypedDict('EmpD', {'name': str, 'id': int})
self.assertEqual(typing.Optional[EmpD], typing.Union[None, EmpD])
self.assertNotEqual(typing.List[EmpD], typing.Tuple[EmpD])