summaryrefslogtreecommitdiffstats
path: root/Lib/typing.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/typing.py')
-rw-r--r--Lib/typing.py26
1 files changed, 18 insertions, 8 deletions
diff --git a/Lib/typing.py b/Lib/typing.py
index 183d5b2..639be75 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -23,10 +23,8 @@ import collections
from collections import defaultdict
import collections.abc
import copyreg
-import contextlib
import functools
import operator
-import re as stdlib_re # Avoid confusion with the typing.re namespace on <=3.11
import sys
import types
from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
@@ -2580,8 +2578,6 @@ MappingView = _alias(collections.abc.MappingView, 1)
KeysView = _alias(collections.abc.KeysView, 1)
ItemsView = _alias(collections.abc.ItemsView, 2)
ValuesView = _alias(collections.abc.ValuesView, 1)
-ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager')
-AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager')
Dict = _alias(dict, 2, inst=False, name='Dict')
DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
OrderedDict = _alias(collections.OrderedDict, 2)
@@ -3238,10 +3234,6 @@ class TextIO(IO[str]):
pass
-Pattern = _alias(stdlib_re.Pattern, 1)
-Match = _alias(stdlib_re.Match, 1)
-
-
def reveal_type[T](obj: T, /) -> T:
"""Reveal the inferred type of a variable.
@@ -3426,3 +3418,21 @@ def get_protocol_members(tp: type, /) -> frozenset[str]:
if not is_protocol(tp):
raise TypeError(f'{tp!r} is not a Protocol')
return frozenset(tp.__protocol_attrs__)
+
+
+def __getattr__(attr):
+ """Improve the import time of the typing module.
+
+ Soft-deprecated objects which are costly to create
+ are only created on-demand here.
+ """
+ if attr in {"Pattern", "Match"}:
+ import re
+ obj = _alias(getattr(re, attr), 1)
+ elif attr in {"ContextManager", "AsyncContextManager"}:
+ import contextlib
+ obj = _alias(getattr(contextlib, f"Abstract{attr}"), 1, name=attr)
+ else:
+ raise AttributeError(f"module {__name__!r} has no attribute {attr!r}")
+ globals()[attr] = obj
+ return obj