You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When nesting generic classes, one of which is a subclass of BaseModel (as shown in the example), Pydantic incorrectly raises a TypeError with the explanation that one of the classes is not generic:
Traceback (most recent call last):
File "/Users/***/Library/Application Support/JetBrains/Toolbox/apps/PyCharm-P/ch-0/232.8660.197/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevconsole.py", line 364, in runcode
coro = func()
^^^^^^
File "<input>", line 12, in <module>
File "/Users/***/Library/Caches/pypoetry/virtualenvs/***/lib/python3.11/site-packages/pydantic/main.py", line 607, in __class_getitem__
raise TypeError(f'{cls} is not a generic class')
TypeError: <class '__main__.BrokenB'> is not a generic class
According to MyPy docs, the example should be valid (and without inheriting BaseModel works just fine.)
There is a workaround - by specifying the Generic keyword with the same TypeVar when inheriting the base class (A in the example code below), the check inside Pydantic succeeds.
Up to this point, the fact that this didn't work was more of an implementation detail than anything intentional, but I think I found a way to make it work. At least, I found a tweak in #7119 that makes this work properly and keeps all existing tests passing. If this introduces any new issues, I'll address them when reported.
Initial Checks
Description
When nesting generic classes, one of which is a subclass of
BaseModel
(as shown in the example), Pydantic incorrectly raises aTypeError
with the explanation that one of the classes is not generic:According to MyPy docs, the example should be valid (and without inheriting
BaseModel
works just fine.)There is a workaround - by specifying the
Generic
keyword with the sameTypeVar
when inheriting the base class (A
in the example code below), the check inside Pydantic succeeds.Example Code
Python, Pydantic & OS Version
Selected Assignee: @dmontagu
The text was updated successfully, but these errors were encountered: