python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.5k stars 2.83k forks source link

Wrong warning when assigning __str__ #3951

Open nicoulaj opened 7 years ago

nicoulaj commented 7 years ago

I have the following decorator:

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])
ilevkivskyi commented 7 years ago

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.