Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strawberry.pydantic.experimental.type strips all staticmethods from type definition besides from_pydantic() and to_pydantic() #3601

Open
bg-radai opened this issue Aug 23, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@bg-radai
Copy link

bg-radai commented Aug 23, 2024

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'

In the pydantic type wrapper the cls object is redefined here using dataclasses.make_dataclass():

cls = 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

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.
Fund with Polar
@bg-radai bg-radai added the bug Something isn't working label Aug 23, 2024
@quinguyen-dev
Copy link

Hey, OP.

I just ran into this issue doing something similar while trying to override from_pydantic(). Were you able to find a solution to this?

Appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants