Skip to content

Commit ac2ed3e

Browse files
committed
avoid false positives when inheriting from functional TypedDicts
1 parent 2949f76 commit ac2ed3e

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

crates/ty_python_semantic/resources/mdtest/typed_dict.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,20 @@ emp_invalid1 = Employee(department="HR")
11001100
emp_invalid2 = Employee(id=3)
11011101
```
11021102

1103+
Fields from functional `TypedDict`s are not currently inherited:
1104+
1105+
```py
1106+
from typing import TypedDict
1107+
1108+
class X(TypedDict("Y", { "y": int })):
1109+
x: int
1110+
1111+
x: X = {"x": 0}
1112+
1113+
# error: [invalid-key] "Invalid key access on TypedDict `X`: Unknown key "y""
1114+
x: X = {"y": 0, "x": 0}
1115+
```
1116+
11031117
## Generic `TypedDict`
11041118

11051119
`TypedDict`s can also be generic.

crates/ty_python_semantic/src/types/class_base.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,12 @@ impl<'db> ClassBase<'db> {
169169
KnownInstanceType::SubscriptedProtocol(_) => Some(Self::Protocol),
170170
KnownInstanceType::TypeAliasType(_)
171171
| KnownInstanceType::TypeVar(_)
172-
| KnownInstanceType::TypedDictType(_)
173172
| KnownInstanceType::TypedDictSchema(_)
174173
| KnownInstanceType::Deprecated(_)
175174
| KnownInstanceType::Field(_)
176175
| KnownInstanceType::ConstraintSet(_) => None,
176+
// TODO: Inherit the fields of synthesized `TypedDict`s.
177+
KnownInstanceType::TypedDictType(_) => Some(Self::TypedDict),
177178
},
178179

179180
Type::SpecialForm(special_form) => match special_form {

0 commit comments

Comments
 (0)