KEMBAR78
`arg-type` error duck typing a `TypedDict` with `NotRequired` field · Issue #18162 · python/mypy · GitHub
Skip to content

arg-type error duck typing a TypedDict with NotRequired field #18162

@ZeeD

Description

@ZeeD

Bug Report

mypy play url

it seems that mypy doesn't "match" a NotRequired field with a normal one when duck typing 2 different typed dict:

Let's say that I have a function foo that expect a Small TypedDict as parameter

class Small(TypedDict):
    a: NotRequired[str]

def foo(small: Small) -> None: ...

In this Small TypedDict the field a is marked as NotRequired.

Elsewere I have another Big TypedDict with 2 fields a, and b, both required.

class Big(TypedDict):
    a: str
    b: str

but if I try to pass a Big dict instance to my foo function, mypy mark it as an error:
main.py:21: error: Argument 1 to "foo" has incompatible type "Big"; expected "Small" [arg-type]

If I change the type of Big.a to a NotRequired[str], mypy is happy, however mypy should allow the original definition, as a "normal" field should be treated as a stricter, "smaller" version of a not required one

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions