Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
:license: Python License.
"""
from _ast import *
lazy from _colorize import can_colorize, get_theme


def parse(source, filename='<unknown>', mode='exec', *,
Expand Down Expand Up @@ -142,6 +141,8 @@ def dump(
If show_empty is False, then empty lists and fields that are None
will be omitted from the output for better readability.
"""
from _colorize import get_theme

t = get_theme(force_color=color, force_no_color=not color).ast

def _format(node, level=0):
Expand Down Expand Up @@ -708,6 +709,7 @@ def main(args=None):

tree = parse(source, name, args.mode, type_comments=args.no_type_comments,
feature_version=feature_version, optimize=args.optimize)
from _colorize import can_colorize
print(dump(tree, include_attributes=args.include_attributes,
color=can_colorize(file=sys.stdout),
indent=args.indent, show_empty=args.show_empty))
Expand Down
3 changes: 2 additions & 1 deletion Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import abc
from reprlib import recursive_repr
lazy import copy
lazy import inspect
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised that this is needed. Up until very recently, inspect was imported eagerly in dataclasses. Isn't the issue with importing dataclasses fixed by the ast change alone?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC the ast change fixes one cycle, but not all of them. If I restore the top-level lazy import inspect in dataclasses, import dataclasses still fails under -X azy_imports=none via: dataclasses -> inspect -> tokenize -> _colorize -> dataclasses

So the inspect move is specifically about avoiding import-time resolution of the lazy inspect import when lazy imports are disabled. The failure is reproducible with a direct import dataclasses, and the stdlib import subprocess test catches it as well.

lazy import re


Expand Down Expand Up @@ -992,6 +991,7 @@ def __get__(self, _obj, cls):
try:
# In some cases fetching a signature is not possible.
# But, we surely should not fail in this case.
import inspect
text_sig = str(inspect.signature(
cls,
annotation_format=annotationlib.Format.FORWARDREF,
Expand Down Expand Up @@ -1401,6 +1401,7 @@ def _add_slots(cls, is_frozen, weakref_slot, defined_fields):

# If this is a wrapped function, unwrap it.
if not isinstance(member, type) and hasattr(member, '__wrapped__'):
import inspect
member = inspect.unwrap(member)

if isinstance(member, types.FunctionType):
Expand Down
44 changes: 44 additions & 0 deletions Lib/test/test_lazy_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,50 @@ def test_cli_lazy_imports_none_forces_all_imports_eager(self):
self.assertEqual(result.returncode, 0, f"stderr: {result.stderr}")
self.assertIn("EAGER", result.stdout)

@support.requires_resource("cpu")
def test_cli_lazy_imports_modes_import_stdlib_modules(self):
"""-X lazy_imports modes should import available stdlib modules."""
# Do not smoke-test modules with intentional import-time effects.
import_side_effect_modules = {"antigravity", "this"}
importable = []

for module in sorted(sys.stdlib_module_names):
if module in import_side_effect_modules:
continue

with self.subTest(module=module):
code = f"import {module}; print({module})"
baseline = subprocess.run(
[sys.executable, "-I", "-c", code],
capture_output=True,
text=True,
timeout=60,
)
if baseline.returncode:
# sys.stdlib_module_names includes modules for other
# platforms and optional extension modules not built here.
continue
importable.append(module)

for mode in ("normal", "none"):
with self.subTest(module=module, mode=mode):
result = subprocess.run(
[
sys.executable,
"-I",
"-X",
f"lazy_imports={mode}",
"-c",
code,
],
capture_output=True,
text=True,
timeout=60,
)
self.assertEqual(result.returncode, 0, result.stderr)

self.assertGreater(len(importable), 100)

def test_cli_lazy_imports_normal_respects_lazy_keyword_only(self):
"""-X lazy_imports=normal should respect lazy keyword only."""
# Note: Use test modules instead of stdlib modules to avoid
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix import cycles exposed by running standard library modules with
``-X lazy_imports=none``.
Loading