Skip to content

ReadOnly field can be added when subclassing a TypedDict with mutable extra_items #11037

@ZhaJiMan

Description

@ZhaJiMan
from typing_extensions import TypedDict, NotRequired, ReadOnly


class A(TypedDict, extra_items=float):
    x: float
    y: float


class B(A):
    z: ReadOnly[NotRequired[float]]


def f(a: A) -> None:
    a["z"] = 0


b: B = {"x": 1, "y": 2, "z": 3}
f(b)

pyright playground version

Class A has mutable extra_items, while its subclass B adds a new read-only field. However, this field can be mutated by functions that accept type A, which violates the ReadOnly constraint. Why do pyright allow this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions