aenum
aenum copied to clipboard
mypy complains with aenum.Enum, but not enum.Enum
Using Python 3.7 on Debian Buster 10.9 development host (target system is actually python2.7 based system).
The following code snippet gives a mypy error when I use aenum.Enum.
Argument "reg_id" to "register_get" has incompatible type "int"; expected "Register_Address"
No mypy error is reported when useing enum.Enum.
# from enum import Enum
from aenum import Enum
class Register_Address( Enum ):
STATUS = 0x0000
GPIO_CTRL = 0x0004
def register_get(
reg_id, # type: Register_Address
) : # type: (...) -> int
"""Get a 32-bit register value."""
# value = my_ioctl( reg_id, ... )
value = reg_id.value
return value
value = register_get( reg_id=Register_Address.GPIO_CTRL )
mypy ships with a plugin [1] for the stdlib enum module, but it doesn't understand aenum. You could either extend that plugin to support aenum, or use it as the basis for a standalone plugin.
[1] https://github.com/python/mypy/blob/master/mypy/plugins/enums.py
Yes, aenum
with proper type checking would be awesome, or a separate package such as types-aenum
:)
I have worked around it another way by using Python3 enum
for type checking and aenum
for real code.
It's not as good has having the real code type checked, but at least the "noise" has gone.
from typing import TYPE_CHECKING
if TYPE_CHECKING :
#! this is only processed by MyPy (i.e. not at runtime)
from enum import Enum
else :
#! this is real runtime code
from aenum import Enum
Another "ugly" workaround that mypy
understands is to use reg_id=Register_Address(Register_Address.GPIO_CTRL)