Skip to content

inspect.isfunction and inspect.ismethod #10117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mristin opened this issue Feb 19, 2021 · 6 comments
Closed

inspect.isfunction and inspect.ismethod #10117

mristin opened this issue Feb 19, 2021 · 6 comments
Labels
bug mypy got something wrong

Comments

@mristin
Copy link

mristin commented Feb 19, 2021

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
@mristin mristin added the bug mypy got something wrong label Feb 19, 2021
@mristin
Copy link
Author

mristin commented Feb 19, 2021

(I am not sure if this is a bug or a feature request, please change as you see fit.)

@JelleZijlstra
Copy link
Member

This should be fixable in typeshed once we have support for PEP 647 (https://www.python.org/dev/peps/pep-0647/). Mypy could also special-case these functions directly but that might be overkill.

@mristin
Copy link
Author

mristin commented Feb 20, 2021

@JelleZijlstra thanks for considering the issue. In fact, I think that mypy should handle inspect.is* methods directly as this seems a canonical way to check whether an object is a callable, a coroutine etc. at runtime.

Otherwise, it would be great if mypy documentation explicitly stated how the user is expected to check for "is a callable" property. (There is no isinstance(obj, Callable) as far as I am aware.)

Edit: typo

@JelleZijlstra
Copy link
Member

We support using callable() (as of #2627).

@AlexWaygood
Copy link
Member

AlexWaygood commented Apr 1, 2022

Hmm, well, TypeGuard is now supported and the relevant changes to typeshed have been made, but there still seem to be some problems on mypy's end:

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)  # Revealed type is "<nothing>"
    else:
        reveal_type(error)  # Revealed type is "Union[def (*Any, **Any) -> builtins.BaseException, Type[builtins.BaseException]]"

@erictraut
Copy link

It looks like this is working as expected with the latest version of mypy.

The first revealed type is now:

Revealed type is "Union[types.FunctionType, types.MethodType]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

4 participants