mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-44863: Allow generic typing.TypedDict (#27663)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com> Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
6c7249f265
commit
f6f36cc269
6 changed files with 172 additions and 7 deletions
|
@ -1796,7 +1796,9 @@ class Generic:
|
|||
if '__orig_bases__' in cls.__dict__:
|
||||
error = Generic in cls.__orig_bases__
|
||||
else:
|
||||
error = Generic in cls.__bases__ and cls.__name__ != 'Protocol'
|
||||
error = (Generic in cls.__bases__ and
|
||||
cls.__name__ != 'Protocol' and
|
||||
type(cls) != _TypedDictMeta)
|
||||
if error:
|
||||
raise TypeError("Cannot inherit from plain Generic")
|
||||
if '__orig_bases__' in cls.__dict__:
|
||||
|
@ -2868,14 +2870,19 @@ class _TypedDictMeta(type):
|
|||
Subclasses and instances of TypedDict return actual dictionaries.
|
||||
"""
|
||||
for base in bases:
|
||||
if type(base) is not _TypedDictMeta:
|
||||
if type(base) is not _TypedDictMeta and base is not Generic:
|
||||
raise TypeError('cannot inherit from both a TypedDict type '
|
||||
'and a non-TypedDict base class')
|
||||
tp_dict = type.__new__(_TypedDictMeta, name, (dict,), ns)
|
||||
|
||||
if any(issubclass(b, Generic) for b in bases):
|
||||
generic_base = (Generic,)
|
||||
else:
|
||||
generic_base = ()
|
||||
|
||||
tp_dict = type.__new__(_TypedDictMeta, name, (*generic_base, dict), ns)
|
||||
|
||||
annotations = {}
|
||||
own_annotations = ns.get('__annotations__', {})
|
||||
own_annotation_keys = set(own_annotations.keys())
|
||||
msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
|
||||
own_annotations = {
|
||||
n: _type_check(tp, msg, module=tp_dict.__module__)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue