When a staticmethod not named from_pydantic
or to_pydantic
is added to a type decorated with strawberry.pydantic.experimental.type
it is not available at runtime. Attempting to call the staticmethod will result in an AttributeError.
given an example type like this:
import strawberry
from pydantic import BaseModel
class Foo(BaseModel):
bar: int
@strawberry.experimental.pydantic.type(model=Foo)
class FooGQLType:
bar: strawberry.auto
@staticmethod
def do_something() -> None:
# impl doesn't matter
pass
if __name__ == "__main__":
FooGQLType.do_something()
Running this code will result in `AttributeError: type object 'FooGQLType' has no attribute 'do_something'
In the pydantic type wrapper the cls
object is redefined here using dataclasses.make_dataclass()
:
The do_something
method is available as an attribute on cls
before this redefinition but is unavailable afterwards.
Both to_pydantic
and from_pydantic
are handled as special cases. If they exist on the type definition they are saved to the namespace dict before this redefinition and re-added after the fact. if they do not exist, the default definitions are supplied
I ran into this issue attempting to find a workaround for a question I posted in the discord titled "Conditionally override from_pydantic()
"
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