Skip to content

Commit 9f3fa9f

Browse files
committed
use declared variable types as bidirectional type context
1 parent 1ade4f2 commit 9f3fa9f

File tree

4 files changed

+102
-63
lines changed

4 files changed

+102
-63
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
## Optional collection literal annotations are understood
@@ -296,6 +302,12 @@ reveal_type(q) # revealed: list[int]
296302

297303
r: list[Literal[1, 2, 3, 4]] = [1, 2]
298304
reveal_type(r) # revealed: list[Literal[1, 2, 3, 4]]
305+
306+
s: list[Literal[1]]
307+
s = [1]
308+
reveal_type(s) # revealed: list[Literal[1]]
309+
(s := [1])
310+
reveal_type(s) # revealed: list[Literal[1]]
299311
```
300312

301313
## 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
@@ -233,10 +233,12 @@ Person({"name": "Alice"})
233233

234234
# error: [missing-typed-dict-key] "Missing required key 'age' in TypedDict `Person` constructor"
235235
accepts_person({"name": "Alice"})
236+
236237
# TODO: this should be an error, similar to the above
237238
house.owner = {"name": "Alice"}
239+
238240
a_person: Person
239-
# TODO: this should be an error, similar to the above
241+
# error: [missing-typed-dict-key] "Missing required key 'age' in TypedDict `Person` constructor"
240242
a_person = {"name": "Alice"}
241243
```
242244

@@ -254,9 +256,12 @@ Person({"name": None, "age": 30})
254256
accepts_person({"name": None, "age": 30})
255257
# TODO: this should be an error, similar to the above
256258
house.owner = {"name": None, "age": 30}
259+
257260
a_person: Person
258-
# TODO: this should be an error, similar to the above
261+
# error: [invalid-argument-type] "Invalid argument to key "name" with declared type `str` on TypedDict `Person`: value of type `None`"
259262
a_person = {"name": None, "age": 30}
263+
# error: [invalid-argument-type] "Invalid argument to key "name" with declared type `str` on TypedDict `Person`: value of type `None`"
264+
(a_person := {"name": None, "age": 30})
260265
```
261266

262267
All of these have an extra field that is not defined in the `TypedDict`:
@@ -273,9 +278,12 @@ Person({"name": "Alice", "age": 30, "extra": True})
273278
accepts_person({"name": "Alice", "age": 30, "extra": True})
274279
# TODO: this should be an error
275280
house.owner = {"name": "Alice", "age": 30, "extra": True}
276-
# TODO: this should be an error
281+
277282
a_person: Person
283+
# error: [invalid-key] "Invalid key access on TypedDict `Person`: Unknown key "extra""
278284
a_person = {"name": "Alice", "age": 30, "extra": True}
285+
# error: [invalid-key] "Invalid key access on TypedDict `Person`: Unknown key "extra""
286+
(a_person := {"name": "Alice", "age": 30, "extra": True})
279287
```
280288

281289
## Type ignore compatibility issues

0 commit comments

Comments
 (0)