PaddlePaddle/ERNIE-SDK

erniebot config cause pytest KeyError: '__wrapped__'

guyskk opened this issue · 2 comments

guyskk commented

运行 pytest doctest 时,erniebot config 模块会导致下面的报错。

../../.pyenv/versions/3.9.11/lib/python3.9/doctest.py:939: in find
    self._find(tests, obj, name, module, source_lines, globs, {})
../../.pyenv/versions/3.9.11/envs/easyai/lib/python3.9/site-packages/_pytest/doctest.py:536: in _find
    super()._find(  # type:ignore[misc]
../../.pyenv/versions/3.9.11/lib/python3.9/doctest.py:998: in _find
    if ((inspect.isroutine(inspect.unwrap(val))
../../.pyenv/versions/3.9.11/envs/easyai/lib/python3.9/site-packages/_pytest/doctest.py:478: in _mock_aware_unwrap
    return real_unwrap(func, stop=_is_mocked)
../../.pyenv/versions/3.9.11/lib/python3.9/inspect.py:521: in unwrap
    while _is_wrapper(func):
../../.pyenv/versions/3.9.11/lib/python3.9/inspect.py:515: in _is_wrapper
    return hasattr(f, '__wrapped__') and not stop(f)
../../.pyenv/versions/3.9.11/envs/easyai/lib/python3.9/site-packages/erniebot/__init__.py:45: in __getattr__
    return GlobalConfig().get_value(name)
../../.pyenv/versions/3.9.11/envs/easyai/lib/python3.9/site-packages/erniebot/config.py:42: in get_value
    cfg = self._cfg_dict[key]
E   KeyError: '__wrapped__'
============================================================== warnings summary ===============================================================
../../.pyenv/versions/3.9.11/envs/easyai/lib/python3.9/site-packages/_pytest/doctest.py:482
  /Users/kk/.pyenv/versions/3.9.11/envs/easyai/lib/python3.9/site-packages/_pytest/doctest.py:482: PytestWarning: Got KeyError('__wrapped__') when unwrapping <module 'erniebot' from '/Users/kk/.pyenv/versions/3.9.11/envs/easyai/lib/python3.9/site-packages/erniebot/__init__.py'>.  This is usually caused by a violation of Python's object protocol; see e.g. https://github.com/pytest-dev/pytest/issues/5080
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html

config.py 模块 __getattr__ 函数需要将 KeyError 转成 AttributeError 再抛出,才能正确处理key不存在的情况。修复如下:

def __getattr__(name):
    try:
        return GlobalConfig().get_value(name)
    except KeyError as ex:
        raise AttributeError(name) from ex

@guyskk 感谢反馈,麻烦 @Bobholamovic 看下

感谢反馈。我已经在 #51 修改~