[red-knot] Silence errors in unreachable type annotations / class bases (#17342)

## Summary

For silencing `invalid-type-form` diagnostics in unreachable code, we
use the same approach that we use before and check the reachability that
we already record.

For silencing `invalid-bases`, we simply check if the type of the base
is `Never`. If so, we silence the diagnostic with the argument that the
class construction would never happen.

## Test Plan

Updated Markdown tests.
This commit is contained in:
David Peter
2025-04-10 22:47:47 +02:00
committed by GitHub
parent 8b2727cf67
commit 1a3b73720c
3 changed files with 83 additions and 51 deletions

View File

@@ -447,15 +447,16 @@ We should not show any diagnostics in type annotations inside unreachable sectio
```py
def _():
class C: ...
class C:
class Inner: ...
return
# TODO
# error: [invalid-type-form] "Variable of type `Never` is not allowed in a type expression"
c: C = C()
c1: C = C()
c2: C.Inner = C.Inner()
c3: tuple[C, C] = (C(), C())
c4: tuple[C.Inner, C.Inner] = (C.Inner(), C.Inner())
# TODO
# error: [invalid-base] "Invalid class base with type `Never` (all bases must be a class, `Any`, `Unknown` or `Todo`)"
class Sub(C): ...
```