Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mypy Unsupported type Type[typeVarAlias]

Mypy is returning an error that I do not understand (nor can I recreate in a reduced form). Googling the error is proving difficult (given the question mark).

Does anyone know what this error means? Specifically what the question mark denotes?

Unsupported type Type[typeBasePage?]

The code in question:

typeBasePage = TypeVar("typeBasePage", bound="BasePage")  # any instance subclass of BasePage
typeNavCls = Type[typeBasePage] # error occurs here - trying to make an alias

class Nav:
    def __init__(self, cls: typeNavCls):
        self.cls: typeNavCls = cls

class BasePage(Base):
    ...
    # redacted because it's huge

Again, if I try to recreate the above code in a very reduced form, mypy doesn't error.

typeB = TypeVar('typeB', bound='B')
tAlias = Type[typeB]

class Nav:
    def __init__(self, cls: tAlias):
        self.cls: tAlias = cls

class A: pass
class B(A): pass
like image 230
Marcel Wilson Avatar asked Oct 18 '25 17:10

Marcel Wilson


1 Answers

I believe I found what the question mark is, but am nowhere closer to figuring out why the error occurs in mypy.

https://mypy.readthedocs.io/en/latest/literal_types.html

If you do not provide an explicit type in the Final, the type of c becomes context-sensitive: mypy will basically try “substituting” the original assigned value whenever it’s used before performing type checking. This is why the revealed type of c is Literal[19]?: the question mark at the end reflects this context-sensitive nature.

like image 89
Marcel Wilson Avatar answered Oct 21 '25 07:10

Marcel Wilson