|
1 | | -import re |
2 | 1 | import sys |
3 | | -import copy |
4 | 2 | import types |
5 | | -import inspect |
6 | 3 | import keyword |
7 | 4 | import itertools |
8 | | -import annotationlib |
9 | 5 | import abc |
10 | 6 | from reprlib import recursive_repr |
11 | 7 |
|
@@ -219,7 +215,7 @@ def __repr__(self): |
219 | 215 | # String regex that string annotations for ClassVar or InitVar must match. |
220 | 216 | # Allows "identifier.identifier[" or "identifier[". |
221 | 217 | # https://bugs.python.org/issue33453 for details. |
222 | | -_MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)') |
| 218 | +_MODULE_IDENTIFIER_RE = None |
223 | 219 |
|
224 | 220 | # Atomic immutable types which don't require any recursive handling and for which deepcopy |
225 | 221 | # returns the same object. We can provide a fast-path for these types in asdict and astuple. |
@@ -747,6 +743,11 @@ def _is_type(annotation, cls, a_module, a_type, is_type_predicate): |
747 | 743 | # correct global and local namespaces. However that would involve |
748 | 744 | # a eval() penalty for every single field of every dataclass |
749 | 745 | # that's defined. It was judged not worth it. |
| 746 | + global _MODULE_IDENTIFIER_RE |
| 747 | + |
| 748 | + if _MODULE_IDENTIFIER_RE is None: |
| 749 | + import re |
| 750 | + _MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)') |
750 | 751 |
|
751 | 752 | match = _MODULE_IDENTIFIER_RE.match(annotation) |
752 | 753 | if match: |
@@ -982,6 +983,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, |
982 | 983 | # actual default value. Pseudo-fields ClassVars and InitVars are |
983 | 984 | # included, despite the fact that they're not real fields. That's |
984 | 985 | # dealt with later. |
| 986 | + import annotationlib |
985 | 987 | cls_annotations = annotationlib.get_annotations( |
986 | 988 | cls, format=annotationlib.Format.FORWARDREF) |
987 | 989 |
|
@@ -1160,6 +1162,8 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, |
1160 | 1162 |
|
1161 | 1163 | if not getattr(cls, '__doc__'): |
1162 | 1164 | # Create a class doc-string. |
| 1165 | + import inspect |
| 1166 | + |
1163 | 1167 | try: |
1164 | 1168 | # In some cases fetching a signature is not possible. |
1165 | 1169 | # But, we surely should not fail in this case. |
@@ -1318,6 +1322,8 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields): |
1318 | 1322 | # original class. We can break out of this loop as soon as we |
1319 | 1323 | # make an update, since all closures for a class will share a |
1320 | 1324 | # given cell. |
| 1325 | + import inspect |
| 1326 | + |
1321 | 1327 | for member in newcls.__dict__.values(): |
1322 | 1328 | # If this is a wrapped function, unwrap it. |
1323 | 1329 | member = inspect.unwrap(member) |
@@ -1484,7 +1490,8 @@ def _asdict_inner(obj, dict_factory): |
1484 | 1490 | # generator |
1485 | 1491 | return obj_type(_asdict_inner(v, dict_factory) for v in obj) |
1486 | 1492 | else: |
1487 | | - return copy.deepcopy(obj) |
| 1493 | + from copy import deepcopy |
| 1494 | + return deepcopy(obj) |
1488 | 1495 |
|
1489 | 1496 |
|
1490 | 1497 | def astuple(obj, *, tuple_factory=tuple): |
@@ -1544,7 +1551,8 @@ def _astuple_inner(obj, tuple_factory): |
1544 | 1551 | return obj_type((_astuple_inner(k, tuple_factory), _astuple_inner(v, tuple_factory)) |
1545 | 1552 | for k, v in obj.items()) |
1546 | 1553 | else: |
1547 | | - return copy.deepcopy(obj) |
| 1554 | + from copy import deepcopy |
| 1555 | + return deepcopy(obj) |
1548 | 1556 |
|
1549 | 1557 |
|
1550 | 1558 | def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, |
|
0 commit comments