-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Description
Bug
When trying to override the BaseModel.__str__ method, mypy (version 0.770) gets upset. This seems to be due a combination of python/mypy#5485 and the way BaseModel sets its default __str__ implementation:
https://github.com/samuelcolvin/pydantic/blob/0f948618bab7d36a7ee153f3ad02594b56554e74/pydantic/main.py#L305-L310
Here's a minimal reproduction:
from pydantic.main import BaseModel
class Test(BaseModel):
value: int
def __str__(self) -> str:
return str(self.value)which has the mypy output of
test.py:5: error: Signature of "__str__" incompatible with supertype "BaseModel"
Found 1 error in 1 file (checked 1 source file)
Note that I tried modifying BaseModel to explicitly call through to Representation for these methods and that fixed the issue:
def __repr_name__(self) -> str:
return Representation.__repr_name__(self)
def __repr_str__(self, join_str: str) -> str:
return Representation.__repr_str__(self, join_str)
def __pretty__(self, fmt: Callable[[Any], Any], **kwargs: Any) -> Generator[Any, None, None]:
return Representation.__pretty__(self, fmt, **kwargs)
def __str__(self) -> str:
return Representation.__str__(self)
def __repr__(self) -> str:
return Representation.__repr__(self)I'm not sure if this is an acceptable solution; if it is I'm happy to open a PR to make this change. I'm also not clear on why BaseModel doesn't inherit from Representation, but I assume there's a good reason for it 🙂
Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":
pydantic version: 1.4
pydantic compiled: False
install path: <omitted>/env/lib/python3.7/site-packages/pydantic
python version: 3.7.2 (default, Dec 27 2018, 07:35:06) [Clang 10.0.0 (clang-1000.11.45.5)]
platform: Darwin-19.3.0-x86_64-i386-64bit
optional deps. installed: ['typing-extensions']