pydantic icon indicating copy to clipboard operation
pydantic copied to clipboard

pydantic.mypy plugin bug (bound TypeVar with Generic)

Open tomhampshire opened this issue 3 years ago • 1 comments

Checks

  • [x] I added a descriptive title to this issue
  • [x] I have searched (google, github) for similar issues and couldn't find anything
  • [x] I have read and followed the docs and still think this is a bug

Bug

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

             pydantic version: 1.9.1
            pydantic compiled: True
                 install path: /home/***/***/***/***/env/lib/python3.8/site-packages/pydantic
               python version: 3.8.13 (default, Mar 23 2022, 12:34:45)  [GCC 11.2.0]
                     platform: Linux-5.15.54-1-lts-x86_64-with-glibc2.34
     optional deps. installed: ['email-validator', 'typing-extensions']

The bug has already been reported, but on the mypy site: https://github.com/python/mypy/issues/12858 The error seems to be with the mypy-pydantic plugin, but, as far as I can tell, this has not been reported at pydantic.

I have code to reproduce the bug. I have tried different versions of mypy (0.930, 0.961) with the same result:

"""Example to break pydantic.mypy!"""
from typing import Generic, TypeVar

from pydantic.generics import GenericModel


# The `bound` argument breaks the pydantic.mypy plugin
T = TypeVar("T", bound="ClassName")


class ClassName(GenericModel, Generic[T]):
    pass

The output is:

Please report a bug at https://github.com/python/mypy/issues
version: 0.961
Traceback (most recent call last):
  File "mypy/semanal.py", line 5350, in accept
  File "mypy/nodes.py", line 747, in accept
  File "mypy/semanal.py", line 637, in visit_func_def
  File "mypy/semanal.py", line 669, in analyze_func_def
  File "mypy/semanal.py", line 5050, in defer
AssertionError: Must not defer during final iteration

Disabling the pydantic.mypy plugin OR removing bound="ClassName" both stop the error from occurring.

tomhampshire avatar Jul 15 '22 15:07 tomhampshire

Confirmed, but I suspect we won't be able to provide full support for this, just return a clearer error.

samuelcolvin avatar Aug 30 '22 12:08 samuelcolvin

I'm not able to reproduce it with the latest mypy, using either Pydantic v1 and v2.

I guess the problem was solved in the meantime.

Kludex avatar May 01 '23 06:05 Kludex