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.
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:
As I implemented the Mypy change, I double-checked that I did not break something. But it seems the definition of
MethodTypein typeshed is inconsistent. Its__init__method acceptsCallable(#1499), but its__func__attribute returnsFunctionType(#1383). AsMethodTypecan accept and return other methods at runtime, it seems right to define the return type asCallable, too.