Python 枚举元数据使打字模块崩溃

Bin*_*gga 8 python enums typing

我一直在思考这个问题,我似乎无法找到解决问题的方法。我使用枚举来管理我在 Flask 服务器中的访问。短篇小说如果查询不存在的枚举值,我需要枚举返回默认值。首先,我为枚举创建了一个元类:

class AuthAccessMeta(enum.EnumMeta):
def __getattr__(self, item):
    try:
        return super().__getattr__(item)
    except Exception as _:
        if self == AuthAccess and item not in ['_subs_tree']:
            Loggers.SYS.warn('Access {} doesn\'t exist, substituting with MISSING.'.format(item))
            return AuthAccess.MISSING
@unique
class AuthAccess(str, AutoName, metaclass=AuthAccessMeta):
    ...
Run Code Online (Sandbox Code Playgroud)

您可以看到我排除了 _subs_tree 属性,因为 EnumMeta 或 Enum 都没有。我发现这个方法的唯一地方是在打字模块中。然后我在其他地方用 AuthAccess 输入一个参数,它给了我这个奇怪的错误:

C:\Users\[USER]\AppData\Local\Programs\Python\Python36\python.exe -m src.main
[SYS][INFO][11:18:54]: Instance 76cb0042196d4a75b3794ce0b9c1590c is running on project 'local/project1'
Traceback (most recent call last):
  File "C:\Users\[USER]\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Users\[USER]\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\[USER]\Documents\code\sollumcloudplatform\src\main.py", line 19, in <module>
    from src.procedures import create_app
  File "C:\Users\[USER]\Documents\code\sollumcloudplatform\src\procedures.py", line 191, in <module>
    def satisfy_role(role: {}, access_need: Tuple[List[AuthAccess]]) -> bool:
  File "C:\Users\[USER]\AppData\Local\Programs\Python\Python36\lib\typing.py", line 626, in inner
    return func(*args, **kwds)
  File "C:\Users\[USER]\AppData\Local\Programs\Python\Python36\lib\typing.py", line 1062, in __getitem__
    orig_bases=self.__orig_bases__)
  File "C:\Users\[USER]\AppData\Local\Programs\Python\Python36\lib\typing.py", line 965, in __new__
    self.__tree_hash__ = hash(self._subs_tree()) if origin else hash((self.__name__,))
  File "C:\Users\[USER]\AppData\Local\Programs\Python\Python36\lib\typing.py", line 1007, in _subs_tree
    tree_args = _subs_tree(self, tvars, args)
  File "C:\Users\[USER]\AppData\Local\Programs\Python\Python36\lib\typing.py", line 548, in _subs_tree
    tree_args.append(_replace_arg(arg, tvars, args))
  File "C:\Users\[USER]\AppData\Local\Programs\Python\Python36\lib\typing.py", line 517, in _replace_arg
    return arg._subs_tree(tvars, args)
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud)

我试过从输入模块返回该方法,但 Python 告诉我它也不存在。我使用元类错了吗?我应该删除参数上的输入吗?

Eth*_*man 8

可以使用正确版本的enum.

我怀疑您现在遇到的问题是因为在您的except分支中,如果if失败,您不会返回值,也不会引发异常- 所以None会返回。

class AuthAccessMeta(enum.EnumMeta):
def __getattr__(self, item):
    try:
        return super().__getattr__(item)
    except Exception as _:
        if self == AuthAccess and item not in ['_subs_tree']:
            Loggers.SYS.warn('Access {} doesn\'t exist, substituting with MISSING.'.format(item))
            return AuthAccess.MISSING
        # need something here, like simply reraising the exception
        raise
Run Code Online (Sandbox Code Playgroud)