|
23 | 23 | from collections import defaultdict
|
24 | 24 | import collections.abc
|
25 | 25 | import copyreg
|
26 |
| -import contextlib |
27 | 26 | import functools
|
28 | 27 | import operator
|
29 |
| -import re as stdlib_re # Avoid confusion with the typing.re namespace on <=3.11 |
30 | 28 | import sys
|
31 | 29 | import types
|
32 | 30 | from types import WrapperDescriptorType, MethodWrapperType, MethodDescriptorType, GenericAlias
|
@@ -2580,8 +2578,6 @@ class Other(Leaf): # Error reported by type checker
|
2580 | 2578 | KeysView = _alias(collections.abc.KeysView, 1)
|
2581 | 2579 | ItemsView = _alias(collections.abc.ItemsView, 2)
|
2582 | 2580 | ValuesView = _alias(collections.abc.ValuesView, 1)
|
2583 |
| -ContextManager = _alias(contextlib.AbstractContextManager, 1, name='ContextManager') |
2584 |
| -AsyncContextManager = _alias(contextlib.AbstractAsyncContextManager, 1, name='AsyncContextManager') |
2585 | 2581 | Dict = _alias(dict, 2, inst=False, name='Dict')
|
2586 | 2582 | DefaultDict = _alias(collections.defaultdict, 2, name='DefaultDict')
|
2587 | 2583 | OrderedDict = _alias(collections.OrderedDict, 2)
|
@@ -3238,10 +3234,6 @@ def __enter__(self) -> 'TextIO':
|
3238 | 3234 | pass
|
3239 | 3235 |
|
3240 | 3236 |
|
3241 |
| -Pattern = _alias(stdlib_re.Pattern, 1) |
3242 |
| -Match = _alias(stdlib_re.Match, 1) |
3243 |
| - |
3244 |
| - |
3245 | 3237 | def reveal_type[T](obj: T, /) -> T:
|
3246 | 3238 | """Reveal the inferred type of a variable.
|
3247 | 3239 |
|
@@ -3426,3 +3418,21 @@ def get_protocol_members(tp: type, /) -> frozenset[str]:
|
3426 | 3418 | if not is_protocol(tp):
|
3427 | 3419 | raise TypeError(f'{tp!r} is not a Protocol')
|
3428 | 3420 | return frozenset(tp.__protocol_attrs__)
|
| 3421 | + |
| 3422 | + |
| 3423 | +def __getattr__(attr): |
| 3424 | + """Improve the import time of the typing module. |
| 3425 | +
|
| 3426 | + Soft-deprecated objects which are costly to create |
| 3427 | + are only created on-demand here. |
| 3428 | + """ |
| 3429 | + if attr in {"Pattern", "Match"}: |
| 3430 | + import re |
| 3431 | + obj = _alias(getattr(re, attr), 1) |
| 3432 | + elif attr in {"ContextManager", "AsyncContextManager"}: |
| 3433 | + import contextlib |
| 3434 | + obj = _alias(getattr(contextlib, f"Abstract{attr}"), 1, name=attr) |
| 3435 | + else: |
| 3436 | + raise AttributeError(f"Module 'typing' has no attribute {attr!r}") |
| 3437 | + globals()[attr] = obj |
| 3438 | + return obj |
0 commit comments