Sphinx의 autodoc을 사용하여 클래스의 __init __ (self) 메서드를 문서화하는 방법은 무엇입니까?
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 프로젝트에 대한 콜백의 여러 변형을 작성했으며 API 문서에 표시했습니다 (결국 이러한 "특수 메소드"는 API의 일부이며 더 나은 위치 특수 메서드의 독 스트링 내부보다 문서화).__init__()
__enter__()
__exit__()
최근에 저는 최고의 구현을 가져와 제 Python 프로젝트 중 하나의 일부로 만들었습니다 ( 여기에 문서가 있습니다 ). 구현은 기본적으로 다음과 같습니다.
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
Yes, there's more documentation than logic :-). The advantage of defining an autodoc-skip-member
callback like this over the use of the special-members
option (for me) is that the special-members
option also enables documentation of properties like __weakref__
(available on all new-style classes, AFAIK) which I consider noise and not useful at all. The callback approach avoids this (because it only works on functions/methods and ignores other attributes).
'developer tip' 카테고리의 다른 글
글꼴 크기 변경 단축키 (0) | 2020.09.15 |
---|---|
C # : 개체 목록을 해당 개체의 단일 속성 목록으로 변환하는 방법은 무엇입니까? (0) | 2020.09.14 |
Eclipse에 포함 된 외부 라이브러리로 jar를 만드는 방법은 무엇입니까? (0) | 2020.09.14 |
Swift에서 뷰 컨트롤러와 다른 객체간에 데이터를 어떻게 공유합니까? (0) | 2020.09.14 |
.json ()이 프라 미스를 반환하는 이유는 무엇입니까? (0) | 2020.09.14 |