Users can't easily define their own pydantic conversion decorators with their desired defaults or values.
It wouldn't be mypy compatible.
First, partial functions don't work in mypy
The second alternative is to manually define it out
def input_all_fields(
model: Type[PydanticModel],
fields: Optional[List[str]] = None,
name: Optional[str] = None,
is_interface: bool = False,
description: Optional[str] = None,
directives: Optional[Sequence[StrawberrySchemaDirective]] = (),
use_pydantic_alias: bool = True,
) -> Callable[..., Type[StrawberryTypeFromPydantic[PydanticModel]]]:
# Custom decorator to create an input with all fields
return strawberry.experimental.pydantic.type(
model=model,
fields=fields,
name=name,
is_input=True,
is_interface=is_interface,
description=description,
directives=directives,
all_fields=True,
use_pydantic_alias=use_pydantic_alias,
)
Now this doesn't work too, because of python/mypy#3135. Mypy doesn't work with class decorators out of the box.
They will need to write their own plugin to hook onto the decorator name.
In our mypy plugin, we check if it is a pydantic conversion parameter through a whitelist in _is_strawberry_pydantic_decorator
.
We can allow more functions to be added through this whitelist.
We can have a specifc keyword that the class decorator will hook on. e.g. strawberry_pydantic
return any(
fullname.endswith(decorator)
for decorator in {
.....
"strawberry_pydantic"
}
)
This way the user can define
def input_all_fields_strawberry_pydantic(
model: Type[PydanticModel],
fields: Optional[List[str]] = None,
name: Optional[str] = None,
is_interface: bool = False,
description: Optional[str] = None,
directives: Optional[Sequence[StrawberrySchemaDirective]] = (),
use_pydantic_alias: bool = True,
) -> Callable[..., Type[StrawberryTypeFromPydantic[PydanticModel]]]:
# Custom decorator to create an input with all fields
return strawberry.experimental.pydantic.type(
model=model,
fields=fields,
name=name,
is_input=True,
is_interface=is_interface,
description=description,
directives=directives,
all_fields=True,
use_pydantic_alias=use_pydantic_alias,
)
and our mypy plugin will hook onto it.
This workaround is quite hacky.
We can add a setting for our mypy plugin which allows users to specify the decorator names.
We'll hook onto those
# mypy.ini
[strawberry-mypy]
pydantic_decorator_names = input_all_fields, some_other_decorator_name
Looking for other suggestions. @patrick91
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too