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 plugin crashes when encountering from_orm method on user's classes #5187

Closed
5 of 15 tasks
Tracked by #5192
ThirVondukr opened this issue Mar 14, 2023 · 11 comments
Closed
5 of 15 tasks
Tracked by #5192
Labels
bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable

Comments

@ThirVondukr
Copy link

Initial Checks

  • I have searched GitHub for a duplicate issue and I'm sure this is something new
  • I have searched Google & StackOverflow for a solution and couldn't find anything
  • I have read and followed the docs and still think this is a bug
  • I am confident that the issue is with pydantic (not my code, or another library in the ecosystem like FastAPI or mypy)

Description

I have a OrmMixin (provided in example code) to help with mapping sqlalchemy models into strawberry-graphql models.

Mypy plugin seems to have problems with from_orm method and works fine if it's renamed, for example into from_orm_.

$ mypy main.py
main.py:19: error: The pydantic mypy plugin ran into unexpected behavior: ctx.type: Type[main.OrmMixin[_TModel`1, _TType`2]] (of type TypeType)
Please consider reporting this bug at https://github.com/pydantic/pydantic/issues/new/choose so we can try to fix it!  [pydantic-unexpected]       
main.py:23: error: The pydantic mypy plugin ran into unexpected behavior: ctx.type: Type[main.OrmMixin[_TModel`1, _TType`2]] (of type TypeType)
Please consider reporting this bug at https://github.com/pydantic/pydantic/issues/new/choose so we can try to fix it!  [pydantic-unexpected]       
Found 2 errors in 1 file (checked 1 source file)

Example Code

import abc
from collections.abc import Iterable
from typing import Generic, TypeVar

_TModel = TypeVar("_TModel")
_TType = TypeVar("_TType")


class OrmMixin(Generic[_TModel, _TType]):
    @classmethod
    @abc.abstractmethod
    def from_orm(cls, model: _TModel) -> _TType:
        raise NotImplementedError

    @classmethod
    def from_orm_optional(cls, model: _TModel | None) -> _TType | None:
        if model is None:
            return None
        return cls.from_orm(model)

    @classmethod
    def from_orm_list(cls, models: Iterable[_TModel]) -> list[_TType]:
        return [cls.from_orm(model) for model in models]

Python, Pydantic & OS Version

pydantic version: 1.10.6
pydantic compiled: True
install path: .venv\Lib\site-packages\pydantic
python version: 3.11.1 (tags/v3.11.1:a7a450f, Dec  6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)]
platform: Windows-10-10.0.19044-SP0
optional deps. installed: ['typing-extensions']

Affected Components

@ThirVondukr ThirVondukr added bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable labels Mar 14, 2023
@jtc42
Copy link

jtc42 commented Mar 14, 2023

I'm having to just # type: ignore these lines for now, which seems to stop the crash at least. Thanks for reporting!

@dmontagu
Copy link
Contributor

Can you share some code showing how you are using it to produce this error?

@dmontagu
Copy link
Contributor

I don't get this error running mypy over the code in the snippet you've shared; however, I looked into it a bit and I see where it needs handling, and I think I it should be straightforward to fix if you can give me some code that shows how to reproduce the issue.

@dmontagu
Copy link
Contributor

Oh also — could you share what version of mypy you are using? (You can find this by running mypy --version)

@ThirVondukr
Copy link
Author

$ mypy --version
mypy 1.1.1 (compiled: yes)

It errors out on provided code:

import abc
from collections.abc import Iterable
from typing import Generic, TypeVar

_TModel = TypeVar("_TModel")
_TType = TypeVar("_TType")


class OrmMixin(Generic[_TModel, _TType]):
    @classmethod
    @abc.abstractmethod
    def from_orm(cls, model: _TModel) -> _TType:
        raise NotImplementedError

    @classmethod
    def from_orm_optional(cls, model: _TModel | None) -> _TType | None:
        if model is None:
            return None
        return cls.from_orm(model)

    @classmethod
    def from_orm_list(cls, models: Iterable[_TModel]) -> list[_TType]:
        return [cls.from_orm(model) for model in models]

Mypy config:

[tool.mypy]
python_version = "3.11"
plugins = [
    "pydantic.mypy",
]

check_untyped_defs = true
disallow_untyped_calls = true
disallow_untyped_defs = true
disallow_incomplete_defs = true
disallow_any_generics = true
no_implicit_optional = true

warn_redundant_casts = true
warn_unused_ignores = true
warn_return_any = true
warn_unreachable = true

strict_optional = true
strict_equality = true

@ThirVondukr
Copy link
Author

# pyproject.toml
dependencies = [
    "mypy>=1.1.1",
    "pydantic>=1.10.6",
]
$ pip freeze
mypy==1.1.1
mypy-extensions==1.0.0
pydantic==1.10.6
typing_extensions==4.5.0

@samuelcolvin
Copy link
Member

see #5192

@ThirVondukr
Copy link
Author

Same behavior with 1.0.1

@dmontagu
Copy link
Contributor

@ThirVondukr thank you for your help/patience. This turned out to have an easy fix so I was able to get it in. That said, I think some of the other issues listed in #5192 will be more involved to resolve, so I don't want to promise as quick of a turn-around on those.

@ThirVondukr
Copy link
Author

Don't stress it, thanks for your help 😃

@dmontagu
Copy link
Contributor

Closed by #5193 and #5194

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X unconfirmed Bug not yet confirmed as valid/applicable
Projects
None yet
Development

No branches or pull requests

4 participants