Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -482,17 +482,14 @@ class TD2(TypedDict):
x: str

def f(self, dt: dict[str, Any], key: str):
# TODO: This should not error once typed dict assignability is implemented.
# error: [invalid-assignment]
x1: TD = dt.get(key, {})
reveal_type(x1) # revealed: TD
reveal_type(x1) # revealed: Any

x2: TD = dt.get(key, {"x": 0})
reveal_type(x2) # revealed: Any

x3: TD | None = dt.get(key, {})
# TODO: This should reveal `Any` once typed dict assignability is implemented.
reveal_type(x3) # revealed: Any | None
reveal_type(x3) # revealed: Any

x4: TD | None = dt.get(key, {"x": 0})
reveal_type(x4) # revealed: Any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ import dict_a
import dict_b

def _(b_person: dict_b.Person):
# TODO should be error: [invalid-assignment] "Object of type `dict_b.Person` is not assignable to `dict_a.Person`"
# error: [invalid-assignment] "Object of type `dict_b.Person` is not assignable to `dict_a.Person`"
person_var: dict_a.Person = b_person
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ source: crates/ty_test/src/lib.rs
expression: snapshot
---
---
mdtest name: assignment_diagnostics.md - Subscript assignment diagnostics - Unknown key for all elemens of a union
mdtest name: assignment_diagnostics.md - Subscript assignment diagnostics - Unknown key for all elements of a union
mdtest path: crates/ty_python_semantic/resources/mdtest/subscript/assignment_diagnostics.md
---

Expand All @@ -16,26 +16,27 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/subscript/assignment_dia
2 |
3 | class Person(TypedDict):
4 | name: str
5 |
6 | class Animal(TypedDict):
7 | name: str
8 | legs: int
9 |
10 | def _(being: Person | Animal) -> None:
11 | # error: [invalid-key]
5 | phone_number: str
6 |
7 | class Animal(TypedDict):
8 | name: str
9 | legs: int
10 |
11 | def _(being: Person | Animal) -> None:
12 | # error: [invalid-key]
13 | being["surname"] = "unknown"
13 | # error: [invalid-key]
14 | being["surname"] = "unknown"
```

# Diagnostics

```
error[invalid-key]: Unknown key "surname" for TypedDict `Person`
--> src/mdtest_snippet.py:13:5
--> src/mdtest_snippet.py:14:5
|
11 | # error: [invalid-key]
12 | # error: [invalid-key]
13 | being["surname"] = "unknown"
13 | # error: [invalid-key]
14 | being["surname"] = "unknown"
| ----- ^^^^^^^^^ Did you mean "name"?
| |
| TypedDict `Person` in union type `Person | Animal`
Expand All @@ -46,11 +47,11 @@ info: rule `invalid-key` is enabled by default

```
error[invalid-key]: Unknown key "surname" for TypedDict `Animal`
--> src/mdtest_snippet.py:13:5
--> src/mdtest_snippet.py:14:5
|
11 | # error: [invalid-key]
12 | # error: [invalid-key]
13 | being["surname"] = "unknown"
13 | # error: [invalid-key]
14 | being["surname"] = "unknown"
| ----- ^^^^^^^^^ Did you mean "name"?
| |
| TypedDict `Animal` in union type `Person | Animal`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/subscript/assignment_dia
2 |
3 | class Person(TypedDict):
4 | name: str
5 |
6 | class Animal(TypedDict):
7 | name: str
8 | legs: int
9 |
10 | def _(being: Person | Animal) -> None:
11 | being["legs"] = 4 # error: [invalid-key]
5 | phone_number: str
6 |
7 | class Animal(TypedDict):
8 | name: str
9 | legs: int
10 |
11 | def _(being: Person | Animal) -> None:
12 | being["legs"] = 4 # error: [invalid-key]
```

# Diagnostics

```
error[invalid-key]: Unknown key "legs" for TypedDict `Person`
--> src/mdtest_snippet.py:11:5
--> src/mdtest_snippet.py:12:5
|
10 | def _(being: Person | Animal) -> None:
11 | being["legs"] = 4 # error: [invalid-key]
11 | def _(being: Person | Animal) -> None:
12 | being["legs"] = 4 # error: [invalid-key]
| ----- ^^^^^^ Unknown key "legs"
| |
| TypedDict `Person` in union type `Person | Animal`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ from typing import TypedDict

class Person(TypedDict):
name: str
phone_number: str
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test wants to look at the diagnostics we print for TypedDicts in a union, but previously Person was a subtype of Animal, so this PR made the union disappear. Adding phone_number breaks the subtyping relationship, so the union remains.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually if we end up going with "don't generally simplify Unions of TypedDicts", I can put this test back the way it was.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better for the test to pass whichever way we go on that, so this change seems good to me :-)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I expect if we land more comprehensive cycle-panic avoidance, we might want to go back to simplifying typed dicts in unions.


class Animal(TypedDict):
name: str
Expand All @@ -104,13 +105,14 @@ def _(being: Person | Animal) -> None:
being["legs"] = 4 # error: [invalid-key]
```

## Unknown key for all elemens of a union
## Unknown key for all elements of a union

```py
from typing import TypedDict

class Person(TypedDict):
name: str
phone_number: str

class Animal(TypedDict):
name: str
Expand Down
Loading
Loading