Skip to content

Commit dca8fc3

Browse files
committed
Lazy import re module
1 parent 5206a52 commit dca8fc3

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

Lib/dataclasses.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import re
1+
lazy import re
22
import sys
33
lazy import copy
44
import types
@@ -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,6 +803,13 @@ 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

806+
# String regex that string annotations for ClassVar or InitVar must match.
807+
# Allows "identifier.identifier[" or "identifier[".
808+
# https://bugs.python.org/issue33453 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+
807813
match = _MODULE_IDENTIFIER_RE.match(annotation)
808814
if match:
809815
ns = None

0 commit comments

Comments
 (0)