Merged
Conversation
Contributor
|
751b05e to
05a808b
Compare
sharkdp
commented
Apr 4, 2025
| /// use(y) | ||
| /// ``` | ||
| /// Depending on the value of `test`, the `y = 1`, `y = 2`, or both bindings may be visible. | ||
| /// The use of `x` is recorded with a reachability constraint of `[test]`. |
Contributor
Author
There was a problem hiding this comment.
I'm completely fine with keeping this more concise.
sharkdp
commented
Apr 4, 2025
Comment on lines
+392
to
+433
| ### Attributes | ||
|
|
||
| When attribute expressions appear in unreachable code, we should not emit `unresolved-attribute` | ||
| diagnostics: | ||
|
|
||
| ```py | ||
| import sys | ||
| import builtins | ||
|
|
||
| if sys.version_info >= (3, 11): | ||
| # TODO | ||
| # error: [unresolved-attribute] | ||
| builtins.ExceptionGroup | ||
| ``` | ||
|
|
||
| ### Imports | ||
|
|
||
| When import statements appear in unreachable code, we should not emit `unresolved-import` | ||
| diagnostics: | ||
|
|
||
| ```py | ||
| import sys | ||
|
|
||
| if sys.version_info >= (3, 11): | ||
| # TODO | ||
| # error: [unresolved-import] | ||
| from builtins import ExceptionGroup | ||
|
|
||
| # TODO | ||
| # error: [unresolved-import] | ||
| import builtins.ExceptionGroup | ||
|
|
||
| # See https://docs.python.org/3/whatsnew/3.11.html#new-modules | ||
|
|
||
| # TODO | ||
| # error: [unresolved-import] | ||
| import tomllib | ||
|
|
||
| # TODO | ||
| # error: [unresolved-import] | ||
| import wsgiref.types | ||
| ``` |
Contributor
Author
There was a problem hiding this comment.
I will leave attributes and imports for a follow up. I want to get some feedback on this first.
05a808b to
7ae5d31
Compare
7ae5d31 to
100d88b
Compare
sharkdp
commented
Apr 4, 2025
|
|
||
| In the example below, since we use `x` in the `inner` function, we use the "public" type of `x`, | ||
| which currently refers to the end-of-scope type of `x`. Since the end of the `outer` scope is | ||
| unreachable, we treat `x` as if it was not defined. This behavior can certainly be improved. |
Contributor
Author
There was a problem hiding this comment.
Nothing changed in this test, but this explanation was outdated. And the return x # … below as confusing.
sharkdp
commented
Apr 4, 2025
|
|
||
| def inner(): | ||
| return x # Name `x` used when not defined | ||
| reveal_type(x) # revealed: Unknown |
Contributor
Author
There was a problem hiding this comment.
The fact that we reveal Unknown here is tracked in https://github.com/astral-sh/ruff/issues/15777
sharkdp
commented
Apr 4, 2025
| FEATURE_X_ACTIVATED = False | ||
| from typing import Literal | ||
|
|
||
| FEATURE_X_ACTIVATED: Literal[False] = False |
Contributor
Author
There was a problem hiding this comment.
We don't support Final yet, so I needed to declare this as Literal[False]. Otherwise, the public type (which we use below) would be Unknown | Literal[False].
sharkdp
commented
Apr 4, 2025
100d88b to
03a2dcf
Compare
dcreager
added a commit
that referenced
this pull request
Apr 9, 2025
* origin/main: [red-knot] Default `python-platform` to current platform (#17183) [red-knot] Add new 'unreachable code' test case (#17306) [red-knot] mypy_primer: Run on `async-utils` (#17303) [red-knot] Add custom `__setattr__` support (#16748) [red-knot] Add `__init__` arguments check when doing `try_call` on a class literal (#16512) [`flake8-pie`] Avoid false positive for multiple assignment with `auto()` (`PIE796`) (#17274) [syntax-errors] Async comprehension in sync comprehension (#17177) [`airflow`] Expand module path check to individual symbols (`AIR302`) (#17278) [syntax-errors] Check annotations in annotated assignments (#17283) [syntax-errors] Extend annotation checks to `await` (#17282) [red-knot] Add support for `assert_never` (#17287) [`flake8-pytest-style`] Avoid false positive for legacy form of `pytest.raises` (`PT011`) (#17231) [red-knot] Do not show types for literal expressions on hover (#17290) [red-knot] Fix dead-code clippy warning (#17291) [red-knot] Reachability analysis (#17199) [red-knot] Don't use latency-sensitive for handlers (#17227)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR proposes a new approach to silencing
unresolved-referencediagnostics by keeping track of the reachability of each use of a symbol. The changes merged in #17169 are still needed for the "Use of variable in nested function" test case, but that could also be solved in another way eventually (see astral-sh/ty#210). We can use the same technique to silenceunresolved-importandunresolved-attributefalse-positives, but I think this could be merged in isolation.Test Plan
New Markdown tests, ecosystem tests