mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 22:01:18 +00:00
[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:
parent
5e29278aa2
commit
215a1c55d4
3 changed files with 34 additions and 8 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue