-
Notifications
You must be signed in to change notification settings - Fork 222
Closed
astral-sh/ruff
#22849Description
Summary
Summary
After narrowing a TypedDict | None type via if not x: raise, ty no
longer detects invalid key access. The invalid-key rule works correctly when the variable is directly typed as the TypedDict.
Reproduction
from typing import TypedDict
class Person(TypedDict):
name: str
age: int
def get_person() -> Person | None:
return None
def test() -> None:
p = get_person()
if not p:
raise ValueError("No person")
# After narrowing, p should be Person - but invalid-key not triggered
print(p["nonexistent"]) # No error (should be invalid-key)
def test_direct(p: Person) -> None:
# Direct parameter works correctly
print(p["nonexistent"]) # Error: invalid-key ✓
Expected Behavior
Both test() and test_direct() should emit invalid-key for
p["nonexistent"].
Actual Behavior
Only test_direct() emits the error. After narrowing from Person | None,
the TypedDict key information is lost.
Related
Possibly related to TypedDict tracking issue #154
Version
0.0.13
Reactions are currently unavailable