google / pytype

A static type analyzer for Python code

Home Page:https://google.github.io/pytype

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

using Optional in TypedDict provides not consistent behaviour with PEP 589

p-malecki opened this issue · comments

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.

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