Skip to content

Commit 765acd2

Browse files
committed
Address review
1 parent c111f92 commit 765acd2

1 file changed

Lines changed: 33 additions & 33 deletions

File tree

Lib/typing.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import sys
2929
import types
3030
from types import GenericAlias
31-
lazy import annotationlib as _lazy_annotationlib
31+
lazy import annotationlib
3232

3333
from _typing import (
3434
_idfunc,
@@ -247,7 +247,7 @@ def _type_repr(obj):
247247
if isinstance(obj, tuple):
248248
# Special case for `repr` of types with `ParamSpec`:
249249
return '[' + ', '.join(_type_repr(t) for t in obj) + ']'
250-
return _lazy_annotationlib.type_repr(obj)
250+
return annotationlib.type_repr(obj)
251251

252252

253253
def _collect_type_parameters(
@@ -455,7 +455,7 @@ def _eval_type(t, globalns, localns, type_params, *, recursive_guard=frozenset()
455455
recursive_guard is used to prevent infinite recursion with a recursive
456456
ForwardRef.
457457
"""
458-
if isinstance(t, _lazy_annotationlib.ForwardRef):
458+
if isinstance(t, annotationlib.ForwardRef):
459459
# If the forward_ref has __forward_module__ set, evaluate() infers the globals
460460
# from the module, and it will probably pick better than the globals we have here.
461461
# We do this only for calls from get_type_hints() (which opts in through the
@@ -996,7 +996,7 @@ def _make_forward_ref(code, *, parent_fwdref=None, **kwargs):
996996
kwargs['module'] = parent_fwdref.__forward_module__
997997
if parent_fwdref.__owner__ is not None:
998998
kwargs['owner'] = parent_fwdref.__owner__
999-
forward_ref = _lazy_annotationlib.ForwardRef(code, **kwargs)
999+
forward_ref = annotationlib.ForwardRef(code, **kwargs)
10001000
# For compatibility, eagerly compile the forwardref's code.
10011001
forward_ref.__forward_code__
10021002
return forward_ref
@@ -1031,18 +1031,18 @@ def evaluate_forward_ref(
10311031
VALUE.
10321032
10331033
"""
1034-
if format == _lazy_annotationlib.Format.STRING:
1034+
if format == annotationlib.Format.STRING:
10351035
return forward_ref.__forward_arg__
10361036
if forward_ref.__forward_arg__ in _recursive_guard:
10371037
return forward_ref
10381038

10391039
if format is None:
1040-
format = _lazy_annotationlib.Format.VALUE
1040+
format = annotationlib.Format.VALUE
10411041
value = forward_ref.evaluate(globals=globals, locals=locals,
10421042
type_params=type_params, owner=owner, format=format)
10431043

1044-
if (isinstance(value, _lazy_annotationlib.ForwardRef)
1045-
and format == _lazy_annotationlib.Format.FORWARDREF):
1044+
if (isinstance(value, annotationlib.ForwardRef)
1045+
and format == annotationlib.Format.FORWARDREF):
10461046
return value
10471047

10481048
if isinstance(value, str):
@@ -1883,8 +1883,8 @@ def _get_protocol_attrs(cls):
18831883
annotations = base.__annotations__
18841884
except Exception:
18851885
# Only go through annotationlib to handle deferred annotations if we need to
1886-
annotations = _lazy_annotationlib.get_annotations(
1887-
base, format=_lazy_annotationlib.Format.FORWARDREF
1886+
annotations = annotationlib.get_annotations(
1887+
base, format=annotationlib.Format.FORWARDREF
18881888
)
18891889
for attr in (*base.__dict__, *annotations):
18901890
if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES:
@@ -2132,8 +2132,8 @@ def _proto_hook(cls, other):
21322132
try:
21332133
annos = base.__annotations__
21342134
except Exception:
2135-
annos = _lazy_annotationlib.get_annotations(
2136-
base, format=_lazy_annotationlib.Format.FORWARDREF
2135+
annos = annotationlib.get_annotations(
2136+
base, format=annotationlib.Format.FORWARDREF
21372137
)
21382138
if attr in annos:
21392139
break
@@ -2420,14 +2420,14 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
24202420
"""
24212421
if getattr(obj, '__no_type_check__', None):
24222422
return {}
2423-
Format = _lazy_annotationlib.Format
2423+
Format = annotationlib.Format
24242424
if format is None:
24252425
format = Format.VALUE
24262426
# Classes require a special treatment.
24272427
if isinstance(obj, type):
24282428
hints = {}
24292429
for base in reversed(obj.__mro__):
2430-
ann = _lazy_annotationlib.get_annotations(base, format=format)
2430+
ann = annotationlib.get_annotations(base, format=format)
24312431
if format == Format.STRING:
24322432
hints.update(ann)
24332433
continue
@@ -2460,7 +2460,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
24602460
else:
24612461
return {k: _strip_annotations(t) for k, t in hints.items()}
24622462

2463-
hints = _lazy_annotationlib.get_annotations(obj, format=format)
2463+
hints = annotationlib.get_annotations(obj, format=format)
24642464
if (
24652465
not hints
24662466
and not isinstance(obj, types.ModuleType)
@@ -3012,10 +3012,10 @@ def _make_eager_annotate(types):
30123012
for key, val in types.items()}
30133013
def annotate(format):
30143014
match format:
3015-
case _lazy_annotationlib.Format.VALUE | _lazy_annotationlib.Format.FORWARDREF:
3015+
case annotationlib.Format.VALUE | annotationlib.Format.FORWARDREF:
30163016
return checked_types
3017-
case _lazy_annotationlib.Format.STRING:
3018-
return _lazy_annotationlib.annotations_to_string(types)
3017+
case annotationlib.Format.STRING:
3018+
return annotationlib.annotations_to_string(types)
30193019
case _:
30203020
raise NotImplementedError(format)
30213021
return annotate
@@ -3045,19 +3045,19 @@ def __new__(cls, typename, bases, ns):
30453045
types = ns["__annotations__"]
30463046
field_names = list(types)
30473047
annotate = _make_eager_annotate(types)
3048-
elif (original_annotate := _lazy_annotationlib.get_annotate_from_class_namespace(ns)) is not None:
3049-
types = _lazy_annotationlib.call_annotate_function(
3050-
original_annotate, _lazy_annotationlib.Format.FORWARDREF)
3048+
elif (original_annotate := annotationlib.get_annotate_from_class_namespace(ns)) is not None:
3049+
types = annotationlib.call_annotate_function(
3050+
original_annotate, annotationlib.Format.FORWARDREF)
30513051
field_names = list(types)
30523052

30533053
# For backward compatibility, type-check all the types at creation time
30543054
for typ in types.values():
30553055
_type_check(typ, "field annotation must be a type")
30563056

30573057
def annotate(format):
3058-
annos = _lazy_annotationlib.call_annotate_function(
3058+
annos = annotationlib.call_annotate_function(
30593059
original_annotate, format)
3060-
if format != _lazy_annotationlib.Format.STRING:
3060+
if format != annotationlib.Format.STRING:
30613061
return {key: _type_check(val, f"field {key} annotation must be a type")
30623062
for key, val in annos.items()}
30633063
return annos
@@ -3199,9 +3199,9 @@ def __new__(cls, name, bases, ns, total=True, closed=None,
31993199
if ns_annotations is not None:
32003200
own_annotate = None
32013201
own_annotations = ns_annotations
3202-
elif (own_annotate := _lazy_annotationlib.get_annotate_from_class_namespace(ns)) is not None:
3203-
own_annotations = _lazy_annotationlib.call_annotate_function(
3204-
own_annotate, _lazy_annotationlib.Format.FORWARDREF, owner=tp_dict
3202+
elif (own_annotate := annotationlib.get_annotate_from_class_namespace(ns)) is not None:
3203+
own_annotations = annotationlib.call_annotate_function(
3204+
own_annotate, annotationlib.Format.FORWARDREF, owner=tp_dict
32053205
)
32063206
else:
32073207
own_annotate = None
@@ -3268,20 +3268,20 @@ def __annotate__(format):
32683268
base_annotate = base.__annotate__
32693269
if base_annotate is None:
32703270
continue
3271-
base_annos = _lazy_annotationlib.call_annotate_function(
3271+
base_annos = annotationlib.call_annotate_function(
32723272
base_annotate, format, owner=base)
32733273
annos.update(base_annos)
32743274
if own_annotate is not None:
3275-
own = _lazy_annotationlib.call_annotate_function(
3275+
own = annotationlib.call_annotate_function(
32763276
own_annotate, format, owner=tp_dict)
3277-
if format != _lazy_annotationlib.Format.STRING:
3277+
if format != annotationlib.Format.STRING:
32783278
own = {
32793279
n: _type_check(tp, msg, module=tp_dict.__module__)
32803280
for n, tp in own.items()
32813281
}
3282-
elif format == _lazy_annotationlib.Format.STRING:
3283-
own = _lazy_annotationlib.annotations_to_string(own_annotations)
3284-
elif format in (_lazy_annotationlib.Format.FORWARDREF, _lazy_annotationlib.Format.VALUE):
3282+
elif format == annotationlib.Format.STRING:
3283+
own = annotationlib.annotations_to_string(own_annotations)
3284+
elif format in (annotationlib.Format.FORWARDREF, annotationlib.Format.VALUE):
32853285
own = own_checked_annotations
32863286
else:
32873287
raise NotImplementedError(format)
@@ -3878,7 +3878,7 @@ def __getattr__(attr):
38783878
are only created on-demand here.
38793879
"""
38803880
if attr == "ForwardRef":
3881-
obj = _lazy_annotationlib.ForwardRef
3881+
obj = annotationlib.ForwardRef
38823882
elif attr in {"Pattern", "Match"}:
38833883
import re
38843884
obj = _alias(getattr(re, attr), 1)

0 commit comments

Comments
 (0)