Concretely, should the following be allowed?
from typing import TypedDict, ReadOnly
class A(TypedDict):
x: str
class B(TypedDict):
x: ReadOnly[str]
def f(b: B) -> None:
a: A = b
The current assignability rules allow this, but the conformance tests here and here mark it as an error.
I think the conformance tests are correct (the redeclaration would make it possible to write to a read-only field), so I’d like to make the following addition (in bold) to the assignability rules:
- For each item in
A
,B
has the corresponding key, unless the item inA
is read-only, not required, and of top value type (ReadOnly[NotRequired[object]]
).- For each item in
A
, ifB
has the corresponding key, the corresponding value type inB
is assignable to the value type inA
.- For each non-read-only item in
A
, its value type is assignable to the corresponding value type inB
, and the corresponding key is not read-only inB
.- For each required key in
A
, the corresponding key is required inB
.- For each non-required key in
A
, if the item is not read-only inA
, the corresponding key is not required inB
.
Feedback welcome =)