from typing import Type, Any
def generate_str(cls: Type) -> Type:
"""
Class decorator that auto generates __str__().
:param cls: class to modify
:return: modified class
"""
def __str__(self) -> str:
return _pretty_print_obj(self)
cls.__str__ = __str__
return cls
Mypy 0.521 on Python 3.6.2 emits this warning:
error: Incompatible types in assignment (expression has type Callable[[Any], str], variable has type Callable[[], str])
This may be related to https://github.com/python/mypy/issues/2427, mypy sometimes has problems distinguishing bound and unbound methods. In the meantime you can just put # type: ignore there.
I have the following decorator:
Mypy 0.521 on Python 3.6.2 emits this warning: