[ty] Synthesize __init__ for TypedDict#24476
Conversation
__init__ for TypedDict__init__ for TypedDict
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 87.72%. The percentage of expected errors that received a diagnostic held steady at 82.85%. The number of fully passing files held steady at 74/132. |
Memory usage reportMemory usage unchanged ✅ |
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-await |
40 | 0 | 0 |
invalid-return-type |
1 | 0 | 0 |
| Total | 41 | 0 | 0 |
Changes in flaky projects detected. Raw diff output excludes flaky projects; see the HTML report for details.
25d963d to
3cdb5ce
Compare
|
I haven't yet looked at the code but this surprisingly doesn't fix astral-sh/ty#3207 (PR #24378). Probably because the parameter lookup tries to lookup the function definition which doesn't exist for synthesized constructors. So we might need something that works for synthesized constructors too (obviously, not something that needs doing in this PR) |
| Parameter::positional_only(Some(Name::new_static("self"))).with_annotated_type(instance_ty); | ||
|
|
||
| let map_param = Parameter::positional_only(Some(Name::new_static("__map"))) | ||
| .with_annotated_type(instance_ty); |
There was a problem hiding this comment.
Technically... this can be a subset of the instance type, if any missing fields are provided by the following keyword arguments. I'm not sure if we want to try modeling that -- does it even matter for the IDE cases?
There was a problem hiding this comment.
is it possible to model that? (maybe by iterating the fields and using type dict[str, str | int]?) pyright and pyrefly are showing the class type.
I think it's useful to let the user know about this what kind of keys it accepts but that can also be something we can add as docstring to this signature to explain it if users need more guidance with the signature.
There was a problem hiding this comment.
I doubt it. I think we should just ignore it.
There was a problem hiding this comment.
But maybe acknowledge it in function header?
30afc50 to
6dca88a
Compare
* main: [ty] Fix bad diagnostic range for incorrect implicit `__init_subclass__` calls (#24541) [ty] Add a `SupportedPythonVersion` enum (#24412) [ty] Ignore unsupported editor-selected Python versions (#24498) [ty] Add snapshots for `__init_subclass__` diagnostics (#24539) [ty] Minor fix in tests (#24538) [ty] Allow `Final` variable assignments in `__post_init__` (#24529) [ty] Expand test suite for assignment errors (#24537) [ty] Use `map`, not `__map`, as the name of the mapping parameter in `TypedDict` `__init__` methods (#24535) [ty] Rework logic for synthesizing `TypedDict` methods (#24534) [flake8-bandit] Fix S103 false positives and negatives in mask analysis (#24424) [ty] mdtest.py: update dependencies (#24533) Rename patterns and arguments source order iterator method (#24532) [ty] Omit invalid keyword arguments from `TypedDict` signature (#24522) [ty] support super() in metaclass methods (#24483) [ty] Synthesize `__init__` for `TypedDict` (#24476)
* main: Bump typing conformance suite commit to latest upstream (#24553) [ty] Reject deleting`Final` attributes (#24508) [ty] Respect property deleters in attribute deletion checks (#24500) [ty] stop unioning Unknown into types of un-annotated attributes (#24531) [ty] Fix bad diagnostic range for incorrect implicit `__init_subclass__` calls (#24541) [ty] Add a `SupportedPythonVersion` enum (#24412) [ty] Ignore unsupported editor-selected Python versions (#24498) [ty] Add snapshots for `__init_subclass__` diagnostics (#24539) [ty] Minor fix in tests (#24538) [ty] Allow `Final` variable assignments in `__post_init__` (#24529) [ty] Expand test suite for assignment errors (#24537) [ty] Use `map`, not `__map`, as the name of the mapping parameter in `TypedDict` `__init__` methods (#24535) [ty] Rework logic for synthesizing `TypedDict` methods (#24534) [flake8-bandit] Fix S103 false positives and negatives in mask analysis (#24424) [ty] mdtest.py: update dependencies (#24533) Rename patterns and arguments source order iterator method (#24532) [ty] Omit invalid keyword arguments from `TypedDict` signature (#24522) [ty] support super() in metaclass methods (#24483) [ty] Synthesize `__init__` for `TypedDict` (#24476)
## Summary
<!-- What's the purpose of the change? What does it do, and why? -->
This PR adds a synthesized `__init__` method for `TypedDict` that is
used in server for hover.
The new method is not used for type checking.
The reason is that the current sophisticated validation logic has better
UX than normal argument matching.
The `__init__` method has two bindings right now:
```
class Movie(TypedDict):
title: str
year: int
class Movie(
__map: Movie,
/,
*,
title: str = ...,
year: int = ...
)
class Movie(
*,
title: str = ...,
year: int = ...
)
```
I removed the previous TODO to use synthesized method for type checking
since this is being implemented with another solution.
#24450.
## Test Plan
<!-- How was it tested? -->
---------
Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
Summary
This PR adds a synthesized
__init__method forTypedDictthat is used in server for hover.The new method is not used for type checking.
The reason is that the current sophisticated validation logic has better UX than normal argument matching.
The
__init__method has two bindings right now:I removed the previous TODO to use synthesized method for type checking since this is being implemented with another solution. #24450.
Test Plan