mypy "is not valid as a type" for types constructed with `type()`

Viewed 6194

mypy complains error: Variable "packagename.Foo" is not valid as a type

Foo = type('Foo', (), {})
Bar = Optional[Foo]

This error can be fixed by defining the type as a class:

class Foo:
    pass

Bar = Optional[Foo]

Is there any other way around this? I need to keep the type definition dynamic.

2 Answers

How about this, as a workaround?

from typing import Optional, TYPE_CHECKING

if TYPE_CHECKING:
    class Foo: pass
else:
    Foo = type('Foo', (), {})
    
Bar = Optional[Foo]

typing.TYPE_CHECKING is a constant that will always be True at compile-time, and will always be False at runtime. In this way, we can keep MyPy happy by only telling it about the static definition, but at runtime we can be as dynamic as we like.

You should be aware, though, that this is very much a workaround rather than a solution. By doing it this way, we are essentially lying to the type-checker about the true definition of Foo. This means that MyPy may fail to spot errors in some places, and may raise errors where none exist in other places. Dynamically constructing types at runtime is very useful in some situations, but goes against some fundamental principles of type-checking in Python, so you'll have trouble getting a type-checker to approve what you're doing without some kind of a hack.

Will this work for you?

from typing import TypeVar, Optional

Foo = TypeVar('Foo')
Bar = Optional[Foo]
Related