Skip to content

Compute scope regions from native AST positions - #909

Open
marlon-costa-dc wants to merge 1 commit into
python-rope:masterfrom
marlon-costa-dc:pr/scope-regions
Open

marlon-costa-dc wants to merge 1 commit into
python-rope:masterfrom
marlon-costa-dc:pr/scope-regions

Conversation

@marlon-costa-dc

Copy link
Copy Markdown
Contributor

pyscopes.get_region delegated to patchedast.patch_ast on the whole module — expensive for every scope query and wrong on modules patchedast cannot walk (unpatchable constructs, exotic newlines). It also mis-derived decorator starts.

Compute the region from the node's native AST positions (module = whole source), locating the @ of decorators via a reverse token search, handling Module/LF/CRLF/CR and normalized Unicode identifiers. Falls back to rope's own error when positions are incomplete.

pyscopestest: 53 passed + 18 subtests; patchedast/codeanalyze suites unaffected locally on 3.13.

Scope.get_region() no longer patches the whole module AST through
patchedast to borrow node regions.  It now resolves each scope's
region directly from the native AST positions via
codeanalyze.ASTLinesAdapter, and extends it to the enclosing
decorator by searching backwards for the "@" token with
_Source.rfind_token.

This keeps regions correct for sources the patched walker cannot
round-trip: normalized Unicode identifiers (e.g. "u\u0301" aliases),
decorated definitions, and raw string sources using LF, CRLF, or CR
newlines.  Modules resolve to the full source range, and nodes
without complete end positions raise RopeError instead of silently
producing partial regions.

Note: the original internal change also fixed _Source tokenization
for CR/CRLF sources; that code path was removed by the patchedast
cleanup (python-rope#896), which already computes positions through
ASTLinesAdapter, so only the pyscopes resolution is carried over.
Copilot AI balanced review requested due to automatic review settings October 18, 2026 14:35

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@codecov

codecov Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.84946% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.37%. Comparing base (22f5e50) to head (84c03fd).

Files with missing lines Patch % Lines
rope/base/pyscopes.py 90.90% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #909      +/-   ##
==========================================
+ Coverage   95.34%   95.37%   +0.02%     
==========================================
  Files         134      134              
  Lines       26762    26846      +84     
==========================================
+ Hits        25516    25604      +88     
+ Misses       1246     1242       -4     

☔ 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.

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.

2 participants