lemi57ssss
lemi57ssss

Reputation: 1380

Is a string acceptable as an int within type Callable?

I would like to know if I have misunderstood the use of Callable.

def func(f1:int, f2:int, s:str) -> bool:
    return isinstance(f2, int)

def func2(fn:Callable[[int, int, str], bool]):
    print(fn(42.42, '42.42', 'hello mum'))  # Incorrectly passed?

func2(func)

I expected my PyCharm IDE to flag the print statement line as a type failure because it supplied (float, str, str) instead of the required (int, int, str). It did not. However, in the following code the func of func2(func) is flagged as a type error.

def func(f1:int, f2:str, s:str) -> bool:
    return isinstance(f2, int)

def func2(fn:Callable[[int, int, str], bool]):
    print(fn(42, 42, 'hello mum'))

func2(func)  # Correctly flagged as a type error

Upvotes: 2

Views: 72

Answers (1)

poke
poke

Reputation: 387825

This seems to be an error or missing functionality with PyCharm. If you use mypy, you correctly get the expected type errors:

test.py: note: In function "func2":
test.py:7: error: Argument 1 has incompatible type "float"; expected "int"
test.py:7: error: Argument 2 has incompatible type "str"; expected "int"

Upvotes: 2

Related Questions