gh-119180: PEP 649 compiler changes (#119361)

This commit is contained in:
Jelle Zijlstra 2024-06-11 07:06:49 -06:00 committed by GitHub
parent 02c1dfff07
commit 9b8611eeea
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 610 additions and 329 deletions

View file

@ -2412,7 +2412,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
base_globals = getattr(sys.modules.get(base.__module__, None), '__dict__', {})
else:
base_globals = globalns
ann = base.__dict__.get('__annotations__', {})
ann = getattr(base, '__annotations__', {})
if isinstance(ann, types.GetSetDescriptorType):
ann = {}
base_locals = dict(vars(base)) if localns is None else localns
@ -2970,7 +2970,12 @@ class NamedTupleMeta(type):
raise TypeError(
'can only inherit from a NamedTuple type and Generic')
bases = tuple(tuple if base is _NamedTuple else base for base in bases)
types = ns.get('__annotations__', {})
if "__annotations__" in ns:
types = ns["__annotations__"]
elif "__annotate__" in ns:
types = ns["__annotate__"](1) # VALUE
else:
types = {}
default_names = []
for field_name in types:
if field_name in ns:
@ -3131,7 +3136,12 @@ class _TypedDictMeta(type):
tp_dict.__orig_bases__ = bases
annotations = {}
own_annotations = ns.get('__annotations__', {})
if "__annotations__" in ns:
own_annotations = ns["__annotations__"]
elif "__annotate__" in ns:
own_annotations = ns["__annotate__"](1) # VALUE
else:
own_annotations = {}
msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
own_annotations = {
n: _type_check(tp, msg, module=tp_dict.__module__)
@ -3143,7 +3153,12 @@ class _TypedDictMeta(type):
mutable_keys = set()
for base in bases:
annotations.update(base.__dict__.get('__annotations__', {}))
# TODO: Avoid eagerly evaluating annotations in VALUE format.
# Instead, evaluate in FORWARDREF format to figure out which
# keys have Required/NotRequired/ReadOnly qualifiers, and create
# a new __annotate__ function for the resulting TypedDict that
# combines the annotations from this class and its parents.
annotations.update(base.__annotations__)
base_required = base.__dict__.get('__required_keys__', set())
required_keys |= base_required