Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[red-knot] Consider all definitions after terminal statements unreachable #15676

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

dcreager
Copy link
Member

We can use the new statically known branches feature to address #14014, by adding an "always false" visibility constraint immediately after each terminal statement.

Test Plan

The new mdtests failed (with incorrect reveal_type results, and spurious possibly-unresolved-reference errors) before adding the new visibility constraints.

@dcreager dcreager added the red-knot Multi-file analysis & type inference label Jan 22, 2025
Copy link
Contributor

github-actions bot commented Jan 22, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@dcreager
Copy link
Member Author

There are a couple of new diagnostics in the benchmark that don't look correct to me. I need to see if I can minimize that into an mdtest to diagnose.

@dcreager dcreager marked this pull request as draft January 22, 2025 22:07
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic!! Love to see a feature that is easier than anticipated :)

x = "test"
else:
return "early"
return x
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to clarify, since it took me a moment on first read, even though you just explained it above:

Suggested change
return x
return x # no possibly-unresolved-reference diagnostic!

else:
break
return x
return "late"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could return x here as well and expect the possibly-unresolved-reference error?

reveal_type(x) # revealed: Literal["before", "loop", "break"]
```

## `return` is terminal in nested scopes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think "nested scopes" is the right term here; Python if statements don't establish nested scopes. This heading would suggest a test with e.g. a nested function.

Suggested change
## `return` is terminal in nested scopes
## `return` is terminal in nested conditionals

reveal_type(x) # revealed: Literal["continue"]
continue
reveal_type(x) # revealed: Literal["loop"]
reveal_type(x) # revealed: Literal["before", "loop"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious in which combination of cond and i it'd be Literal["loop"] at here.


def g(cond: bool, i: int):
x = "before"
while i < 5:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to switch to for loop instead or increase i's value inside the loop, otherwise it's infinite loop when i<5.

* main:
  [red-knot] MDTests: Do not depend on precise public-symbol type inference (#15691)
  [red-knot] Make `infer.rs` unit tests independent of public symbol inference (#15690)
  Tidy knot CLI tests (#15685)
  [red-knot] Port comprehension tests to Markdown (#15688)
  Create Unknown rule diagnostics with a source range (#15648)
  [red-knot] Port 'deferred annotations' unit tests to Markdown (#15686)
  [red-knot] Support custom typeshed Markdown tests (#15683)
  Don't run the linter ecosystem check on PRs that only touch red-knot crates (#15687)
  Add `rules` table to configuration (#15645)
  [red-knot] Make `Diagnostic::file` optional (#15640)
  [red-knot] Add test for nested attribute access (#15684)
  [red-knot] Anchor relative paths in configurations (#15634)
  [`pyupgrade`] Handle multiple base classes for PEP 695 generics (`UP046`) (#15659)
  [`pyflakes`] Treat arguments passed to the `default=` parameter of `TypeVar` as type expressions (`F821`) (#15679)
  Upgrade zizmor to the latest version in CI (#15649)
  [`pyupgrade`] Add rules to use PEP 695 generics in classes and functions (`UP046`, `UP047`) (#15565)
  [red-knot] Ensure a gradual type can always be assigned to itself (#15675)
@dylwil3
Copy link
Collaborator

dylwil3 commented Jan 23, 2025

I don't think you need to change anything for this PR, but just so it's on your radar: try/finally knows how to ruin any clean story. For example, the following test fails on this branch:

def f():
    x = 1
    while True:
        try:
            break
        finally:
            x = 2
    reveal_type(x)  # revealed: Literal[2] 

(it gives a revealed type of Literal[1] instead)

@carljm
Copy link
Contributor

carljm commented Jan 23, 2025

Yeah, we can handle finally in this PR or as a separate follow up PR, but it probably will need some special handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants