Skip to content

MethodType uses FunctionLike types inconsistently #11200

@tyralla

Description

@tyralla

Since version 1.8, Mypy does not intersect types in isinstance checks if at least one is final. In my code base, this leads to unexpected unreachable warnings. I could narrow them down to:

from types import MethodType

m: MethodType
if isinstance(m.__func__, MethodType):  # error: Subclass of "FunctionType" and "MethodType" cannot exist: "FunctionType" is final  [unreachable] \
                                        # error: Subclass of "FunctionType" and "MethodType" cannot exist: "MethodType" is final  [unreachable]
    m = m.__func__  # error: Statement is unreachable  [unreachable]

As I implemented the Mypy change, I double-checked that I did not break something. But it seems the definition of MethodType in typeshed is inconsistent. Its __init__ method accepts Callable (#1499), but its __func__ attribute returns FunctionType (#1383). As MethodType can accept and return other methods at runtime, it seems right to define the return type as Callable, too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions