Skip to content

A new approach to fastapi/dataclass interfaces#35

Merged
msullivan merged 1 commit intomainfrom
description-field
Jan 14, 2026
Merged

A new approach to fastapi/dataclass interfaces#35
msullivan merged 1 commit intomainfrom
description-field

Conversation

@msullivan
Copy link
Collaborator

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.

@msullivan msullivan requested a review from 1st1 January 14, 2026 01:46
Copy link
Member

@1st1 1st1 left a comment

Choose a reason for hiding this comment

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

Beautifully complicated, but I love test_fastapilike_2. Feels we're onto something here.

@1st1
Copy link
Member

1st1 commented Jan 14, 2026

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...

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.
@msullivan msullivan merged commit 816db86 into main Jan 14, 2026
2 checks passed
@msullivan msullivan deleted the description-field branch January 14, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants