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

Fix pydantic type decorator - add missing internal methods #3366

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Git-Lior
Copy link

I noticed that when wrapping a class with @strawberry.experimental.pydantic.type, internal methods "disappear" (I hope I got the terminology right, by internal methods I mean methods not marked as @strawberry.field).

See this example from the test I added:

class User(pydantic.BaseModel):
    age: int
    password: Optional[str]

@strawberry.experimental.pydantic.type(User)
class UserType:
    age: strawberry.auto
    password: strawberry.auto

    @strawberry.field
    def password_hash(self) -> int:
        return self.perform_hash(self.password)

    def perform_hash(self, data: str) -> int:
        return hash(data)

Before the change, querying for password_hash would have raised an error: AttributeError: type object 'UserType' has no attribute 'perform_hash'.

The change I propose is simple - after calling make_dataclass, add back attributes that were not copied from the original class.

Description

Types of Changes

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).

@botberry
Copy link
Member

Thanks for adding the RELEASE.md file!

Here's a preview of the changelog:


Fix pydantic type decorator - add internal methods not copied by dataclasses.make_dataclass()

Here's the tweet text:

🆕 Release (next) is out! Thanks to Git-Lior for the PR 👏

Get it here 👉 https://beta.strawberry.rocks/release/(next)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants