mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
gh-118761: Lazily import annotationlib in typing (#132060)
annotationlib is used quite a few times in typing.py, but I think the usages are just rare enough that this makes sense. The import would get triggered by: - Using get_type_hints(), evaluate_forward_ref(), and similar introspection functions - Using a string annotation anywhere that goes through _type_convert (e.g., "Final['x']" will trigger an annotationlib import in order to access the ForwardRef class). - Creating a TypedDict or NamedTuple (unless it's empty or PEP 563 is on). Lots of programs will want to use typing without any of these, so the tradeoff seems worth it.
This commit is contained in:
parent
04bc681e7c
commit
305be5fb1a
1 changed files with 48 additions and 31 deletions
|
@ -19,8 +19,6 @@ that may be changed without notice. Use at your own risk!
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from abc import abstractmethod, ABCMeta
|
from abc import abstractmethod, ABCMeta
|
||||||
import annotationlib
|
|
||||||
from annotationlib import ForwardRef
|
|
||||||
import collections
|
import collections
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
import collections.abc
|
import collections.abc
|
||||||
|
@ -163,6 +161,15 @@ __all__ = [
|
||||||
'Unpack',
|
'Unpack',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
class _LazyAnnotationLib:
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
global _lazy_annotationlib
|
||||||
|
import annotationlib
|
||||||
|
_lazy_annotationlib = annotationlib
|
||||||
|
return getattr(annotationlib, attr)
|
||||||
|
|
||||||
|
_lazy_annotationlib = _LazyAnnotationLib()
|
||||||
|
|
||||||
|
|
||||||
def _type_convert(arg, module=None, *, allow_special_forms=False):
|
def _type_convert(arg, module=None, *, allow_special_forms=False):
|
||||||
"""For converting None to type(None), and strings to ForwardRef."""
|
"""For converting None to type(None), and strings to ForwardRef."""
|
||||||
|
@ -246,7 +253,7 @@ def _type_repr(obj):
|
||||||
if isinstance(obj, tuple):
|
if isinstance(obj, tuple):
|
||||||
# Special case for `repr` of types with `ParamSpec`:
|
# Special case for `repr` of types with `ParamSpec`:
|
||||||
return '[' + ', '.join(_type_repr(t) for t in obj) + ']'
|
return '[' + ', '.join(_type_repr(t) for t in obj) + ']'
|
||||||
return annotationlib.value_to_string(obj)
|
return _lazy_annotationlib.value_to_string(obj)
|
||||||
|
|
||||||
|
|
||||||
def _collect_type_parameters(args, *, enforce_default_ordering: bool = True):
|
def _collect_type_parameters(args, *, enforce_default_ordering: bool = True):
|
||||||
|
@ -423,7 +430,7 @@ _sentinel = _Sentinel()
|
||||||
|
|
||||||
|
|
||||||
def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=frozenset(),
|
def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=frozenset(),
|
||||||
format=annotationlib.Format.VALUE, owner=None):
|
format=None, owner=None):
|
||||||
"""Evaluate all forward references in the given type t.
|
"""Evaluate all forward references in the given type t.
|
||||||
|
|
||||||
For use of globalns and localns see the docstring for get_type_hints().
|
For use of globalns and localns see the docstring for get_type_hints().
|
||||||
|
@ -433,7 +440,7 @@ def _eval_type(t, globalns, localns, type_params=_sentinel, *, recursive_guard=f
|
||||||
if type_params is _sentinel:
|
if type_params is _sentinel:
|
||||||
_deprecation_warning_for_no_type_params_passed("typing._eval_type")
|
_deprecation_warning_for_no_type_params_passed("typing._eval_type")
|
||||||
type_params = ()
|
type_params = ()
|
||||||
if isinstance(t, ForwardRef):
|
if isinstance(t, _lazy_annotationlib.ForwardRef):
|
||||||
# If the forward_ref has __forward_module__ set, evaluate() infers the globals
|
# If the forward_ref has __forward_module__ set, evaluate() infers the globals
|
||||||
# from the module, and it will probably pick better than the globals we have here.
|
# from the module, and it will probably pick better than the globals we have here.
|
||||||
if t.__forward_module__ is not None:
|
if t.__forward_module__ is not None:
|
||||||
|
@ -930,7 +937,7 @@ def TypeIs(self, parameters):
|
||||||
|
|
||||||
|
|
||||||
def _make_forward_ref(code, **kwargs):
|
def _make_forward_ref(code, **kwargs):
|
||||||
forward_ref = ForwardRef(code, **kwargs)
|
forward_ref = _lazy_annotationlib.ForwardRef(code, **kwargs)
|
||||||
# For compatibility, eagerly compile the forwardref's code.
|
# For compatibility, eagerly compile the forwardref's code.
|
||||||
forward_ref.__forward_code__
|
forward_ref.__forward_code__
|
||||||
return forward_ref
|
return forward_ref
|
||||||
|
@ -943,7 +950,7 @@ def evaluate_forward_ref(
|
||||||
globals=None,
|
globals=None,
|
||||||
locals=None,
|
locals=None,
|
||||||
type_params=None,
|
type_params=None,
|
||||||
format=annotationlib.Format.VALUE,
|
format=None,
|
||||||
_recursive_guard=frozenset(),
|
_recursive_guard=frozenset(),
|
||||||
):
|
):
|
||||||
"""Evaluate a forward reference as a type hint.
|
"""Evaluate a forward reference as a type hint.
|
||||||
|
@ -965,10 +972,11 @@ def evaluate_forward_ref(
|
||||||
evaluating the forward reference. This parameter should be provided (though
|
evaluating the forward reference. This parameter should be provided (though
|
||||||
it may be an empty tuple) if *owner* is not given and the forward reference
|
it may be an empty tuple) if *owner* is not given and the forward reference
|
||||||
does not already have an owner set. *format* specifies the format of the
|
does not already have an owner set. *format* specifies the format of the
|
||||||
annotation and is a member of the annotationlib.Format enum.
|
annotation and is a member of the annotationlib.Format enum, defaulting to
|
||||||
|
VALUE.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
if format == annotationlib.Format.STRING:
|
if format == _lazy_annotationlib.Format.STRING:
|
||||||
return forward_ref.__forward_arg__
|
return forward_ref.__forward_arg__
|
||||||
if forward_ref.__forward_arg__ in _recursive_guard:
|
if forward_ref.__forward_arg__ in _recursive_guard:
|
||||||
return forward_ref
|
return forward_ref
|
||||||
|
@ -977,7 +985,7 @@ def evaluate_forward_ref(
|
||||||
value = forward_ref.evaluate(globals=globals, locals=locals,
|
value = forward_ref.evaluate(globals=globals, locals=locals,
|
||||||
type_params=type_params, owner=owner)
|
type_params=type_params, owner=owner)
|
||||||
except NameError:
|
except NameError:
|
||||||
if format == annotationlib.Format.FORWARDREF:
|
if format == _lazy_annotationlib.Format.FORWARDREF:
|
||||||
return forward_ref
|
return forward_ref
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
@ -2257,7 +2265,7 @@ def assert_type(val, typ, /):
|
||||||
|
|
||||||
|
|
||||||
def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
|
def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
|
||||||
*, format=annotationlib.Format.VALUE):
|
*, format=None):
|
||||||
"""Return type hints for an object.
|
"""Return type hints for an object.
|
||||||
|
|
||||||
This is often the same as obj.__annotations__, but it handles
|
This is often the same as obj.__annotations__, but it handles
|
||||||
|
@ -2290,12 +2298,15 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
|
||||||
"""
|
"""
|
||||||
if getattr(obj, '__no_type_check__', None):
|
if getattr(obj, '__no_type_check__', None):
|
||||||
return {}
|
return {}
|
||||||
|
Format = _lazy_annotationlib.Format
|
||||||
|
if format is None:
|
||||||
|
format = Format.VALUE
|
||||||
# Classes require a special treatment.
|
# Classes require a special treatment.
|
||||||
if isinstance(obj, type):
|
if isinstance(obj, type):
|
||||||
hints = {}
|
hints = {}
|
||||||
for base in reversed(obj.__mro__):
|
for base in reversed(obj.__mro__):
|
||||||
ann = annotationlib.get_annotations(base, format=format)
|
ann = _lazy_annotationlib.get_annotations(base, format=format)
|
||||||
if format == annotationlib.Format.STRING:
|
if format == Format.STRING:
|
||||||
hints.update(ann)
|
hints.update(ann)
|
||||||
continue
|
continue
|
||||||
if globalns is None:
|
if globalns is None:
|
||||||
|
@ -2319,12 +2330,12 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
|
||||||
value = _eval_type(value, base_globals, base_locals, base.__type_params__,
|
value = _eval_type(value, base_globals, base_locals, base.__type_params__,
|
||||||
format=format, owner=obj)
|
format=format, owner=obj)
|
||||||
hints[name] = value
|
hints[name] = value
|
||||||
if include_extras or format == annotationlib.Format.STRING:
|
if include_extras or format == Format.STRING:
|
||||||
return hints
|
return hints
|
||||||
else:
|
else:
|
||||||
return {k: _strip_annotations(t) for k, t in hints.items()}
|
return {k: _strip_annotations(t) for k, t in hints.items()}
|
||||||
|
|
||||||
hints = annotationlib.get_annotations(obj, format=format)
|
hints = _lazy_annotationlib.get_annotations(obj, format=format)
|
||||||
if (
|
if (
|
||||||
not hints
|
not hints
|
||||||
and not isinstance(obj, types.ModuleType)
|
and not isinstance(obj, types.ModuleType)
|
||||||
|
@ -2333,7 +2344,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
|
||||||
and not hasattr(obj, '__annotate__')
|
and not hasattr(obj, '__annotate__')
|
||||||
):
|
):
|
||||||
raise TypeError(f"{obj!r} is not a module, class, or callable.")
|
raise TypeError(f"{obj!r} is not a module, class, or callable.")
|
||||||
if format == annotationlib.Format.STRING:
|
if format == Format.STRING:
|
||||||
return hints
|
return hints
|
||||||
|
|
||||||
if globalns is None:
|
if globalns is None:
|
||||||
|
@ -2850,10 +2861,10 @@ def _make_eager_annotate(types):
|
||||||
for key, val in types.items()}
|
for key, val in types.items()}
|
||||||
def annotate(format):
|
def annotate(format):
|
||||||
match format:
|
match format:
|
||||||
case annotationlib.Format.VALUE | annotationlib.Format.FORWARDREF:
|
case _lazy_annotationlib.Format.VALUE | _lazy_annotationlib.Format.FORWARDREF:
|
||||||
return checked_types
|
return checked_types
|
||||||
case annotationlib.Format.STRING:
|
case _lazy_annotationlib.Format.STRING:
|
||||||
return annotationlib.annotations_to_string(types)
|
return _lazy_annotationlib.annotations_to_string(types)
|
||||||
case _:
|
case _:
|
||||||
raise NotImplementedError(format)
|
raise NotImplementedError(format)
|
||||||
return annotate
|
return annotate
|
||||||
|
@ -2884,7 +2895,8 @@ class NamedTupleMeta(type):
|
||||||
annotate = _make_eager_annotate(types)
|
annotate = _make_eager_annotate(types)
|
||||||
elif "__annotate__" in ns:
|
elif "__annotate__" in ns:
|
||||||
original_annotate = ns["__annotate__"]
|
original_annotate = ns["__annotate__"]
|
||||||
types = annotationlib.call_annotate_function(original_annotate, annotationlib.Format.FORWARDREF)
|
types = _lazy_annotationlib.call_annotate_function(
|
||||||
|
original_annotate, _lazy_annotationlib.Format.FORWARDREF)
|
||||||
field_names = list(types)
|
field_names = list(types)
|
||||||
|
|
||||||
# For backward compatibility, type-check all the types at creation time
|
# For backward compatibility, type-check all the types at creation time
|
||||||
|
@ -2892,8 +2904,9 @@ class NamedTupleMeta(type):
|
||||||
_type_check(typ, "field annotation must be a type")
|
_type_check(typ, "field annotation must be a type")
|
||||||
|
|
||||||
def annotate(format):
|
def annotate(format):
|
||||||
annos = annotationlib.call_annotate_function(original_annotate, format)
|
annos = _lazy_annotationlib.call_annotate_function(
|
||||||
if format != annotationlib.Format.STRING:
|
original_annotate, format)
|
||||||
|
if format != _lazy_annotationlib.Format.STRING:
|
||||||
return {key: _type_check(val, f"field {key} annotation must be a type")
|
return {key: _type_check(val, f"field {key} annotation must be a type")
|
||||||
for key, val in annos.items()}
|
for key, val in annos.items()}
|
||||||
return annos
|
return annos
|
||||||
|
@ -3069,8 +3082,8 @@ class _TypedDictMeta(type):
|
||||||
own_annotations = ns["__annotations__"]
|
own_annotations = ns["__annotations__"]
|
||||||
elif "__annotate__" in ns:
|
elif "__annotate__" in ns:
|
||||||
own_annotate = ns["__annotate__"]
|
own_annotate = ns["__annotate__"]
|
||||||
own_annotations = annotationlib.call_annotate_function(
|
own_annotations = _lazy_annotationlib.call_annotate_function(
|
||||||
own_annotate, annotationlib.Format.FORWARDREF, owner=tp_dict
|
own_annotate, _lazy_annotationlib.Format.FORWARDREF, owner=tp_dict
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
own_annotate = None
|
own_annotate = None
|
||||||
|
@ -3137,18 +3150,20 @@ class _TypedDictMeta(type):
|
||||||
base_annotate = base.__annotate__
|
base_annotate = base.__annotate__
|
||||||
if base_annotate is None:
|
if base_annotate is None:
|
||||||
continue
|
continue
|
||||||
base_annos = annotationlib.call_annotate_function(base.__annotate__, format, owner=base)
|
base_annos = _lazy_annotationlib.call_annotate_function(
|
||||||
|
base.__annotate__, format, owner=base)
|
||||||
annos.update(base_annos)
|
annos.update(base_annos)
|
||||||
if own_annotate is not None:
|
if own_annotate is not None:
|
||||||
own = annotationlib.call_annotate_function(own_annotate, format, owner=tp_dict)
|
own = _lazy_annotationlib.call_annotate_function(
|
||||||
if format != annotationlib.Format.STRING:
|
own_annotate, format, owner=tp_dict)
|
||||||
|
if format != _lazy_annotationlib.Format.STRING:
|
||||||
own = {
|
own = {
|
||||||
n: _type_check(tp, msg, module=tp_dict.__module__)
|
n: _type_check(tp, msg, module=tp_dict.__module__)
|
||||||
for n, tp in own.items()
|
for n, tp in own.items()
|
||||||
}
|
}
|
||||||
elif format == annotationlib.Format.STRING:
|
elif format == _lazy_annotationlib.Format.STRING:
|
||||||
own = annotationlib.annotations_to_string(own_annotations)
|
own = _lazy_annotationlib.annotations_to_string(own_annotations)
|
||||||
elif format in (annotationlib.Format.FORWARDREF, annotationlib.Format.VALUE):
|
elif format in (_lazy_annotationlib.Format.FORWARDREF, _lazy_annotationlib.Format.VALUE):
|
||||||
own = own_checked_annotations
|
own = own_checked_annotations
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(format)
|
raise NotImplementedError(format)
|
||||||
|
@ -3732,7 +3747,9 @@ def __getattr__(attr):
|
||||||
Soft-deprecated objects which are costly to create
|
Soft-deprecated objects which are costly to create
|
||||||
are only created on-demand here.
|
are only created on-demand here.
|
||||||
"""
|
"""
|
||||||
if attr in {"Pattern", "Match"}:
|
if attr == "ForwardRef":
|
||||||
|
obj = _lazy_annotationlib.ForwardRef
|
||||||
|
elif attr in {"Pattern", "Match"}:
|
||||||
import re
|
import re
|
||||||
obj = _alias(getattr(re, attr), 1)
|
obj = _alias(getattr(re, attr), 1)
|
||||||
elif attr in {"ContextManager", "AsyncContextManager"}:
|
elif attr in {"ContextManager", "AsyncContextManager"}:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue