Pyright: Unnecessary isinstance call: Union[Callable, Something] is never instance of FunctionType

Created on 21 Apr 2020  路  5Comments  路  Source: microsoft/pyright

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

addressed in next version bug

All 5 comments

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 of callable(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!!

Was this page helpful?
0 / 5 - 0 ratings