:license: Python License.
"""
from _ast import *
-lazy from _colorize import can_colorize, get_theme
def parse(source, filename='<unknown>', mode='exec', *,
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):
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))
import abc
from reprlib import recursive_repr
lazy import copy
-lazy import inspect
lazy import re
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,
# 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):
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
--- /dev/null
+Fix import cycles exposed by running standard library modules with
+``-X lazy_imports=none``.