[ty] Subscript assignment diagnostics follow-up (#21452)
Some checks are pending
CI / cargo test (${{ github.repository == 'astral-sh/ruff' && 'depot-windows-2022-16' || 'windows-latest' }}) (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (macos-latest) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / ty completion evaluation (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks instrumented (ruff) (push) Blocked by required conditions
CI / benchmarks instrumented (ty) (push) Blocked by required conditions
CI / benchmarks walltime (medium|multithreaded) (push) Blocked by required conditions
CI / benchmarks walltime (small|large) (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

## Summary

Follow up from https://github.com/astral-sh/ruff/pull/21411. Again,
there are more things that could be improved here (like the diagnostics
for `lists`, or extending what we have for `dict` to `OrderedDict` etc),
but that will have to be postponed.
This commit is contained in:
David Peter 2025-11-17 12:14:58 +01:00 committed by GitHub
parent 901e9cdf49
commit 1a86e13472
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 110 additions and 32 deletions

View file

@ -4,6 +4,15 @@
## Invalid value type
### For a `list`
```py
numbers: list[int] = []
numbers[0] = "three" # error: [invalid-assignment]
```
### For a `dict`
```py
config: dict[str, int] = {}
config["retries"] = "three" # error: [invalid-assignment]
@ -11,6 +20,15 @@ config["retries"] = "three" # error: [invalid-assignment]
## Invalid key type
### For a `list`
```py
numbers: list[int] = []
numbers["zero"] = 3 # error: [invalid-assignment]
```
### For a `dict`
```py
config: dict[str, int] = {}
config[0] = 3 # error: [invalid-assignment]

View file

@ -110,6 +110,6 @@ class Identity:
pass
a = Identity()
# error: [invalid-assignment] "Method `__setitem__` of type `bound method Identity.__setitem__(index: int, value: int) -> None` cannot be called with a key of type `Literal["a"]` and a value of type `Literal[0]` on object of type `Identity`"
# error: [invalid-assignment] "Invalid subscript assignment with key of type `Literal["a"]` and value of type `Literal[0]` on object of type `Identity`"
a["a"] = 0
```