Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
lazy import re
import sys
import copy
lazy import copy
import types
import inspect
import keyword
Expand Down Expand Up @@ -217,9 +217,8 @@ def __repr__(self):
_POST_INIT_NAME = '__post_init__'

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

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

# String regex that string annotations for ClassVar or InitVar must match.
# Allows "identifier.identifier[" or "identifier[".
# https://bugs.python.org/issue33453 for details.
Comment thread
danielhollas marked this conversation as resolved.
Outdated
global _MODULE_IDENTIFIER_RE
if _MODULE_IDENTIFIER_RE is None:
_MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)')

match = _MODULE_IDENTIFIER_RE.match(annotation)
Comment thread
hugovk marked this conversation as resolved.
Outdated
if match:
ns = None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improve import time of :mod:`dataclasses` module by lazy importing :mod:`re`
and :mod:`copy` modules.
Loading