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
{{ message }}
Repository navigation
mypy fails to recognise dunder init method override #15213
Bug Report
In an inheritance scenario with pydantic.BaseModel and generics, mypy 1.2.0 fails to recognise an overridden __init__(),
and wrongly reports an error (see "actual behavior").
To Reproduce
from __future__ importannotationsfromtypingimportGeneric, List, Type, TypeVarfrompydanticimportBaseModel, PrivateAttrB_co=TypeVar("B_co", bound=BaseModel, covariant=True)
classContainer(Generic[B_co], BaseModel):
"""Container is the base class for any item-type specific containers, to allow bundling and processing of aggregates """_item_class: Type[B_co] =PrivateAttr()
__root__: List[B_co]
def__init__(self, *args: B_co) ->None:
super().__init__(__root__=list(args))
def__len__(self) ->int:
returnlen(self.__root__)
def__getitem__(self, index: int) ->B_co:
returnself.__root__[index]
classItem(BaseModel):
name: strclassItems(Container[Item]):
_item_class=Itemitems=Items(Item(name="one"), Item(name="two"))
print(items)
Expected Behavior
The overridden __init__ method in the Container class has the correct signature, and mypy should not flag any issues.
Actual Behavior
ttt/ttt.py:33: error: Too many arguments for "Items" [call-arg]
ttt/ttt.py:33: error: Too many positional arguments for "Items" [misc]
ttt/ttt.py:33: error: Argument 1 to "Items" has incompatible type "Item"; expected "List[B_co]" [arg-type]
Found 3 errors in 1 file (checked 1 source file)
Your Environment
Mypy version used: 1.2.0
Mypy command-line flags: none
Mypy configuration options from mypy.ini (and other config files):
Are you using the Pydantic MyPy plugin? Some functionality works better with the plugin, particularly since MyPy started adding support for dataclass_transform in 1.1.1. (dataclass_transform standardizes a subset of the functionality that Pydantic offers; with vanilla MyPy, only that subset can be typechecked. The plugin adds additional functionality that's not in the dataclass_transform standard.)
Yes, I pasted the poetry config, listing the mypy pydantic plugin. The error message however appears, at least in my setup, with and without the plugin enabled. This error did not appear with mypy 1.0.
It seems that pydantic support wrongly takes precedence over the specific __init__ method.
Activity
wesleywright commentedon May 9, 2023
Are you using the Pydantic MyPy plugin? Some functionality works better with the plugin, particularly since MyPy started adding support for
dataclass_transformin 1.1.1. (dataclass_transformstandardizes a subset of the functionality that Pydantic offers; with vanilla MyPy, only that subset can be typechecked. The plugin adds additional functionality that's not in thedataclass_transformstandard.)marekro commentedon May 9, 2023
Yes, I pasted the poetry config, listing the mypy pydantic plugin. The error message however appears, at least in my setup, with and without the plugin enabled. This error did not appear with mypy 1.0.
It seems that pydantic support wrongly takes precedence over the specific __init__ method.