【问题标题】:Sphinx autoclass with metaclass not working元类不工作的狮身人面像自动类
【发布时间】:2021-11-05 11:08:46
【问题描述】:

元类:

class RequestMeta(type):
    def __new__(mcs, what, bases, attrs):
        _attrs = {name: decimal_to_string(attr) if callable(attr) else attr for name, attr in attrs.items()}
        return super().__new__(mcs, what, bases, _attrs)

当我们为元类导入 RequestMeta 时,Sphinx 无法生成成员:

Class_name(metaclass=RequestMeta)
    def test
        """
        Get test
        :return:
        .. code-block:: json

            [
                {
                "message":"test",
                "timestamp": 123
                }
            ]
        """

test.rst:

Test
____

.. autoclass:: my_prog.a.Class_name
   :members:

【问题讨论】:

  • 尝试在返回和代码块之间插入一个空行。
  • "blank line" 不会对任何 Python 代码产生任何影响,但对于 linter 警告。 (在这种情况下不是)

标签: python python-sphinx metaclass autodoc


【解决方案1】:

问题可能在于您的decimal_to_string 装饰器。 Sphinx 不会关心元类,但如果在装饰函数时去掉它的属性,它就无能为力了。

检查 decimal_to_string 是否将其目标函数包装在 functools.wraps 中 - 这可能会解决您的问题。

【讨论】:

  • functools.wraps - 工作!!!谢谢!问题出在元类中的@decorator
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-26
  • 1970-01-01
相关资源
最近更新 更多