Sphinx는 기본적으로 __init __ (self)에 대한 문서를 생성하지 않습니다. 나는 다음을 시도했다 :
.. automodule:: mymodule
:members:
과
..autoclass:: MyClass
:members:
conf.py에서 다음을 설정하면 __init __ (self) docstring이 클래스 docstring에 추가됩니다 ( Sphinx autodoc 문서 는 이것이 예상되는 동작이라는 데 동의하는 것처럼 보이지만 해결하려는 문제에 대해서는 언급하지 않습니다).
autoclass_content = 'both'
답변
다음은 세 가지 대안입니다.
-
__init__()
항상 문서화 되도록하려면autodoc-skip-member
conf.py에서 사용할 수 있습니다 . 이렇게 :def skip(app, what, name, obj, would_skip, options): if name == "__init__": return False return would_skip def setup(app): app.connect("autodoc-skip-member", skip)
이것은
__init__
생략되지 않도록 명시 적으로 정의합니다 (기본값). 이 구성은 한 번 지정되며 .rst 소스의 모든 클래스에 대해 추가 마크 업이 필요하지 않습니다. -
이
special-members
옵션은 Sphinx 1.1 에 추가되었습니다 . 그것은 “특별한”회원 (같은 이름을 가진 사람들)을 만든다.__special__
)가 autodoc에 의해 문서화됩니다.Sphinx 1.2부터이 옵션은 이전보다 더 유용하게 만드는 인수를 사용합니다.
-
사용
automethod
:.. autoclass:: MyClass :members: .. automethod:: __init__
이것은 모든 클래스에 대해 추가되어야합니다 (
automodule
이 답변의 첫 번째 개정판에 대한 주석에서 지적했듯이 와 함께 사용할 수 없음 ).
답변
당신은 가까웠습니다. 파일 에서 autoclass_content
옵션을 사용할 수 있습니다 conf.py
.
autoclass_content = 'both'
답변
지난 몇 년 동안 저는 , 및 autodoc-skip-member
같은 메서드를 원했기 때문에 관련없는 다양한 Python 프로젝트에 대해 여러 가지 콜백 변형을 작성했습니다.__init__()
__enter__()
__exit__()
API 문서에 표시했습니다 (결국 이러한 “특수 메소드”는 API의 일부이며 더 나은 위치 특수 메서드의 독 스트링 내부보다 문서화).
최근에 저는 최고의 구현을 가져와 제 Python 프로젝트 중 하나의 일부로 만들었습니다 ( 여기에 문서가 있습니다 ). 구현은 기본적으로 다음과 같습니다.
import types
def setup(app):
"""Enable Sphinx customizations."""
enable_special_methods(app)
def enable_special_methods(app):
"""
Enable documenting "special methods" using the autodoc_ extension.
:param app: The Sphinx application object.
This function connects the :func:`special_methods_callback()` function to
``autodoc-skip-member`` events.
.. _autodoc: http://www.sphinx-doc.org/en/stable/ext/autodoc.html
"""
app.connect('autodoc-skip-member', special_methods_callback)
def special_methods_callback(app, what, name, obj, skip, options):
"""
Enable documenting "special methods" using the autodoc_ extension.
Refer to :func:`enable_special_methods()` to enable the use of this
function (you probably don't want to call
:func:`special_methods_callback()` directly).
This function implements a callback for ``autodoc-skip-member`` events to
include documented "special methods" (method names with two leading and two
trailing underscores) in your documentation. The result is similar to the
use of the ``special-members`` flag with one big difference: Special
methods are included but other types of members are ignored. This means
that attributes like ``__weakref__`` will always be ignored (this was my
main annoyance with the ``special-members`` flag).
The parameters expected by this function are those defined for Sphinx event
callback functions (i.e. I'm not going to document them here :-).
"""
if getattr(obj, '__doc__', None) and isinstance(obj, (types.FunctionType, types.MethodType)):
return False
else:
return skip
예, 논리보다 더 많은 문서가 있습니다. :-). 옵션을 autodoc-skip-member
사용하는 것보다 이와 같은 콜백 을 정의 할 때의 이점 special-members
(나에게)은 special-members
옵션이 __weakref__
노이즈를 고려하고 전혀 유용하지 않은 (모든 새로운 스타일의 클래스, AFAIK에서 사용 가능) 과 같은 속성의 문서화도 가능하게 한다는 것입니다. 콜백 접근 방식은이를 방지합니다 (함수 / 메소드에서만 작동하고 다른 속성을 무시하기 때문).
답변
이것은 이전 게시물이지만 지금 찾고있는 사람들을 위해 버전 1.8에 도입 된 또 다른 솔루션이 있습니다. 문서 에 따르면 special-member
autodoc_default_options 의 키를 conf.py
.
예:
autodoc_default_options = {
'members': True,
'member-order': 'bysource',
'special-members': '__init__',
'undoc-members': True,
'exclude-members': '__weakref__'
}
답변
이것은 __init__
인수가있는 경우 에만 포함하는 변형입니다 .
import inspect
def skip_init_without_args(app, what, name, obj, would_skip, options):
if name == '__init__':
func = getattr(obj, '__init__')
spec = inspect.getfullargspec(func)
return not spec.args and not spec.varargs and not spec.varkw and not spec.kwonlyargs
return would_skip
def setup(app):
app.connect("autodoc-skip-member", skip_init_without_args)
답변
