Skip to content

Take the TYPE_CHECKING branch over its else when inferring names - #963

Open
marlon-costa-dc wants to merge 2 commits into
python-rope:masterfrom
marlon-costa-dc:pr/type-checking-branch
Open

marlon-costa-dc wants to merge 2 commits into
python-rope:masterfrom
marlon-costa-dc:pr/type-checking-branch

Conversation

@marlon-costa-dc

Copy link
Copy Markdown
Contributor

Static analysis assumes typing.TYPE_CHECKING is true (PEP 484), but rope takes the last assignment of a name, so in

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    Alias = Checked
else:
    Alias = Runtime
class Derived(Alias): ...

Alias infers as Runtime, and Derived gets the runtime base instead of the declared one.

This makes inference skip assignments that sit in the else of an if whose test reads typing.TYPE_CHECKING:

  • The scope visitor records, on each assignment, the tests of the if statements whose else holds it (AssignmentValue.orelse_tests).
  • The test is followed through imports, attributes of imported modules and a name's single plain assignment (like TYPE_CHECKING = typing.TYPE_CHECKING in typing_extensions), without inferring any object. The constant is recognized as the TYPE_CHECKING attribute of the module named typing, whatever its value, so a project typing module binding TYPE_CHECKING = False still reads as true.
  • Any other test, not TYPE_CHECKING, and a rebound name keep today's behaviour. Decisions are kept in the module's concluded data.

Tests cover TYPE_CHECKING, typing.TYPE_CHECKING, t.TYPE_CHECKING and a once-bound alias (the if/else pick fails on master), class bases read through the alias, the undecided cases, and a project-local typing module.

Local run (CPython 3.13): pytest 2182 passed, 7 skipped, 5 xfailed, 7 subtests passed; the one error, test_get_package_tuple_compiled, comes from that interpreter's statically linked _sqlite3 and is unrelated. pre-commit (black, isort, whitespace) passes.

🤖 Generated with Claude Code

marlon-costa-dc and others added 2 commits November 21, 2026 23:28
Static analysis assumes `typing.TYPE_CHECKING` is true (PEP 484), but rope
took the last assignment, so in

    if TYPE_CHECKING:
        Alias = Checked
    else:
        Alias = Runtime

`Alias` inferred as `Runtime`, and classes based on it lost their base.

The scope visitor records, on each assignment, the tests of the `if`
statements whose `else` holds it. Inference skips assignments under the
`else` of a test that reads `typing.TYPE_CHECKING`, followed through
imports, attributes of imported modules and a name's single plain
assignment, without inferring any object. The constant is recognized as the
`TYPE_CHECKING` attribute of the module named `typing`, whatever its value.
Other tests, `not TYPE_CHECKING` and rebound names keep today's behaviour.
Decisions are kept in the module's concluded data.

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

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.67442% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 95.45%. Comparing base (ff8a306) to head (213a826).

Files with missing lines Patch % Lines
rope/base/oi/soi.py 95.74% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #963   +/-   ##
=======================================
  Coverage   95.44%   95.45%           
=======================================
  Files         134      134           
  Lines       26910    26995   +85     
=======================================
+ Hits        25685    25768   +83     
- Misses       1225     1227    +2     

☔ 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

Copy link
Copy Markdown
Contributor Author

The upstream test workflows wait for maintainer approval on fork PRs. For reference, this head passed the fork's full CI test matrix: https://github.com/marlon-costa-dc/rope/actions/runs/44976350714

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.

1 participant