I need to declare global variables that have "complex" type and should not be instantiated at import time. In Python 3.6+, I can omit initialization, for example:
log: logging.Logger
pollset: select.poll
I need to make the code compatible with Python 3.5. I can use comment type annotations:
log = ... # type: logging.Logger
pollset = ... # type: select.poll
but then I have to provide initial value. This is not a problem at runtime, assigning the initial value of None or ... will do. But any of those trigger mypy typecheck error:
myprog.py:19: error: Incompatible types in assignment (expression has type "ellipsis", variable has type "Logger")
Of course I could use Optional type to allow initializing to None, but then type checking would be weakened. For instance, assigning None value to the variable elsewhere in the code is illegal, but it would not be caught.
Is there an accepted way to use strong type checking of a variable in a way that is compatible with Python 3.5?
log: logging.Logger, there is still no variable namedlog.