Skip to content

Commit dd2735d

Browse files
danielhollashugovk
andauthored
gh-137855: Improve import time of dataclasses by lazy importing re and copy modules (#148379)
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
1 parent ba1e1c1 commit dd2735d

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Lib/dataclasses.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import re
21
import sys
3-
import copy
42
import types
53
import inspect
64
import keyword
75
import itertools
86
import annotationlib
97
import abc
108
from reprlib import recursive_repr
9+
lazy import copy
10+
lazy import re
1111

1212

1313
__all__ = ['dataclass',
@@ -217,9 +217,8 @@ def __repr__(self):
217217
_POST_INIT_NAME = '__post_init__'
218218

219219
# String regex that string annotations for ClassVar or InitVar must match.
220-
# Allows "identifier.identifier[" or "identifier[".
221-
# https://bugs.python.org/issue33453 for details.
222-
_MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)')
220+
# This regular expression is compiled on demand so that 're' module can be imported lazily
221+
_MODULE_IDENTIFIER_RE = None
223222

224223
# Atomic immutable types which don't require any recursive handling and for which deepcopy
225224
# returns the same object. We can provide a fast-path for these types in asdict and astuple.
@@ -804,10 +803,17 @@ def _is_type(annotation, cls, a_module, a_type, is_type_predicate):
804803
# a eval() penalty for every single field of every dataclass
805804
# that's defined. It was judged not worth it.
806805

807-
match = _MODULE_IDENTIFIER_RE.match(annotation)
806+
# String regex that string annotations for ClassVar or InitVar must match.
807+
# Allows "identifier.identifier[" or "identifier[".
808+
# https://github.com/python/cpython/issues/77634 for details.
809+
global _MODULE_IDENTIFIER_RE
810+
if _MODULE_IDENTIFIER_RE is None:
811+
_MODULE_IDENTIFIER_RE = re.compile(r'(?:\s*(\w+)\s*\.)?\s*(\w+)')
812+
813+
match = _MODULE_IDENTIFIER_RE.prefixmatch(annotation)
808814
if match:
809815
ns = None
810-
module_name = match.group(1)
816+
module_name = match[1]
811817
if not module_name:
812818
# No module name, assume the class's module did
813819
# "from dataclasses import InitVar".
@@ -817,7 +823,7 @@ def _is_type(annotation, cls, a_module, a_type, is_type_predicate):
817823
module = sys.modules.get(cls.__module__)
818824
if module and module.__dict__.get(module_name) is a_module:
819825
ns = sys.modules.get(a_type.__module__).__dict__
820-
if ns and is_type_predicate(ns.get(match.group(2)), a_module):
826+
if ns and is_type_predicate(ns.get(match[2]), a_module):
821827
return True
822828
return False
823829

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Improve import time of :mod:`dataclasses` module by lazy importing :mod:`re`
2+
and :mod:`copy` modules.

0 commit comments

Comments
 (0)