[red-knot] Add new 'unreachable code' test case (#17306)

## Summary

This is a new test case that I don't know how to handle yet. It leads to
many false positives in `rich/tests/test_win32_console.py`, which does
something like:

```py
if sys.platform == "win32":
    from windows_only_module import some_symbol

    some_other_symbol = 1

    def some_test_case():
        use(some_symbol)  # Red Knot: unresolved-reference
        use(some_other_symbol)  # Red Knot: unresolved-reference
```

Also adds a test for using unreachable symbols in type annotations or as
class bases.
This commit is contained in:
David Peter 2025-04-09 11:45:42 +02:00 committed by GitHub
parent 9a5a86769b
commit 00e00b9ad6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -432,6 +432,44 @@ if sys.version_info >= (3, 11):
import wsgiref.types import wsgiref.types
``` ```
### Nested scopes
When we have nested scopes inside the unreachable section, we should not emit diagnostics either:
```py
if False:
x = 1
def f():
# TODO
# error: [unresolved-reference]
print(x)
class C:
def __init__(self):
# TODO
# error: [unresolved-reference]
print(x)
```
### Use of unreachable symbols in type annotations, or as class bases
We should not show any diagnostics in type annotations inside unreachable sections.
```py
def _():
class C: ...
return
# TODO
# error: [invalid-type-form] "Variable of type `Never` is not allowed in a type expression"
c: C = C()
# TODO
# error: [invalid-base] "Invalid class base with type `Never` (all bases must be a class, `Any`, `Unknown` or `Todo`)"
class Sub(C): ...
```
### Emit diagnostics for definitely wrong code ### Emit diagnostics for definitely wrong code
Even though the expressions in the snippet below are unreachable, we still emit diagnostics for Even though the expressions in the snippet below are unreachable, we still emit diagnostics for