mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-21 07:41:53 +00:00
[ty] allow any string Literal
type expression as a key when constructing a TypedDict
(#20792)
This commit is contained in:
parent
75f3c0e8e6
commit
db91ac7dce
2 changed files with 48 additions and 7 deletions
|
@ -46,6 +46,46 @@ Methods that are available on `dict`s are also available on `TypedDict`s:
|
|||
bob.update(age=26)
|
||||
```
|
||||
|
||||
`TypedDict` keys do not have to be string literals, as long as they can be statically determined
|
||||
(inferred to be of type string `Literal`).
|
||||
|
||||
```py
|
||||
from typing import Literal, Final
|
||||
|
||||
NAME = "name"
|
||||
AGE = "age"
|
||||
|
||||
def non_literal() -> str:
|
||||
return "name"
|
||||
|
||||
def name_or_age() -> Literal["name", "age"]:
|
||||
return "name"
|
||||
|
||||
carol: Person = {NAME: "Carol", AGE: 20}
|
||||
|
||||
reveal_type(carol[NAME]) # revealed: str
|
||||
# error: [invalid-key] "TypedDict `Person` cannot be indexed with a key of type `str`"
|
||||
reveal_type(carol[non_literal()]) # revealed: Unknown
|
||||
reveal_type(carol[name_or_age()]) # revealed: str | int | None
|
||||
|
||||
FINAL_NAME: Final = "name"
|
||||
FINAL_AGE: Final = "age"
|
||||
|
||||
def _():
|
||||
carol: Person = {FINAL_NAME: "Carol", FINAL_AGE: 20}
|
||||
|
||||
CAPITALIZED_NAME = "Name"
|
||||
|
||||
# error: [invalid-key] "Invalid key access on TypedDict `Person`: Unknown key "Name" - did you mean "name"?"
|
||||
# error: [missing-typed-dict-key] "Missing required key 'name' in TypedDict `Person` constructor"
|
||||
dave: Person = {CAPITALIZED_NAME: "Dave", "age": 20}
|
||||
|
||||
def age() -> Literal["age"] | None:
|
||||
return "age"
|
||||
|
||||
eve: Person = {"na" + "me": "Eve", age() or "age": 20}
|
||||
```
|
||||
|
||||
The construction of a `TypedDict` is checked for type correctness:
|
||||
|
||||
```py
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue