Indexing TypedDict with a non-literal variable (original) (raw)
jorenham (Joren Hammudoglu) April 25, 2025, 4:15pm 8
I don’t think that option 1 would resolve this false positive, because key
is already inferred as a Literal["b"]
, and o[key]
is inferred as Never
(i.e. assignable to anything):
class A(TypedDict):
a: int
class B(A):
b: str
def fn(o: A) -> int:
key = "b"
if key in o:
reveal_type(key) # Type of "key" is "Literal['b']"
reveal_type(o[key]) # Type of "o[key]" is "Never"
return o[key]
return o["a"]