Skip to content

FIX: handling multi-target (from-)import statements#434

Open
TTsangSC wants to merge 5 commits into
pyutils:mainfrom
TTsangSC:433-fix
Open

FIX: handling multi-target (from-)import statements#434
TTsangSC wants to merge 5 commits into
pyutils:mainfrom
TTsangSC:433-fix

Conversation

@TTsangSC

Copy link
Copy Markdown
Collaborator

Closes #433.

Changes

  • Added the optional argument assume_single_target_imports to line_profiler.autoprofile.profmod_extractor.ProfmodExtractor.run():
    • True (default) retains the old behavior (method returns dict[int, str]), but issues (1) a DeprecationWarning, and (2) in addition a UserWarning when this results in profiling targets being dropped
    • False changes the return type to dict[int, list[str]], so as to properly handle statements like from foo import spam, ham, eggs as jam.
  • line_profiler.autoprofile.ast_tree_profiler.AstTreeProfiler has been correspondingly updated to insert prof.add_imported_function_or_module() statements for each eligible target in multi-target imports.
  • Added ty config line to pyproject.toml to guard against v0.0.52's making redundant casts an error.
  • Added new tests in tests/test_autoprofile.py to verify the fixes:
    • test_multitarget_import_resolution() checks that AstTreeProfiler.profile() now correctly transforms ASTs with multi-target import statements.
    • test_profmod_extractor_multitarget_behavior() checks that ProfmodExtractor.run()'s return type and warning issuance are as aformentioned.

TTsangSC added 5 commits June 29, 2026 21:19
line_profiler/autoprofile/
    ast_tree_profiler.py::AstTreeProfiler.profile()
        Updated implementation to account for multiple import targets
        in the same (from-)import statement
    profmod_extractor.py
        - Removed unused typing imports
        - Updated implementation and return type of
          `ProfmodExtractor.run()` to account for multiple import
          targets in the same (from-)import statement

XXX:
    should we add a switch for `ProfmodExtractor.run()` so that it
    behaves as before by default?
CHANGELOG.rst
    Added entry

tests/test_autoprofile.py::test_multitarget_import_resolution()
    New parametrized test testing that issue pyutils#433 has been fixed
line_profiler/autoprofile/ast_tree_profiler.py::AstTreeProfiler
    __init__()
        Updated type hints for `ast_transformer_class_handler` and
        `profmod_extractor_class_handler` to be more specific
    profile()
        Updated call to `ProfmodExtractor.run()` to include
        `assume_single_target_imports=False` (see below)

line_profiler/autoprofile/profmod_extractor.py::ProfmodExtractor.run()
    Updated call signature:
    - New parameter `assume_single_target_imports` defauling to true,
      restoring the legacy behavior
    - If `assume_single_target_imports` is true, a `DeprecationWarning`
      is issued; and if it results in an valid profiling target being
      dropped, a `UserWarning` is issued
    - If false, the new behavior (returning a `dict[int, list[str]]`) is
      used

tests/test_autoprofile.py::test_profmod_extractor_multitarget_behavior()
    New parametrized test to cover the aforementioned warning behavior,
    ensure that backward compatibility is retained while users are
    warned against the shortcomings
@codecov

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.97%. Comparing base (4940bca) to head (3a40fe8).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #434      +/-   ##
==========================================
+ Coverage   82.40%   83.97%   +1.57%     
==========================================
  Files          20       20              
  Lines        2256     2278      +22     
  Branches      359      364       +5     
==========================================
+ Hits         1859     1913      +54     
+ Misses        300      268      -32     
  Partials       97       97              
Files with missing lines Coverage Δ
line_profiler/autoprofile/ast_tree_profiler.py 100.00% <100.00%> (+13.95%) ⬆️
line_profiler/autoprofile/profmod_extractor.py 84.42% <100.00%> (+23.42%) ⬆️

... and 2 files with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f8e40f6...3a40fe8. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

autoprofile cannot handle multiple imports on the same line

1 participant