Skip to content

ReadOnly TypedDict items should be treated as covariant #17901

Closed
@JukkaL

Description

@JukkaL

ReadOnly was implemented in #17644. I think the implementation is incomplete -- this generates a false positive:

from typing import TypedDict
from typing_extensions import ReadOnly

class A(TypedDict):
    name: ReadOnly[str | None]

class B(TypedDict):
    name: str

def f(a: A) -> None: ...

b: B = {"name": "x"}
f(b)  # Incompatible type "B"; expected "A"

Extract from PEP 705:

Read-only items behave covariantly, as they cannot be mutated.

cc @sobolevn

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions