[red-knot] Emit error if int/float/complex/bytes/boolean literals appear in type expressions outside typing.Literal[] (#16765)

## Summary
Fixes https://github.com/astral-sh/ruff/issues/16532

## Test Plan

New mdtest assertions added

---------

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Matthew Mckee
2025-03-17 11:56:16 +00:00
committed by GitHub
parent 93ca4a96e0
commit 24707777af
5 changed files with 72 additions and 8 deletions

View File

@@ -43,3 +43,21 @@ def _(
reveal_type(q) # revealed: Unknown
reveal_type(r) # revealed: Unknown
```
## Invalid AST nodes
```py
def _(
a: 1, # error: [invalid-type-form] "Int literals are not allowed in this context in a type expression"
b: 2.3, # error: [invalid-type-form] "Float literals are not allowed in type expressions"
c: 4j, # error: [invalid-type-form] "Complex literals are not allowed in type expressions"
d: True, # error: [invalid-type-form] "Boolean literals are not allowed in this context in a type expression"
# error: [invalid-type-form] "Bytes literals are not allowed in this context in a type expression"
e: int | b"foo",
):
reveal_type(a) # revealed: Unknown
reveal_type(b) # revealed: Unknown
reveal_type(c) # revealed: Unknown
reveal_type(d) # revealed: Unknown
reveal_type(e) # revealed: int | Unknown
```