Skip to content

Wrong warning when assigning __str__ #3951

@nicoulaj

Description

@nicoulaj

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])

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-descriptorsProperties, class vs. instance attributestopic-runtime-semanticsmypy doesn't model runtime semantics correctly

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions