Skip to content

inspect.isfunction and inspect.ismethod #10117

Closed
@mristin

Description

@mristin

Bug Report

I use an union between a callable and a class:

# example.py

import inspect
from typing import Union, Callable, Type


def some_func(
        error: Union[Callable[..., BaseException], Type[BaseException]]
)->None:
    if (inspect.isfunction(error) or inspect.ismethod(error)):
        reveal_type(error)
    else:
        reveal_type(error)

Mypy does not infer that the type is Callable based on inspect.isfunction and inspect.ismethod, but still assumes the union:

example.py:9: note: Revealed type is 'Union[def (*Any, **Any) -> builtins.BaseException, Type[builtins.BaseException]]'
example.py:11: note: Revealed type is 'Union[def (*Any, **Any) -> builtins.BaseException, Type[builtins.BaseException]]'

To Reproduce

Execute the following command:

mypy example.py

Your Environment

  • Mypy version used: 0.812

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions