|
8 | 8 | import abc |
9 | 9 | from reprlib import recursive_repr |
10 | 10 |
|
| 11 | +lazy import inspect |
11 | 12 |
|
12 | 13 | __all__ = ['dataclass', |
13 | 14 | 'field', |
@@ -981,15 +982,12 @@ def _hash_exception(cls, fields, func_builder): |
981 | 982 | # See https://bugs.python.org/issue32929#msg312829 for an if-statement |
982 | 983 | # version of this table. |
983 | 984 |
|
984 | | -class AutoDocstring: |
985 | | - """A non-data descriptor to autogenerate class docstring |
986 | | - from the signature of its __init__ method. |
987 | | - """ |
| 985 | +# A non-data descriptor to autogenerate class docstring |
| 986 | +# from the signature of its __init__ method on demand. |
| 987 | +# The primary reason is to be able to lazy import `inspect` module. |
| 988 | +class _AutoDocstring: |
988 | 989 |
|
989 | 990 | def __get__(self, _obj, cls): |
990 | | - # TODO: Make this top-level lazy import once PEP810 lands |
991 | | - import inspect |
992 | | - |
993 | 991 | try: |
994 | 992 | # In some cases fetching a signature is not possible. |
995 | 993 | # But, we surely should not fail in this case. |
@@ -1237,7 +1235,7 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen, |
1237 | 1235 | if not getattr(cls, '__doc__'): |
1238 | 1236 | # Create a class doc-string lazily via descriptor protocol |
1239 | 1237 | # to avoid importing `inspect` module. |
1240 | | - cls.__doc__ = AutoDocstring() |
| 1238 | + cls.__doc__ = _AutoDocstring() |
1241 | 1239 |
|
1242 | 1240 | if match_args: |
1243 | 1241 | # I could probably compute this once. |
@@ -1392,8 +1390,6 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields): |
1392 | 1390 |
|
1393 | 1391 | # If this is a wrapped function, unwrap it. |
1394 | 1392 | if not isinstance(member, type) and hasattr(member, '__wrapped__'): |
1395 | | - # TODO: Make this top-level lazy import once PEP810 lands |
1396 | | - import inspect |
1397 | 1393 | member = inspect.unwrap(member) |
1398 | 1394 |
|
1399 | 1395 | if isinstance(member, types.FunctionType): |
|
0 commit comments