我在这方面一直不知所措,似乎找不到解决问题的办法。我使用枚举来管理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):
...
您可以看到我排除了_subs_tree属性,因为EnumMeta或Enum都没有。我发现此方法的唯一位置是在键入模块中。然后我在其他地方用AuthAcess键入一个参数,这给了我这个奇怪的错误:
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
我尝试过从类型模块返回方法,但是Python告诉我它也不存在。我使用的是元类错误吗?我应该删除参数的类型吗?