← 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
"""
2
Tests
inheritance
rules
involving
the
update
method
for
TypedDicts
3
that
contain
read
-
only
items
.
4
"""
5
6
# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html#read-only-items
7
8
from
typing
import
Never
,
NotRequired
,
TypedDict
9
from
typing_extensions
import
ReadOnly
10
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.
14
15
16
class
A
(
TypedDict
):
17
x
:
ReadOnly
[
int
]
18
y
:
int
19
20
21
a1
:
A
=
{
"x"
:
1
,
"y"
:
2
}
22
a2
:
A
=
{
"x"
:
3
,
"y"
:
4
}
23
a1
.
update
(
a2
)
# E
Expected a ty diagnostic for this line
24
25
# > Unless the declared value is of bottom type (:data:`~typing.Never`).
26
27
28
class
B
(
TypedDict
):
29
x
:
NotRequired
[
Never
]
30
y
:
ReadOnly
[
int
]
31
32
33
def
update_a
(
a
:
A
,
b
:
B
)
->
None
:
34
a
.
update
(
b
)
# OK