2Tests type consistency rules for TypedDict with ReadOnly items.
5# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html#read-only-items
7from typing import Any, Collection, NotRequired, Required, TypedDict
8from typing_extensions import ReadOnly
10# > A TypedDict type ``A`` is consistent with TypedDict ``B`` if ``A`` is
11# > structurally compatible with ``B``. This is true if and only if all of
12# > the following are satisfied:
14# > For each item in ``B``, ``A`` has the corresponding key, unless the item
15# > in ``B`` is read-only, not required, and of top value type
16# > (``ReadOnly[NotRequired[object]]``).
30 y: ReadOnly[NotRequired[str]]
33def func1(a: A1, b: B1, c: C1):
[invalid-assignment] Object of type `A1` is not assignable to `B1`
[invalid-assignment] Object of type `C1` is not assignable to `B1`
[invalid-assignment] Object of type `A1` is not assignable to `C1`
44# > For each item in ``B``, if ``A`` has the corresponding key, the
45# > corresponding value type in ``A`` is consistent with the value type in ``B``.
47# (This is the same rule that applies to TypedDicts without read-only items,
48# so we will skip this redundant test.)
50# > For each non-read-only item in ``B``, its value type is consistent with
51# > the corresponding value type in ``A``.
53# (This is the same rule that applies to TypedDicts without read-only items,
54# so we will skip this redundant test.)
56# > For each required key in ``B``, the corresponding key is required in ``A``.
58# (This is the same rule that applies to TypedDicts without read-only items,
59# so we will skip this redundant test.)
61# > For each non-required key in ``B``, if the item is not read-only in ``B``,
62# > the corresponding key is not required in ``A``.
66 x: NotRequired[ReadOnly[str]]
77def func2(a: A2, b: B2, c: C2):
[invalid-assignment] Object of type `A2` is not assignable to `B2`
[invalid-assignment] Object of type `C2` is not assignable to `B2`
[invalid-assignment] Object of type `A2` is not assignable to `C2`
[invalid-assignment] Object of type `B2` is not assignable to `C2`