using Optional in TypedDict provides not consistent behaviour with PEP 589
p-malecki opened this issue · comments
Paweł Małecki commented
Example Code
from typing_extensions import TypedDict
from typing import Optional
class A(TypedDict):
x: Optional[int]
class B(TypedDict):
x: int
def f(a: A) -> None:
a['x'] = None
b: B = {'x': 0}
f(b) # Type check error: 'B' not compatible with 'A'
b['x'] + 1 # Runtime error: None + 1
Expected Behavior
error: incompatible type argument in function f
Actual Behavior
Success: no errors found
Example works as expected in mypy.
Rebecca Chen commented
Agreed, pytype should flag this. We (the pytype devs) discussed this briefly, and we probably want to match mutable and immutable container types differently.