Skip to content

[ty] Synthesize __init__ for TypedDict#24476

Merged
charliermarsh merged 9 commits intoastral-sh:mainfrom
Glyphack:shaygan-typedict-binding
Apr 9, 2026
Merged

[ty] Synthesize __init__ for TypedDict#24476
charliermarsh merged 9 commits intoastral-sh:mainfrom
Glyphack:shaygan-typedict-binding

Conversation

@Glyphack
Copy link
Copy Markdown
Contributor

@Glyphack Glyphack commented Apr 7, 2026

Summary

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

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Apr 7, 2026
@Glyphack Glyphack changed the title Synthesize __init__ for TypedDict [ty] Synthesize __init__ for TypedDict Apr 7, 2026
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 7, 2026

Typing conformance results

No changes detected ✅

Current numbers
The 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.

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 7, 2026

Memory usage report

Memory usage unchanged ✅

@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot Bot commented Apr 7, 2026

ecosystem-analyzer results

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.

Full report with detailed diff (timing results)

@Glyphack Glyphack force-pushed the shaygan-typedict-binding branch from 25d963d to 3cdb5ce Compare April 9, 2026 05:43
@Glyphack Glyphack marked this pull request as ready for review April 9, 2026 05:54
@astral-sh-bot astral-sh-bot Bot requested a review from charliermarsh April 9, 2026 05:54
@carljm carljm removed their request for review April 9, 2026 05:56
@AlexWaygood AlexWaygood added the server Related to the LSP server label Apr 9, 2026
@MichaReiser
Copy link
Copy Markdown
Member

MichaReiser commented Apr 9, 2026

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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

@Glyphack Glyphack Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt it. I think we should just ignore it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But maybe acknowledge it in function header?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add and merge.

@charliermarsh charliermarsh force-pushed the shaygan-typedict-binding branch from 30afc50 to 6dca88a Compare April 9, 2026 18:57
@charliermarsh charliermarsh merged commit 239a6cb into astral-sh:main Apr 9, 2026
55 checks passed
@Glyphack Glyphack deleted the shaygan-typedict-binding branch April 10, 2026 04:31
carljm added a commit that referenced this pull request Apr 10, 2026
* 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)
carljm added a commit that referenced this pull request Apr 10, 2026
* 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)
ibraheemdev pushed a commit that referenced this pull request Apr 15, 2026
## 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants