-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Comments
(I am not sure if this is a bug or a feature request, please change as you see fit.) |
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. |
@JelleZijlstra thanks for considering the issue. In fact, I think that mypy should handle 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 Edit: typo |
We support using |
Hmm, well, 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]]" |
It looks like this is working as expected with the latest version of mypy. The first revealed type is now:
|
Uh oh!
There was an error while loading. Please reload this page.
Bug Report
I use an union between a callable and a class:
Mypy does not infer that the type is
Callable
based oninspect.isfunction
andinspect.ismethod
, but still assumes the union:To Reproduce
Execute the following command:
Your Environment
The text was updated successfully, but these errors were encountered: