Skip to content
Advertisement

Pylance: “ClassVar” is not allowed in this context?

With the following function:

from typing import ClassVar 

def overrode_make_stub(cls: ClassVar["Emailer"]) -> bool:
    return not (getattr(cls.make_stub, "_not_overridden", False))

I’m getting this error from Pylance in Visual Studio Code:

“ClassVar” is not allowed in this context

I’m using Python3.9 and Pylance v2021.10.0.

In this example the Email class has a “make_stub” function, which has a _not_overriden attribute set to True. When I inspect a module for subclasses of that class I can use this to filter only the ones that overrode the “make_sub” function.

I can’t find any documentation on this error. Does anyone know why it’s coming up?

Advertisement

Answer

The ClassVar type hint is intended to mark class-level variables to tell the type system that they really are class variables, not defaults for instance variables.

from typing import ClassVar

class Example:
    x: int              # x and y are being type hinted as instance variables
    y: int
    z: ClassVar[int]    # z is being type hinted as a class variable instead

If the argument to your function is supposed to be a class, then you should use typing.Type as the type hint. Try this, to hint that you expect a subclass of Emailer:

from typing import Type

def overrode_make_stub(cls: Type["Emailer"]) -> bool:
    return not (getattr(cls.make_stub, "_not_overridden", False))

Note that if you only need to be compatible with Python 3.9 and later, you don’t actually need to use typing.Type at all. You can instead use the builtin type object, which can be indexed in the same way, thanks to PEP 585: cls: type["Emailer"]

Advertisement