← Back to index

typeddicts_readonly_update.py

True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 0
FP: 0
FN: 1
Optional: 0 / 0
1"""
2Tests inheritance rules involving the update method for TypedDicts
3that contain read-only items.
4"""
5
6# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html#read-only-items
7
8from typing import Never, NotRequired, TypedDict
9from typing_extensions import ReadOnly
11# > In addition to existing type checking rules, type checkers should error if
12# > a TypedDict with a read-only item is updated with another TypedDict that
13# > declares that key.
16class A(TypedDict):
17 x: ReadOnly[int]
18 y: int
21a1: A = {"x": 1, "y": 2}
22a2: A = {"x": 3, "y": 4}
23a1.update(a2) # E
Expected a ty diagnostic for this line
25# > Unless the declared value is of bottom type (:data:`~typing.Never`).
28class B(TypedDict):
29 x: NotRequired[Never]
30 y: ReadOnly[int]
33def update_a(a: A, b: B) -> None:
34 a.update(b) # OK