Skip to content

Compute scope regions from native AST positions - #909

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

marlon-costa-dc wants to merge 4 commits 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

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.48%. Comparing base (ff8a306) to head (657f9d7).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #909      +/-   ##
==========================================
+ Coverage   95.44%   95.48%   +0.03%     
==========================================
  Files         134      134              
  Lines       26910    27010     +100     
==========================================
+ Hits        25685    25791     +106     
+ 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.

marlon-costa-dc added a commit to marlon-costa-dc/rope that referenced this pull request Nov 7, 2026
Our open python-rope/rope PRs carry tests that the fork release absorbed
in behaviour but not in tests. Each passes against the current code:
- python-rope#908 test_assignment_hints_ignore_names_that_are_not_assigned
- python-rope#909 test_scope_region_rejects_decorator_without_source_positions,
       test_scope_region_rejects_decorator_without_source_marker
- python-rope#920 test_hint_param_on_a_hierarchy_python_refuses
- python-rope#910 test_find_module_skips_python_path_files
- python-rope#844 test_format_string_region_without_closing_quote_is_rejected

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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