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.
Describe the Bug
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'
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
System Information
Operating system: MacOS Sonoma 14.4.1
Strawberry version: 0.237.3
Python version: 3.11.2
Additional Context
I ran into this issue attempting to find a workaround for a question I posted in the discord titled "Conditionally override from_pydantic()"
Upvote & Fund
We're using Polar.sh so you can upvote and help fund this issue.
We receive the funding once the issue is completed & confirmed by you.
Thank you in advance for helping prioritize & fund our backlog.
When a staticmethod not named
from_pydantic
orto_pydantic
is added to a type decorated withstrawberry.pydantic.experimental.type
it is not available at runtime. Attempting to call the staticmethod will result in an AttributeError.Describe the Bug
given an example type like this:
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 usingdataclasses.make_dataclass()
: https://github.com/strawberry-graphql/strawberry/blob/cafc388f8221781507a53f00c02d3913679ae5be/strawberry/experimental/pydantic/object_type.py#L248The
do_something
method is available as an attribute oncls
before this redefinition but is unavailable afterwards.Both
to_pydantic
andfrom_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 suppliedSystem Information
Additional Context
I ran into this issue attempting to find a workaround for a question I posted in the discord titled "Conditionally override
from_pydantic()
"Upvote & Fund