Skip to content

Fix source regions for regular argument annotations - #887

Open
Strohutt wants to merge 1 commit into
python-rope:masterfrom
Strohutt:fix/argument-annotation-regions
Open

Strohutt wants to merge 1 commit into
python-rope:masterfrom
Strohutt:fix/argument-annotation-regions

Conversation

@Strohutt

Copy link
Copy Markdown

Description

Searching a parameter annotation such as def foo(items: list): ... raises AttributeError because _arg leaves the annotation without a source region. Visit the annotation with the existing child-node handler, following v587su's proposed fix in #802.

Regression tests cover exact source regions, source round trips, wildcard matching, and an actual Restructure replacement of nested annotations with defaults. The change covers regular function arguments.

Fixes #802

Checklist

  • I have added tests that prove my fix is effective
  • I have updated CHANGELOG.md

Validation on Windows with Python 3.12.14: all five new regression tests fail against the original module and pass with the fix. Full suite: 2,153 passed, 12 skipped, 5 xfailed. Targeted Black and isort checks pass; wheel and sdist builds pass.

@marlon-costa-dc

Copy link
Copy Markdown
Contributor

Complementary regions while reviewing this PR: the argument-annotation : annotation hunk here is byte-identical to what we carry, but return annotations (-> node.returns) and class keywords (metaclass=, *bases, **options interleaved with bases by position) still walk to nothing on master.

We split that delta into a branch with round-trip and match-region tests at dc/pr-887 (patchedasttest 171 passed, similarfindertest 42 passed on 3.13) — happy to open it as a follow-up PR stacked on this one if the direction sounds right.

@mcepl

mcepl commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

This overlaps with #907, which includes the same regular-argument annotation fix alongside positional-only and keyword-only argument walking.

My preference is to consolidate the implementation in #907, but keep the additional coverage here. In particular, the Restructure replacement and annotation-matching tests exercise useful paths beyond AST round trips.

Could you coordinate with #907's author to carry those tests over? Please keep this PR open until the combined change lands and its coverage is verified. If the broader parameter work stalls, this remains a useful smaller fix to merge independently.

Visit each argument's annotation so structural searches and replacements
can use its source region. Cover the reported wildcard crash, nested
annotations, defaults, and source round trips.

Based on the diagnosis and proposed fix in issue python-rope#802.
@mcepl
mcepl force-pushed the fix/argument-annotation-regions branch from 777bfc9 to 83ee137 Compare December 3, 2026 18:55
@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.51%. Comparing base (b12a579) to head (83ee137).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #887      +/-   ##
==========================================
+ Coverage   95.48%   95.51%   +0.02%     
==========================================
  Files         134      134              
  Lines       27127    27171      +44     
==========================================
+ Hits        25902    25952      +50     
+ Misses       1225     1219       -6     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

@mcepl mcepl left a comment

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.

#907 was closed without being merged, so as discussed earlier this
smaller fix goes in on its own. Approved.

I rebased the branch onto current master and resolved the
conflicts: similarfindertest.py now keeps both your new tests and
the class-header test from #990, and the changelog entry moved from
CHANGELOG.md to changelog.d/887.md (see
changelog.d/README.md). The commit is otherwise unchanged.

Verified:

  • All 5 new tests fail on master (e.g. RawSimilarFinder on
    def foo(l: list) raises AttributeError: 'Name' object has no attribute 'region') and pass here.
  • No regressions in source round trips for regular, annotated,
    positional-only, keyword-only, annotated *args/**kwargs,
    async def and lambda signatures.
  • Full suite passes locally (2209 passed); CI is green on all 24
    checks (two macOS jobs needed a re-run after a runner shortage).

Non-blocking note: annotations on *args/**kwargs and on
positional-only/keyword-only parameters still get no regions,
because _arguments walks only node.args and emits varargs as
plain strings. That was the remaining part of #907; worth a
follow-up, but out of scope here, as the PR says.

@mcepl

mcepl commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

@lieryan, could you please take a look as well? Rebased onto master, CI is green. The remaining parameter kinds (*args/**kwargs, positional-only and keyword-only), which still crash similar-code search the same way, are now tracked in #994.

This branch has not been deployed

No deployments
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.

patchedast doesn't consider the annotation of arguments

3 participants