Skip to content

ReadOnly TypedDict items should be treated as covariant #17901

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions