Skip to content

Mypy plugin crashes when encountering from_orm method on user's classes #5187

@notypecheck

Description

@notypecheck

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug V1Bug related to Pydantic V1.XpendingIs unconfirmed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions