← 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"""
2Tests the use of unpacked TypedDicts with read-only items when used to annotate
3a **kwargs parameter.
4"""
5
6# Specification: https://typing.readthedocs.io/en/latest/spec/typeddict.html#read-only-items
7
8from typing import Protocol, TypedDict, Unpack
9from typing_extensions import ReadOnly
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.
18class Args(TypedDict):
19 key1: int
20 key2: str
23class ReadOnlyArgs(TypedDict):
24 key1: ReadOnly[int]
25 key2: ReadOnly[str]
28class Function(Protocol):
29 def __call__(self, **kwargs: Unpack[Args]) -> None: ...
32def impl(**kwargs: Unpack[ReadOnlyArgs]) -> None:
33 kwargs["key1"] = 3 # E
Expected a ty diagnostic for this line
36fn: Function = impl # OK