A new approach to fastapi/dataclass interfaces#35
Merged
Conversation
1st1
reviewed
Jan 14, 2026
Member
1st1
left a comment
There was a problem hiding this comment.
Beautifully complicated, but I love test_fastapilike_2. Feels we're onto something here.
Member
I still want us to push through the Annotated stuff 🤦♂️ |
We want to be able to support transforming types based on
dataclasses/attrs/pydantic style field descriptors. In order to do
that, we need to be able to consume things like calls to `Field`.
Our strategy for this is to introduce a new type
`InitField[KwargDict]` that collects arguments defined by a
`KwargDict: TypedDict`:
```
class InitField[KwargDict: BaseTypedDict]:
def __init__(self, **kwargs: typing.Unpack[KwargDict]) -> None:
...
def _get_kwargs(self) -> KwargDict:
...
```
When `InitField` or (more likely) a subtype of it is instantiated
inside a class body, we infer a *more specific* type for it, based on
`Literal` types for all the arguments passed.
So if we write:
class A:
foo: int = InitField(default=0)
then we would infer the type
`InitField[TypedDict('...', {'default': Literal[0]})]`
for the initializer, and that would be made available
as the `Init` field of the `Member`.
Honestly this is pretty subtle and will probably be controversial, but
maybe *less* controversial than the ``Annotated`` stuff and it will
produce a better result than that, so...
Take a look at test_fastapilike_2.py for the full fastapi + `__init__`
generation example using this system.
40c241a to
8bbf12d
Compare
dnwpark
approved these changes
Jan 14, 2026
375e1be to
8bbf12d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We want to be able to support transforming types based on
dataclasses/attrs/pydantic style field descriptors. In order to do
that, we need to be able to consume things like calls to
Field.Our strategy for this is to introduce a new type
InitField[KwargDict]that collects arguments defined by aKwargDict: TypedDict:When
InitFieldor (more likely) a subtype of it is instantiatedinside a class body, we infer a more specific type for it, based on
Literaltypes for all the arguments passed.So if we write:
class A:
foo: int = InitField(default=0)
then we would infer the type
InitField[TypedDict('...', {'default': Literal[0]})]for the initializer, and that would be made available
as the
Initfield of theMember.Honestly this is pretty subtle and will probably be controversial, but
maybe less controversial than the
Annotatedstuff and it willproduce a better result than that, so...
Take a look at test_fastapilike_2.py for the full fastapi +
__init__generation example using this system.