TypeError: 'ABCMeta' object is not subscriptable on Callable

Viewed 2894

I have an issue with the way python 3.8 and mypy work together. Consider this code:

from collections.abc import Callable

def feeder(get_next_item: Callable[[], str]) -> str:
    return get_next_item()

While mypy approves this code, running it using python 3.8 creates an error

Traceback (most recent call last):
  File "mypytest.py", line 3, in <module>
    def feeder(get_next_item: Callable[[], str]) -> str:
TypeError: 'ABCMeta' object is not subscriptable

So, Lets try something else: from collections.abc import Callable

def feeder(get_next_item: Callable) -> str:
    return get_next_item()

At this point, python3.8 will run it fine, but mypy will say

mypytest.py:4: error: Returning Any from function declared to return "str"

Is there any way out of this trap, except using #type: ignore on the return? Thanks in advance

1 Answers

Thanks XtianP. Just for the record, this code worked well for me

from typing import Callable

def feeder(get_next_item: Callable[[], str]) -> str:
    return get_next_item()

print( feeder(lambda : 'Hi man') )
Related