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.