## 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>
## Summary
In this PR, I explored the proposal in
https://github.com/astral-sh/ty/issues/3251. The idea was to treat a
function like `def f(x="foo"): ...` as if the `x` parameter was declared
as `str`. This is what pyright does, and would follow what we do for
unannotated class attributes with default values. However, implementing
this showed a huge ecosystem impact of 8700 new diagnostics.
I tried various things to improve this. For example, I promoted `None`
to `Unknown | None` because it seems unreasonable to assume that you
only ever want callers to pass in `None`. Then there's a lot of code
that takes `x=()` as a default value and wants callers to be able to
pass in arbitrary tuples. I tried patching that by promoting `tuple[()]
-> tuple[Unknown, ...]`.
But the biggest problem is with functions that take a default int value
(e.g. `x=0`), but want to allow their callers to pass in float values. I
tried promoting the type of `x` from `int` to `float`/`complex`, but
that leads to the opposite problem: now the function body of *every*
function that takes `x=0` must also handle the possibility that `x` is
`float`/`complex`.
Ultimately, I think a scenario like this:
```py
def some_computation(..., tolerance=0): ...
some_computation(..., tolerance=1e-3)
```
is just far more common than the corresponding scenario with class
attributes.
I think it's unfortunate that our behavior is inconsistent with what we
do for class attributes, but I don't think it's worth introducing ~8000
diagnostics (no variant that I tried went below that limit). The
corresponding PR for attributes introduced an order of magnitude fewer
new diagnostics (https://github.com/astral-sh/ruff/pull/24531).
So in this PR, all I do is to document the current behavior.
closes https://github.com/astral-sh/ty/issues/3251
## Summary
Fixes https://github.com/astral-sh/ty/issues/2161
* Infer parameter default expressions using the parameter annotation as
type context.
* Fix `invalid-parameter-default` false positives for `TypedDict`
defaults (literals `{...}` and calls `dict(...)`).
* Add mdtests covering valid and invalid `TypedDict` defaults plus a
non-`TypedDict` assignability case.
## Test Plan
* Assert no diagnostics for `Foo` defaults via `{"x": 42}` and
`dict(x=42)`
* Assert `invalid-parameter-default` for missing key, wrong value type,
extra key in `Foo` defaults and non-assignable default (`tuple[int] =
()`)