Skip to content

Commit 9b81c82

Browse files
committed
use declared variable types as bidirectional type context
1 parent 66885e4 commit 9b81c82

File tree

4 files changed

+119
-79
lines changed

4 files changed

+119
-79
lines changed

crates/ty_python_semantic/resources/mdtest/assignment/annotations.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ reveal_type(q) # revealed: dict[int | str, int]
144144

145145
r: dict[int | str, int | str] = {1: 1, 2: 2, 3: 3}
146146
reveal_type(r) # revealed: dict[int | str, int | str]
147+
148+
s: dict[int | str, int | str]
149+
s = {1: 1, 2: 2, 3: 3}
150+
reveal_type(s) # revealed: dict[int | str, int | str]
151+
(s := {1: 1, 2: 2, 3: 3})
152+
reveal_type(s) # revealed: dict[int | str, int | str]
147153
```
148154

149155
## Incorrect collection literal assignments are complained aobut
@@ -229,6 +235,12 @@ reveal_type(q) # revealed: list[int]
229235

230236
r: list[Literal[1, 2, 3, 4]] = [1, 2]
231237
reveal_type(r) # revealed: list[Literal[1, 2, 3, 4]]
238+
239+
s: list[Literal[1]]
240+
s = [1]
241+
reveal_type(s) # revealed: list[Literal[1]]
242+
(s := [1])
243+
reveal_type(s) # revealed: list[Literal[1]]
232244
```
233245

234246
## PEP-604 annotations are supported

crates/ty_python_semantic/resources/mdtest/narrow/conditionals/nested.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,13 @@ no longer valid in the inner lazy scope.
310310
def f(l: list[str | None]):
311311
if l[0] is not None:
312312
def _():
313-
reveal_type(l[0]) # revealed: str | None | Unknown
313+
reveal_type(l[0]) # revealed: str | None
314314
l = [None]
315315

316316
def f(l: list[str | None]):
317317
l[0] = "a"
318318
def _():
319-
reveal_type(l[0]) # revealed: str | None | Unknown
319+
reveal_type(l[0]) # revealed: str | None
320320
l = [None]
321321

322322
def f(l: list[str | None]):

crates/ty_python_semantic/resources/mdtest/typed_dict.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,12 @@ Person({"name": "Alice"})
194194

195195
# error: [missing-typed-dict-key] "Missing required key 'age' in TypedDict `Person` constructor"
196196
accepts_person({"name": "Alice"})
197+
197198
# TODO: this should be an error, similar to the above
198199
house.owner = {"name": "Alice"}
200+
199201
a_person: Person
200-
# TODO: this should be an error, similar to the above
202+
# error: [missing-typed-dict-key] "Missing required key 'age' in TypedDict `Person` constructor"
201203
a_person = {"name": "Alice"}
202204
```
203205

@@ -215,9 +217,12 @@ Person({"name": None, "age": 30})
215217
accepts_person({"name": None, "age": 30})
216218
# TODO: this should be an error, similar to the above
217219
house.owner = {"name": None, "age": 30}
220+
218221
a_person: Person
219-
# TODO: this should be an error, similar to the above
222+
# error: [invalid-argument-type] "Invalid argument to key "name" with declared type `str` on TypedDict `Person`: value of type `None`"
220223
a_person = {"name": None, "age": 30}
224+
# error: [invalid-argument-type] "Invalid argument to key "name" with declared type `str` on TypedDict `Person`: value of type `None`"
225+
(a_person := {"name": None, "age": 30})
221226
```
222227

223228
All of these have an extra field that is not defined in the `TypedDict`:
@@ -234,9 +239,12 @@ Person({"name": "Alice", "age": 30, "extra": True})
234239
accepts_person({"name": "Alice", "age": 30, "extra": True})
235240
# TODO: this should be an error
236241
house.owner = {"name": "Alice", "age": 30, "extra": True}
237-
# TODO: this should be an error
242+
238243
a_person: Person
244+
# error: [invalid-key] "Invalid key access on TypedDict `Person`: Unknown key "extra""
239245
a_person = {"name": "Alice", "age": 30, "extra": True}
246+
# error: [invalid-key] "Invalid key access on TypedDict `Person`: Unknown key "extra""
247+
(a_person := {"name": "Alice", "age": 30, "extra": True})
240248
```
241249

242250
## Type ignore compatibility issues

0 commit comments

Comments
 (0)