← Back to index
typeddicts_readonly_kwargs.py
True Positive
False Positive
False Negative
Optional (detected)
Warning or Info
TP: 0
FP: 0
FN: 1
Optional: 0 / 0
1
"""
2
Tests
the
use
of
unpacked
TypedDicts
with
read
-
only
items
when
used
to
annotate
3
a
**
kwargs
parameter
.
4
"""
5
6
# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html#read-only-items
7
8
from
typing
import
Protocol
,
TypedDict
,
Unpack
9
from
typing_extensions
import
ReadOnly
10
11
# > :pep:`692` introduced ``Unpack`` to annotate ``**kwargs`` with a
12
# > ``TypedDict``. Marking one or more of the items of a ``TypedDict`` used
13
# > in this way as read-only will have no effect on the type signature of
14
# > the method. However, it *will* prevent the item from being modified in
15
# > the body of the function.
16
17
18
class
Args
(
TypedDict
):
19
key1
:
int
20
key2
:
str
21
22
23
class
ReadOnlyArgs
(
TypedDict
):
24
key1
:
ReadOnly
[
int
]
25
key2
:
ReadOnly
[
str
]
26
27
28
class
Function
(
Protocol
):
29
def
__call__
(
self
,
**
kwargs
:
Unpack
[
Args
])
->
None
:
...
30
31
32
def
impl
(
**
kwargs
:
Unpack
[
ReadOnlyArgs
])
->
None
:
33
kwargs
[
"key1"
]
=
3
# E
Expected a ty diagnostic for this line
34
35
36
fn
:
Function
=
impl
# OK