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

mypy errors when using Pydantic integration #1830

Closed
olivier-thatch opened this issue Apr 20, 2022 · 9 comments · Fixed by #1832
Closed

mypy errors when using Pydantic integration #1830

olivier-thatch opened this issue Apr 20, 2022 · 9 comments · Fixed by #1832
Assignees
Labels
mypy Issues related to the mypy plugin

Comments

@olivier-thatch
Copy link

olivier-thatch commented Apr 20, 2022

Hello,

I'm trying to use the Pydantic integration and getting a few mypy errors.

Simplified version of the code:

import strawberry
from pydantic import BaseModel, EmailStr


class UserBase(BaseModel):
    email: Optional[EmailStr] = None


class UserCreate(PatientBase):
    email: EmailStr


@strawberry.experimental.pydantic.input(model=UserCreate, all_fields=True)
class UserCreateInput:
    pass


@strawberry.mutation
def create_user(self, info: Info, input_data: UserCreateInput) -> PatientType:
    instance = input_data.to_pydantic()
    # ...

This causes 2 errors:

  1. @strawberry.experimental.pydantic.input(model=UserCreate, all_fields=True) causes this error:

    error: model argument in decorator failed to be parsed
    
  2. instance = input_data.to_pydantic() causes this error:

    error: "UserCreateInput" has no attribute "to_pydantic"
    

Package versions:

  • Python 3.10.3
  • strawberry-graphql 0.106.3
  • pydantic 1.9.0

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
@motherofcoconuts
Copy link

motherofcoconuts commented Apr 20, 2022

Experiencing the same. Furthermore I have similar issues with from_pydantic. Ex: error: "Type[User]" has no attribute "from_pydantic"

@thejaminator
Copy link
Contributor

hey @motherofcoconuts and @olivier-thatch are you using our mypy plugin? i fixed that

[mypy]
plugins = strawberry.ext.mypy_plugin

@motherofcoconuts
Copy link

motherofcoconuts commented Apr 21, 2022

hey @thejaminator i am indeed:

pyproject.toml

[tool.mypy]
plugins = ["pydantic.mypy", "strawberry.ext.mypy_plugin", "sqlalchemy.ext.mypy.plugin"]

@thejaminator
Copy link
Contributor

thejaminator commented Apr 21, 2022

@motherofcoconuts uhoh. i've figured out why

@motherofcoconuts
Copy link

Pulled from my poetry.lock file:

[[package]]
name = "strawberry-graphql"
version = "0.97.0"
description = "A library for creating GraphQL APIs"
category = "main"
optional = false
python-versions = ">=3.7,<4.0"
[[package]]
name = "mypy"
version = "0.942"
description = "Optional static typing for Python"
category = "dev"
optional = false
python-versions = ">=3.6"

@thejaminator
Copy link
Contributor

thejaminator commented Apr 21, 2022

@motherofcoconuts as a temporary workaround use the is_input=True parameter instead for strawberry.experimental.pydantic.type

from typing import Optional
import strawberry
from pydantic import BaseModel, EmailStr


class UserBase(BaseModel):
    email: Optional[EmailStr] = None


class UserCreate(BaseModel):
    email: EmailStr


@strawberry.experimental.pydantic.type(model=UserCreate, all_fields=True, is_input=True)
class UserCreateInput:
    pass


@strawberry.mutation
def create_user(self, info, input_data: UserCreateInput) -> None:
    instance = input_data.to_pydantic()
    # ...

basically

@strawberry.experimental.pydantic.type(model=UserCreate, all_fields=True, is_input=True)

instead of

@strawberry.experimental.pydantic.input(model=UserCreate, all_fields=True)

@thejaminator thejaminator added the mypy Issues related to the mypy plugin label Apr 21, 2022
@thejaminator thejaminator self-assigned this Apr 21, 2022
@motherofcoconuts
Copy link

motherofcoconuts commented Apr 21, 2022

@thejaminator @olivier-thatch is using the pydantic.input type. I actually use the following partials:

type = partial(strawberry.experimental.pydantic.type, all_fields=True)
input = partial(strawberry.experimental.pydantic.type, is_input=True, all_fields=True)

and experience it for both types with the @type decorator as well as the @input decoratior.

@thejaminator
Copy link
Contributor

thejaminator commented Apr 21, 2022

@thejaminator @olivier-thatch is using the pydantic.input type. I actually use the following partials:

type = partial(strawberry.experimental.pydantic.type, all_fields=True)
input = partial(strawberry.experimental.pydantic.type, is_input=True, all_fields=True)

and experience it for both types with the @type decorator as well as the @input decoratior.

ah ok, yeah seems like the mypy plugin isn't working well with those partials :(

See python/mypy#1484

@thejaminator
Copy link
Contributor

@motherofcoconuts for your case, where you manually define your own decorator. unfortunately there are issues with mypy that i write here #1833

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mypy Issues related to the mypy plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants