Skip to content

Structural subtyping: unawareness of special attributes of user-defined functions. #10403

@tyralla

Description

@tyralla

User-defined functions come, among others, with the special attribute __name__. Mypy 0.812 correctly reveals the type str for attribute __name__ of function f in the following example. Still, it states an assignment error when trying to handle f as a subtype of the callback protocol A, which also defines a __name__ attribute of type str.

from typing import Protocol

class A(Protocol):
    __name__: str
    def __call__(self) -> None: ...

def f() -> None: ...

reveal_type(f.__name__)  # note: Revealed type is 'builtins.str'
a: A = f  # error: Incompatible types in assignment (expression has type "Callable[[], None]", variable has type "A")  [assignment]

This problem also occurs for all other special attributes I checked (e.g. __doc__).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions