[ty] Detect illegal non-enum attribute accesses in Literal annotation (#19477)

## Summary

Detect illegal attribute accesses in `Literal[X.Y]` annotations if `X`
is not an enum class.

## Test Plan

New Markdown test
This commit is contained in:
David Peter 2025-07-22 11:42:03 +02:00 committed by GitHub
parent 5e29278aa2
commit 215a1c55d4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 8 deletions

View file

@ -25,6 +25,9 @@ class Color(Enum):
b1: Literal[Color.RED]
MissingT = Enum("MissingT", {"MISSING": "MISSING"})
b2: Literal[MissingT.MISSING]
def f():
reveal_type(mode) # revealed: Literal["w", "r"]
reveal_type(a1) # revealed: Literal[26]
@ -51,6 +54,12 @@ invalid4: Literal[
hello, # error: [invalid-type-form]
(1, 2, 3), # error: [invalid-type-form]
]
class NotAnEnum:
x: int = 1
# error: [invalid-type-form]
invalid5: Literal[NotAnEnum.x]
```
## Shortening unions of literals