google / pytype

A static type analyzer for Python code
https://google.github.io/pytype
Other
4.76k stars 277 forks source link

using Optional in TypedDict provides not consistent behaviour with PEP 589 #1549

Open p-malecki opened 10 months ago

p-malecki commented 10 months ago

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.

rchen152 commented 10 months ago

Agreed, pytype should flag this. We (the pytype devs) discussed this briefly, and we probably want to match mutable and immutable container types differently.