|
28 | 28 | import sys |
29 | 29 | import types |
30 | 30 | from types import GenericAlias |
31 | | -lazy import annotationlib as _lazy_annotationlib |
| 31 | +lazy import annotationlib |
32 | 32 |
|
33 | 33 | from _typing import ( |
34 | 34 | _idfunc, |
@@ -247,7 +247,7 @@ def _type_repr(obj): |
247 | 247 | if isinstance(obj, tuple): |
248 | 248 | # Special case for `repr` of types with `ParamSpec`: |
249 | 249 | return '[' + ', '.join(_type_repr(t) for t in obj) + ']' |
250 | | - return _lazy_annotationlib.type_repr(obj) |
| 250 | + return annotationlib.type_repr(obj) |
251 | 251 |
|
252 | 252 |
|
253 | 253 | def _collect_type_parameters( |
@@ -455,7 +455,7 @@ def _eval_type(t, globalns, localns, type_params, *, recursive_guard=frozenset() |
455 | 455 | recursive_guard is used to prevent infinite recursion with a recursive |
456 | 456 | ForwardRef. |
457 | 457 | """ |
458 | | - if isinstance(t, _lazy_annotationlib.ForwardRef): |
| 458 | + if isinstance(t, annotationlib.ForwardRef): |
459 | 459 | # If the forward_ref has __forward_module__ set, evaluate() infers the globals |
460 | 460 | # from the module, and it will probably pick better than the globals we have here. |
461 | 461 | # 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): |
996 | 996 | kwargs['module'] = parent_fwdref.__forward_module__ |
997 | 997 | if parent_fwdref.__owner__ is not None: |
998 | 998 | kwargs['owner'] = parent_fwdref.__owner__ |
999 | | - forward_ref = _lazy_annotationlib.ForwardRef(code, **kwargs) |
| 999 | + forward_ref = annotationlib.ForwardRef(code, **kwargs) |
1000 | 1000 | # For compatibility, eagerly compile the forwardref's code. |
1001 | 1001 | forward_ref.__forward_code__ |
1002 | 1002 | return forward_ref |
@@ -1031,18 +1031,18 @@ def evaluate_forward_ref( |
1031 | 1031 | VALUE. |
1032 | 1032 |
|
1033 | 1033 | """ |
1034 | | - if format == _lazy_annotationlib.Format.STRING: |
| 1034 | + if format == annotationlib.Format.STRING: |
1035 | 1035 | return forward_ref.__forward_arg__ |
1036 | 1036 | if forward_ref.__forward_arg__ in _recursive_guard: |
1037 | 1037 | return forward_ref |
1038 | 1038 |
|
1039 | 1039 | if format is None: |
1040 | | - format = _lazy_annotationlib.Format.VALUE |
| 1040 | + format = annotationlib.Format.VALUE |
1041 | 1041 | value = forward_ref.evaluate(globals=globals, locals=locals, |
1042 | 1042 | type_params=type_params, owner=owner, format=format) |
1043 | 1043 |
|
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): |
1046 | 1046 | return value |
1047 | 1047 |
|
1048 | 1048 | if isinstance(value, str): |
@@ -1883,8 +1883,8 @@ def _get_protocol_attrs(cls): |
1883 | 1883 | annotations = base.__annotations__ |
1884 | 1884 | except Exception: |
1885 | 1885 | # 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 |
1888 | 1888 | ) |
1889 | 1889 | for attr in (*base.__dict__, *annotations): |
1890 | 1890 | if not attr.startswith('_abc_') and attr not in EXCLUDED_ATTRIBUTES: |
@@ -2132,8 +2132,8 @@ def _proto_hook(cls, other): |
2132 | 2132 | try: |
2133 | 2133 | annos = base.__annotations__ |
2134 | 2134 | 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 |
2137 | 2137 | ) |
2138 | 2138 | if attr in annos: |
2139 | 2139 | break |
@@ -2420,14 +2420,14 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False, |
2420 | 2420 | """ |
2421 | 2421 | if getattr(obj, '__no_type_check__', None): |
2422 | 2422 | return {} |
2423 | | - Format = _lazy_annotationlib.Format |
| 2423 | + Format = annotationlib.Format |
2424 | 2424 | if format is None: |
2425 | 2425 | format = Format.VALUE |
2426 | 2426 | # Classes require a special treatment. |
2427 | 2427 | if isinstance(obj, type): |
2428 | 2428 | hints = {} |
2429 | 2429 | for base in reversed(obj.__mro__): |
2430 | | - ann = _lazy_annotationlib.get_annotations(base, format=format) |
| 2430 | + ann = annotationlib.get_annotations(base, format=format) |
2431 | 2431 | if format == Format.STRING: |
2432 | 2432 | hints.update(ann) |
2433 | 2433 | continue |
@@ -2460,7 +2460,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False, |
2460 | 2460 | else: |
2461 | 2461 | return {k: _strip_annotations(t) for k, t in hints.items()} |
2462 | 2462 |
|
2463 | | - hints = _lazy_annotationlib.get_annotations(obj, format=format) |
| 2463 | + hints = annotationlib.get_annotations(obj, format=format) |
2464 | 2464 | if ( |
2465 | 2465 | not hints |
2466 | 2466 | and not isinstance(obj, types.ModuleType) |
@@ -3012,10 +3012,10 @@ def _make_eager_annotate(types): |
3012 | 3012 | for key, val in types.items()} |
3013 | 3013 | def annotate(format): |
3014 | 3014 | match format: |
3015 | | - case _lazy_annotationlib.Format.VALUE | _lazy_annotationlib.Format.FORWARDREF: |
| 3015 | + case annotationlib.Format.VALUE | annotationlib.Format.FORWARDREF: |
3016 | 3016 | 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) |
3019 | 3019 | case _: |
3020 | 3020 | raise NotImplementedError(format) |
3021 | 3021 | return annotate |
@@ -3045,19 +3045,19 @@ def __new__(cls, typename, bases, ns): |
3045 | 3045 | types = ns["__annotations__"] |
3046 | 3046 | field_names = list(types) |
3047 | 3047 | 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) |
3051 | 3051 | field_names = list(types) |
3052 | 3052 |
|
3053 | 3053 | # For backward compatibility, type-check all the types at creation time |
3054 | 3054 | for typ in types.values(): |
3055 | 3055 | _type_check(typ, "field annotation must be a type") |
3056 | 3056 |
|
3057 | 3057 | def annotate(format): |
3058 | | - annos = _lazy_annotationlib.call_annotate_function( |
| 3058 | + annos = annotationlib.call_annotate_function( |
3059 | 3059 | original_annotate, format) |
3060 | | - if format != _lazy_annotationlib.Format.STRING: |
| 3060 | + if format != annotationlib.Format.STRING: |
3061 | 3061 | return {key: _type_check(val, f"field {key} annotation must be a type") |
3062 | 3062 | for key, val in annos.items()} |
3063 | 3063 | return annos |
@@ -3199,9 +3199,9 @@ def __new__(cls, name, bases, ns, total=True, closed=None, |
3199 | 3199 | if ns_annotations is not None: |
3200 | 3200 | own_annotate = None |
3201 | 3201 | 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 |
3205 | 3205 | ) |
3206 | 3206 | else: |
3207 | 3207 | own_annotate = None |
@@ -3268,20 +3268,20 @@ def __annotate__(format): |
3268 | 3268 | base_annotate = base.__annotate__ |
3269 | 3269 | if base_annotate is None: |
3270 | 3270 | continue |
3271 | | - base_annos = _lazy_annotationlib.call_annotate_function( |
| 3271 | + base_annos = annotationlib.call_annotate_function( |
3272 | 3272 | base_annotate, format, owner=base) |
3273 | 3273 | annos.update(base_annos) |
3274 | 3274 | if own_annotate is not None: |
3275 | | - own = _lazy_annotationlib.call_annotate_function( |
| 3275 | + own = annotationlib.call_annotate_function( |
3276 | 3276 | own_annotate, format, owner=tp_dict) |
3277 | | - if format != _lazy_annotationlib.Format.STRING: |
| 3277 | + if format != annotationlib.Format.STRING: |
3278 | 3278 | own = { |
3279 | 3279 | n: _type_check(tp, msg, module=tp_dict.__module__) |
3280 | 3280 | for n, tp in own.items() |
3281 | 3281 | } |
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): |
3285 | 3285 | own = own_checked_annotations |
3286 | 3286 | else: |
3287 | 3287 | raise NotImplementedError(format) |
@@ -3878,7 +3878,7 @@ def __getattr__(attr): |
3878 | 3878 | are only created on-demand here. |
3879 | 3879 | """ |
3880 | 3880 | if attr == "ForwardRef": |
3881 | | - obj = _lazy_annotationlib.ForwardRef |
| 3881 | + obj = annotationlib.ForwardRef |
3882 | 3882 | elif attr in {"Pattern", "Match"}: |
3883 | 3883 | import re |
3884 | 3884 | obj = _alias(getattr(re, attr), 1) |
|
0 commit comments