Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mypy: got "function", expected "Callable[..., Any]"

import glob
from typing import Callable, List


def fun1(b: str) -> List[str]:
    return ["a", "b"] + [b]


def fun(a: str) -> Callable:
    return fun1 if a == "hello" else glob.glob

Running mypy on this file gives:

error: Incompatible return value type (got "function", expected "Callable[..., Any]")  [return-value]

but I thought a function was a Callable? This seems specific to glob.glob since if I simply return fun1 then the error goes away.

EDIT:

Opened an issue on github.

like image 651
duff18 Avatar asked Aug 14 '26 08:08

duff18


1 Answers

I think it is a mypy limit with if else ternary operator, this indeed seems to work:

import glob
from typing import Callable, List


def fun1(b: str) -> List[str]:
    return ["a", "b"] + [b]


def fun(a: str) -> Callable:
    if a == "hello":
        return fun1
    else:
        return glob.glob
like image 96
hussic Avatar answered Aug 16 '26 06:08

hussic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!