From dfb71a65a5d160d9b74dd197000f642966e188a3 Mon Sep 17 00:00:00 2001 From: David Peter Date: Wed, 9 Apr 2025 11:23:22 +0200 Subject: [PATCH] [red-knot] Add new 'unreachable code' test case --- .../resources/mdtest/unreachable.md | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/crates/red_knot_python_semantic/resources/mdtest/unreachable.md b/crates/red_knot_python_semantic/resources/mdtest/unreachable.md index fce99d51ce87f..3f56372d97eef 100644 --- a/crates/red_knot_python_semantic/resources/mdtest/unreachable.md +++ b/crates/red_knot_python_semantic/resources/mdtest/unreachable.md @@ -432,6 +432,44 @@ if sys.version_info >= (3, 11): 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 Even though the expressions in the snippet below are unreachable, we still emit diagnostics for