I am writing a class that represents a hashable dictionary in Python, following what has been suggested in here: hashable dict. I am doing this because I need it to be hashable for other implementations.
So I basically created my nice HashableDict
class:
class HashableBinnerDict(dict[KEY, VALUE]):
"""
Class that represents a hashable dict.
"""
def __hash__(self) -> int:
return hash(frozenset(self))
It inherits from dict and KEY
and VALUE
are two generic datatypes needed to parametrize the typing of my HashableDict
.
The overall code that leverages such class works perfectly. However, mypy is complaining with this error:
error: Signature of "__hash__" incompatible with supertype "dict" [override]
And I guess it is caused by the fact that inside the "base" class dict
of python there's not implemented hash function, in fact we have (extracted from the dict class in the Python codebase):
class dict(object):
...
... # skipping non relevant code
def __sizeof__(self): # real signature unknown; restored from __doc__
""" D.__sizeof__() -> size of D in memory, in bytes """
pass
__hash__ = None
The hash attribute is defined as None, so I guess mypy is complaining because of that. Any idea how to solve this besides brutally ignoring the error?