TypeError: type 'range' is not an acceptable base type

488 Views Asked by At

Could you please tell me what is wrong with this code?

from multimethod import multimethod
from typing import Iterable

@multimethod
def foo(x: Iterable[Iterable]):
    print(x)

@multimethod
def foo(x):
    print(x)

foo([range(1), range(2)])

The error I get is:

TypeError: type 'range' is not an acceptable base type

Interestingly, these snippets do work:

from multimethod import multimethod

@multimethod
def foo(x):
    print(x)

foo([range(1), range(2)])

or

from multimethod import multimethod
from typing import Iterable

@multimethod
def foo(x: Iterable):
    print(x)

@multimethod
def foo(x):
    print(x)

foo([range(1), range(2)])

As a little bit of context, what I am trying to do is build a hierarchy and dispatch different functions depending on what types an Iterable holds:

from multimethod import multimethod
from typing import Generator, Iterable
import string

def gen(stop):
    i = 0
    for c in string.ascii_lowercase:
        if i < stop:
            i += 1
            yield c

@multimethod
def bar(x: Iterable[range]):
    print('bar(x: Iterable[range]')

@multimethod
def bar(x: Iterable[Generator]):
    print('bar(x: Generator)')

@multimethod
def bar(x: Iterable[Iterable]):
    print('bar(x: Iterable)')


#bar([range(1), range(2)])
bar([gen(2), gen(3)])
bar([[0, 1, 2], [0, 1, 2, 3]])
0

There are 0 best solutions below