Files
Alex WaygoodandDavid Peter d65c056994 [ty] Split invalid-key into three TypedDict diagnostic rules (#29955)
## Summary

Given a `Movie` TypedDict with a `title` field, ty currently reports
`invalid-key` for three cases. This PR gives each its own rule.

| Example | Rule |
| --- | --- |
| `movie["titel"]` reads a misspelled key | `invalid-typed-dict-key` |
| `movie[key]` reads a key whose type is `str` and whose value is
unknown | `non-literal-typed-dict-key` |
| `Movie(title="Alien", titel="Alien")` supplies an unknown key during
construction | `unknown-typed-dict-constructor-key` |

Both subscript rules apply to reads and assignments. An invalid
non-string key such as `movie[0]` reports `invalid-typed-dict-key`. The
constructor rule reports the extra `titel` key in `movie: Movie =
{"title": "Alien", "titel": "Alien"}`. For a closed TypedDict, it flags
`{key: value}` when `key` has type `str` and might introduce an extra
item.

Projects migrating to ty lose typo detection if they disable
`invalid-key` to allow dynamic TypedDict lookups. In mypy, disabling
`literal-required` allows `movie[key]` while `movie["titel"]` still
produces a `typeddict-item` error. Pyright allows the dynamic lookup by
default and also flags the typo. To keep that behavior in ty, set
`non-literal-typed-dict-key` to `ignore` and leave
`invalid-typed-dict-key` enabled.

All three rules default to `error`. With the alias handling from #29960,
`invalid-key` maps to `invalid-typed-dict-key` in configuration, inline
ignores, and existing baselines. Previously baselined diagnostics can
therefore reappear under either of the other two rule names. To preserve
a custom severity across the split, set all three rules to the same
level. For inline ignores, use the rule or rules reported on that line.

---------

Co-authored-by: David Peter <mail@david-peter.de>
2026-11-06 15:34:17 +00:00

2.8 KiB

Subscript assignment diagnostics

Invalid value type

For a list

numbers: list[int] = []
numbers[0] = "three"  # error: [invalid-assignment]

For a dict

config: dict[str, int] = {}
config["retries"] = "three"  # error: [invalid-assignment]

Annotated subscript targets

Annotations on subscript targets are ignored, and the assignment is checked against the collection's element type.

numbers: list[int] = []
# error: [invalid-type-form]
# error: [invalid-assignment]
numbers[0]: str = "three"

Invalid key type

For a list

numbers: list[int] = []
numbers["zero"] = 3  # error: [invalid-assignment]

For a dict

config: dict[str, int] = {}
config[0] = 3  # error: [invalid-assignment]

Invalid value type for TypedDict

from typing import TypedDict

class Config(TypedDict):
    retries: int

def _(config: Config) -> None:
    config["retries"] = "three"  # error: [invalid-assignment]

Invalid key type for TypedDict

from typing import TypedDict

class Config(TypedDict):
    retries: int

def _(config: Config) -> None:
    config[0] = 3  # error: [invalid-typed-dict-key]

Misspelled key for TypedDict

from typing import TypedDict

class Config(TypedDict):
    retries: int

def _(config: Config) -> None:
    config["Retries"] = 30.0  # error: [invalid-typed-dict-key]

No __setitem__ method

class ReadOnlyDict:
    def __getitem__(self, key: str) -> int:
        return 42

config = ReadOnlyDict()
config["retries"] = 3  # error: [invalid-assignment]

Possibly missing __setitem__ method

def _(config: dict[str, int] | None) -> None:
    config["retries"] = 3  # error: [invalid-assignment]

Unknown key for one element of a union

from typing import TypedDict

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

class Animal(TypedDict):
    name: str
    legs: int

def _(being: Person | Animal) -> None:
    being["legs"] = 4  # error: [invalid-typed-dict-key]

Unknown key for all elements of a union

from typing import TypedDict

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

class Animal(TypedDict):
    name: str
    legs: int

def _(being: Person | Animal) -> None:
    # error: [invalid-typed-dict-key]
    # error: [invalid-typed-dict-key]
    being["nane"] = "unknown"

Wrong value type for one element of a union

def _(config: dict[str, int] | dict[str, str]) -> None:
    config["retries"] = 3  # error: [invalid-assignment]

Wrong value type for all elements of a union

def _(config: dict[str, int] | dict[str, str]) -> None:
    # error: [invalid-assignment]
    # error: [invalid-assignment]
    config["retries"] = 3.0