I'm wondering which is the correct way of annotating __dict__ property. Dict[str, Any] seem quite obvious and is used multiple times in typeshed, but:
from typing import Any, Dict
class Test:
@property
def __dict__(self) -> Dict[str, Any]:
return {}
% mypy test.py
test.py:5: error: Signature of "__dict__" incompatible with supertype "object"
This appears to be a duplicate of https://github.com/python/mypy/issues/5836. For example see this minimal repro:
class Test:
a = "" # type: str
class B(Test):
@property
def a(self) -> str:
return ""
As a workaround in your case, you can likely do something like:
from typing import Dict, Any, TYPE_CHECKING
class Test:
if TYPE_CHECKING:
__dict__ = {} # type: Dict[str, Any]
else:
@property
def __dict__(self) -> Dict[str, Any]:
return {}
and mypy should be happy.
Most helpful comment
This appears to be a duplicate of https://github.com/python/mypy/issues/5836. For example see this minimal repro:
As a workaround in your case, you can likely do something like:
and mypy should be happy.