Skip to content

inspect.isfunction and inspect.ismethod #10117

Closed

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

Activity

  1. mristin commented on Feb 19, 2021

    @mristin
    Author

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

  2. JelleZijlstra commented on Feb 19, 2021

    @JelleZijlstra
    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.

  3. mristin commented on Feb 20, 2021

    @mristin
    Author

    @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

  4. JelleZijlstra commented on Feb 20, 2021

    @JelleZijlstra
    Member

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

  5. AlexWaygood commented on Apr 1, 2022

    @AlexWaygood
    Member

    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]]"
  6. erictraut commented on Aug 17, 2023

    @erictraut

    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

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