From 180461fd8a793f63340e86446844d79f59c9e374 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Thu, 21 Sep 2023 15:17:56 +0100 Subject: [PATCH 1/3] Optimise the import time for `typing` --- Lib/test/test_typing.py | 4 ++++ Lib/typing.py | 26 ++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py index 4d1c0f2c724b86..9e891f113840be 100644 --- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -9373,6 +9373,10 @@ def test_all(self): self.assertIn('SupportsComplex', a) def test_all_exported_names(self): + # ensure all dynamically created objects are actualised + for name in typing.__all__: + getattr(typing, name) + actual_all = set(typing.__all__) computed_all = { k for k, v in vars(typing).items() diff --git a/Lib/typing.py b/Lib/typing.py index 183d5b29a23362..84b741bfb0dd25 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -23,10 +23,8 @@ 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 @@ class Other(Leaf): # Error reported by type checker 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 @@ def __enter__(self) -> 'TextIO': 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 'typing' has no attribute {attr!r}") + globals()[attr] = obj + return obj From 75901019413d559c6acd21d55880bb60ae06730e Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Thu, 21 Sep 2023 19:23:07 +0100 Subject: [PATCH 2/3] Update Lib/typing.py Co-authored-by: Thomas Grainger --- Lib/typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/typing.py b/Lib/typing.py index 84b741bfb0dd25..639be75747dae0 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -3433,6 +3433,6 @@ def __getattr__(attr): import contextlib obj = _alias(getattr(contextlib, f"Abstract{attr}"), 1, name=attr) else: - raise AttributeError(f"Module 'typing' has no attribute {attr!r}") + raise AttributeError(f"module {__name__!r} has no attribute {attr!r}") globals()[attr] = obj return obj From b57caaabb6729611948970010f7a0b3786797c62 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Thu, 21 Sep 2023 19:42:24 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-09-21-19-42-22.gh-issue-109653.bL3iLH.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-09-21-19-42-22.gh-issue-109653.bL3iLH.rst diff --git a/Misc/NEWS.d/next/Library/2023-09-21-19-42-22.gh-issue-109653.bL3iLH.rst b/Misc/NEWS.d/next/Library/2023-09-21-19-42-22.gh-issue-109653.bL3iLH.rst new file mode 100644 index 00000000000000..9f794bb58ba63b --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-09-21-19-42-22.gh-issue-109653.bL3iLH.rst @@ -0,0 +1,2 @@ +Reduce the import time of :mod:`typing` by around a third. +Patch by Alex Waygood.