from typing import Union, TypedDict
class Foo(TypedDict):
x: int
class Foo2(Foo):
y: int
class Bar(TypedDict):
y: str
def f(foobar: Union[Foo, Bar]):
if 'y' in foobar:
print(foobar['y'].lower())
x: Foo2 = {'x': 1, 'y': 2}
f(x)
This will fail at runtime, but typechecks just fine. That's because Foo is not guaranteed to not have a y key, as it can be a subtype of Foo. Is this as designed?
This will fail at runtime, but typechecks just fine. That's because
Foois not guaranteed to not have aykey, as it can be a subtype ofFoo. Is this as designed?