Describe the bug
A parameter of _get_type_of_object can be an instance of types.FunctionType but pyright raises an error.
{
"resource": "{SOME_PATH}/test.py",
"owner": "_generated_diagnostic_collection_name_#0",
"severity": 8,
"message": "Unnecessary isinstance call; \"Union[(*args, **kwargs) -> Any, CustomClass]\" is never instance of \"FunctionType\"",
"source": "Pyright (reportUnnecessaryIsInstance)",
"startLineNumber": 11,
"startColumn": 8,
"endLineNumber": 11,
"endColumn": 40
}
To Reproduce
Expected behavior
Code
from types import FunctionType
from typing import Any, Callable, Union
class CustomClass:
def __call__(self, *args):
pass
def _get_type_of_object(object: Union[Callable[..., Any], CustomClass]):
if isinstance(object, FunctionType):
return "is function"
if callable(object):
return "is callable"
return "nothing"
print(_get_type_of_object(lambda: 1))
# is function
print(_get_type_of_object(CustomClass()))
# is callable
VS Code extension or command-line
VS Code Extension
Additional context
Version 1.1.33
Thanks for the bug report.
I generally don't recommend using isinstance(x, FunctionType) because it doesn't work with built-in functions. The use of callable(x) is preferable.
That said, I agree that this is a false positive error. I'll work on a fix.
I generally don't recommend using
isinstance(x, FunctionType)because it doesn't work with built-in functions. The use ofcallable(x)is preferable.
It is right, but I had to check whether an object is a function object or a callable object. :cry:
Thanks for the quick feedback.
This will be addressed in the next published version of Pyright.
This is now fixed in version 1.1.36, which I just published.
Thank you!!