docstring !!
!1
docstring
• Python
2
def f(arg1):
""" """
pass
• @cocodrips
• 

→ 

→MLOps
• :

PyDataTokyo 

3
1. docstring
2. docstring
3. docstring
. DocString
docstring
• Python
5
def f(arg1):
""" """
pass
docstring
6
>>> help(f)
Help on function f:
f(arg1)
• docstring help 

docstring
• Jupyter Notebook ? 

help
7
Docstring
•
• 

• type 

IDE (IDE )
•
8
9
docstring
11
def func(arg1, arg2):
“”" 1
0
:param arg1: arg1
:type arg1: int #arg1
:param arg2: arg2
:type arg2: str #arg2
:rtype: bool #
:return:
"""
12
•
def func(arg1, arg2):
“”” ””"
13
• TypeHint
def func(arg1: int, arg2: str) -> bool:
"""
:param arg1: arg1
:param arg2: arg2
:return:
"""
14
• module(.py ) docstring -
"""
"""
• Class docstring
class Hello:
"""
"""
docstring
3
• ReStructuredText
• NumPy Style
• Google Style
15
ReStructuredText(reST)
16
def func(arg1, arg2):
"""
:param arg1: 1
:type arg1: 1
:param arg2: 2
:type arg2: 2
:return:
:rtype:
"""
X
def func(arg1: , arg2: ): ->
"""
:param arg1: 1
:param arg2: 2
:return:
"""
ReStructuredText - TypeHints
NumPy style
17
def func(arg1, arg2):
"""
Parameters
----------
arg1 : 1
1
arg2 : 2
2
Returns
-------
"""
• 

•
NumPy style - TypeHints
X
def func(arg1: , arg2: ) -> :
"""
Parameters
----------
arg1 :
1
arg2 :
2
Returns
-------
"""
Google Style
18
def func(arg1, arg2):
"""
Args:
arg1 ( 1 ): 1
arg2 ( 2 ): 2
Returns:
:
"""
19
• ReStructuredText
•
• NumPy Style
•
• Google Style
•
20
• ReStructuredText
•
• NumPy Style
•
• Google Style
•
TypeHints
21
• ReStructuredText
•
• NumPy Style
•
• Google Style
•
TypeHints
22
def func(arg1: , arg2: ): ->
"""
:param arg1: 1
:param arg2: 2
:return:
"""
ReStructuredText - TypeHints
PyCharm:
23
VS Code: autoDocsring
24
docstring
Sphinx
26
•知的で美しいドキュメント 

( )
•
^ω^
27
class Person:
""" """
def __init__(self, age: int, name: str):
self.age = age
self.name = name
def rest_doc_sample(age: int, name: str) -> Person:
"""Person
:param age:
:param name:
:return: Person
"""
# ...
return Person(age, name)
28
sample_package/simple_rest.py
$ pip install Sphinx sphinx-autodoc-typehints
# 依存ライブラリのインストール
$ python setup.py install
X
sphinx-apidoc import 



sphinx-apidoc
29
# 設定ファイルを作る & ドキュメントの雛形を生成
$ sphinx-apidoc -F -a -o ./doc ./sample_package
doc
├── Makefile
├── _build
├── _static
├── _templates
├── conf.py
├── index.rst
設定ファイル
• Extensionsに以下を追加
• sphinx_autodoc_typehints
• テーマを変更
• sphinx_rtd_theme
• Numpy/Google Styleを

採用する場合は拡張が必要
30
$ cd doc
$ make html
$ open _build/html/index.html
!31
…
• Quick Start
•
•
33


34
: DocString
DocString
• 

=> TypeHints + reST
• CI branch 

GitHub Pages
• docstring PR 

↑↑↑↑↑↑ ↑↑↑↑↑↑
36
DocString
• 

=> TypeHints + reST
• CI branch 

GitHub Pages
• docstring PR 

↑↑↑↑↑↑ ↑↑↑↑↑↑
37
doc-cov
Pull request
38
# CI内で以下を実行
$ doccov <package> -fmc --output csv > doccov.csv
$ doccov-report doccov.csv
Pull Request / Issue

39
$ pip install doc-cov

チームメイトのためにdocstringを書こう! pyconjp2019