mypy icon indicating copy to clipboard operation
mypy copied to clipboard

"Callable | Callable" not callable [misc]

Open jolaf opened this issue 1 year ago • 3 comments

The following code:

from collections.abc import Callable, Sequence
from functools import partial, reduce
from typing import cast, Any

CA = Callable[[Any], Any]
CB = Callable[[Callable[..., Any], Any], Any]

def f(s: Sequence[CA | CB]) -> CA:
    return cast(CA, reduce(lambda a, b: partial(b, a), s))

produces the following output:

test.py: note: In function "f":
test.py:9:41: error: "Callable[[Any], Any] | Callable[[Callable[..., Any], Any], Any]" not callable  [misc]
        return cast(CA, reduce(lambda a, b: partial(b, a), s))
                                            ^

This looks like a false positive to me.

$ mypy --version
mypy 1.11.1 (compiled: yes)

$ python --version
Python 3.12.3

$ lsb_release -d
No LSB modules are available.
Description:	Ubuntu 24.04 LTS

jolaf avatar Aug 03 '24 18:08 jolaf

This is the same as many other issues, where mypy computes join between callables to builtins.function. In this case join of [def (Any) -> Any and def (def (*Any, **Any) -> Any, Any) -> Any]. Happens to shows up in 1.11 because we now type check functools.partial more closely, but underlying issue is old. Seems tricky to turn the callsite in extract_callable_type to return a union

hauntsaninja avatar Aug 09 '24 09:08 hauntsaninja

I've taken on the challenge of fixing this. It's my first time working with mypy, so bear with me or let me know if someone else fixes it.

johnslavik avatar Aug 09 '24 17:08 johnslavik

I've taken on the challenge of fixing this. It's my first time working with mypy, so bear with me or let me know if someone else fixes it.

Thanks!!

jolaf avatar Aug 09 '24 17:08 jolaf

This was fixed in #17903

hauntsaninja avatar Jan 01 '25 22:01 hauntsaninja