Pythonドメイン¶
Added in version 1.0.
Pythonドメイン(py)では、モジュールの説明のために、次のようなディレクティブを提供しています:
- .. py:module:: name¶
This directive marks the beginning of the description of a module (or package submodule, in which case the name should be fully qualified, including the package name). A description of the module such as the docstring can be placed in the body of the directive.
このディレクティブを使用すると、グローバルなモジュール索引に項目が追加されます。
バージョン 5.2 で変更: Module directives support body content.
options
- :platform: platforms (comma separated list)¶
Indicate platforms which the module is available (if it is available on all platforms, the option should be omitted). The keys are short identifiers; examples that are in use include "IRIX", "Mac", "Windows" and "Unix". It is important to use a key which has already been used when applicable.
- :synopsis: purpose (text)¶
Consist of one sentence describing the module's purpose -- it is currently only used in the Global Module Index.
- :deprecated: (no argument)¶
Mark a module as deprecated; it will be designated as such in various locations then.
- .. py:currentmodule:: name¶
このディレクティブはSphinxに対して、この行以降のクラスや関数などが、指定された与えられたモジュール (
py:module
のように)の中にある、ということを通知します。これを使用しても、索引のエントリーは作成されません。py:mod
へのリンクターゲットも作成されません。このディレクティブは、モジュールに含まれる項目へのドキュメントが様々なファイルやセクションに分割されている場合に便利です。この場合には一カ所だけpy:module
ディレクティブを使用して、他の箇所でpy:currentmodule
を使用するようにします。
モジュールとクラスの中の構成要素を記述するために、次のようなディレクティブが提供されています:
- .. py:function:: name(parameters)¶
- .. py:function:: name[type parameters](parameters)
Describes a module-level function. The signature should include the parameters, together with optional type parameters, as given in the Python function definition, see Pythonシグニチャ. For example:
.. py:function:: Timer.repeat(repeat=3, number=1_000_000) .. py:function:: add[T](a: T, b: T) -> T
メソッドには
py:method
を使用します。説明には一般的に、パラメータに必要な関する情報と、それらがどのように使用されるのか(変更可能なオブジェクトが渡されたときに、変更されるのかどうか)、副作用、投げられる可能性のある例外の情報を含めます。
この情報は 詳細情報フィールドのリスト にあるデータ構造で表現できます(他のpyドメインのディレクティブでも同様です)。
options
- :async: (no value)¶
Indicate the function is an async function.
Added in version 2.1.
- :canonical: (full qualified name including module name)¶
Describe the location where the object is defined if the object is imported from other modules
Added in version 4.0.
- :single-line-parameter-list: (no value)¶
Ensures that the function's arguments will be emitted on a single logical line, overriding
python_maximum_signature_line_length
andmaximum_signature_line_length
.Added in version 7.1.
- :single-line-type-parameter-list: (no value)¶
Ensure that the function's type parameters are emitted on a single logical line, overriding
python_maximum_signature_line_length
andmaximum_signature_line_length
.Added in version 7.1.
- .. py:data:: name¶
モジュール内のグローバルなデータの説明をします。変数も値も"定義された定数"として取り込むことができます。クラスとオブジェクトの属性はこの環境を使用してドキュメントを書くことはできません。
options
- :type: type of the variable (text)¶
Added in version 2.4.
- :value: initial value of the variable (text)¶
Added in version 2.4.
- :canonical: (full qualified name including module name)¶
Describe the location where the object is defined if the object is imported from other modules
Added in version 4.0.
- .. py:exception:: name¶
- .. py:exception:: name(parameters)
- .. py:exception:: name[type parameters](parameters)
Describes an exception class. The signature can, but need not include parentheses with constructor arguments, or may optionally include type parameters (see PEP 695).
options
- :final: (no value)¶
Indicate the class is a final class.
Added in version 3.1.
- :single-line-parameter-list: (no value)¶
See
py:class:single-line-parameter-list
.Added in version 7.1.
- :single-line-type-parameter-list: (no value)¶
See
py:class:single-line-type-parameter-list
.Added in version 7.1.
- .. py:class:: name¶
- .. py:class:: name(parameters)
- .. py:class:: name[type parameters](parameters)
Describes a class. The signature can optionally include type parameters (see PEP 695) or parentheses with parameters which will be shown as the constructor arguments. See also Pythonシグニチャ.
このクラスに属する属性とメソッドのディレクティブはこのディレクティブの本体の中に記述します。このクラスの外に書いた場合は、提供された名前にクラス名が含まれていれば、クロスリファレンスは動作します。サンプル:
.. py:class:: Foo .. py:method:: quux() -- or -- .. py:class:: Bar .. py:method:: Bar.quux()
最初の書き方が推奨です。
options
- :canonical: (full qualified name including module name)¶
Describe the location where the object is defined if the object is imported from other modules
Added in version 4.0.
- :final: (no value)¶
Indicate the class is a final class.
Added in version 3.1.
- :single-line-parameter-list: (no value)¶
Ensures that the class constructor's arguments will be emitted on a single logical line, overriding
python_maximum_signature_line_length
andmaximum_signature_line_length
.Added in version 7.1.
- :single-line-type-parameter-list: (no value)¶
Ensure that the class type parameters are emitted on a single logical line, overriding
python_maximum_signature_line_length
andmaximum_signature_line_length
.
- .. py:attribute:: name¶
オブジェクトの属性のデータの説明をします。この説明には期待されるデータの型、値を直接変更できるかどうか、という情報を含めます。
options
- :type: type of the attribute (text)¶
Added in version 2.4.
- :value: initial value of the attribute (text)¶
Added in version 2.4.
- :canonical: (full qualified name including module name)¶
Describe the location where the object is defined if the object is imported from other modules
Added in version 4.0.
- .. py:property:: name¶
Describes an object property.
Added in version 4.0.
options
- :abstractmethod: (no value)¶
Indicate the property is abstract.
- :classmethod: (no value)¶
Indicate the property is a classmethod.
Added in version 4.2.
- :type: type of the property (text)¶
- .. py:method:: name(parameters)¶
- .. py:method:: name[type parameters](parameters)
オブジェクトのメソッドの説明をします。パラメータからは
self
パラメータははずします。この説明にはfunction
と同じ情報を記述するようにします。 Pythonシグニチャ と 詳細情報フィールドのリスト も参照してください。options
- :abstractmethod: (no value)¶
Indicate the method is an abstract method.
Added in version 2.1.
- :async: (no value)¶
Indicate the method is an async method.
Added in version 2.1.
- :canonical: (full qualified name including module name)¶
Describe the location where the object is defined if the object is imported from other modules
Added in version 4.0.
- :classmethod: (no value)¶
Indicate the method is a class method.
Added in version 2.1.
- :final: (no value)¶
Indicate the method is a final method.
Added in version 3.1.
- :single-line-parameter-list: (no value)¶
Ensures that the method's arguments will be emitted on a single logical line, overriding
python_maximum_signature_line_length
andmaximum_signature_line_length
.Added in version 7.1.
- :single-line-type-parameter-list: (no value)¶
Ensure that the method's type parameters are emitted on a single logical line, overriding
python_maximum_signature_line_length
andmaximum_signature_line_length
.Added in version 7.2.
- :staticmethod: (no value)¶
Indicate the method is a static method.
Added in version 2.1.
- .. py:staticmethod:: name(parameters)¶
- .. py:staticmethod:: name[type parameters](parameters)
py:method
とほぼ一緒ですが、メソッドがスタティックメソッドであるということを表します。Added in version 0.4.
- .. py:classmethod:: name(parameters)¶
- .. py:classmethod:: name[type parameters](parameters)
py:method
とほぼ一緒ですが、メソッドがクラスメソッドであるということを表します。Added in version 0.6.
- .. py:decorator:: name¶
- .. py:decorator:: name(parameters)
- .. py:decorator:: name[type parameters](parameters)
デコレーターを説明します。このシグニチャにはデコレーターの使い方を記述します。例えばこの関数は
def removename(func): func.__name__ = '' return func def setnewname(name): def decorator(func): func.__name__ = name return func return decorator
次のように説明を書くことが出来ます
.. py:decorator:: removename Remove name of the decorated function. .. py:decorator:: setnewname(name) Set name of the decorated function to *name*.
(
.. py:decorator:: removename(func)
とは対象的です)これらに対応する、
py:deco
といったロールはありません。代わりに、py:func
ロールを使用してください。- :single-line-parameter-list: (no value)¶
Ensures that the decorator's arguments will be emitted on a single logical line, overriding
python_maximum_signature_line_length
andmaximum_signature_line_length
.Added in version 7.1.
- :single-line-type-parameter-list: (no value)¶
Ensure that the decorator's type parameters are emitted on a single logical line, overriding
python_maximum_signature_line_length
andmaximum_signature_line_length
.Added in version 7.2.
- .. py:decoratormethod:: name¶
- .. py:decoratormethod:: name(signature)
- .. py:decoratormethod:: name[type parameters](signature)
py:decorator
とほぼ同じですが、対象がメソッドになります。このデコレータを指定したい場合には、
py:meth
ロールを使います。
Pythonシグニチャ¶
Pythonで書くのと同じように、関数、メソッド、クラスコンストラクタのシグニチャを与えることが出来ます。
オプション引数のデフォルト値を与えることもできます。ただし、値にカンマが含まれると、シグニチャのパーサはうまく動作しません。Pythonの3つのスタイルの引数のアノテーションと同様に、返り値の型も記述できます:
.. py:function:: compile(source : string, filename, symbol='file') -> ast object
デフォルト値を持たない省略可能なパラメータを持つ関数(よくある例としてはキーワード引数をサポートしていないC拡張モジュールで実装された関数があります)のため、省略可能な部分を角かっこで指定することも出来ます。
- compile(source[, filename[, symbol]])¶
通常、カンマの前に開き括弧を付けます。
Python 3.12 introduced type parameters, which are type variables declared directly within the class or function definition:
class AnimalList[AnimalT](list[AnimalT]):
...
def add[T](a: T, b: T) -> T:
return a + b
The corresponding reStructuredText documentation would be:
.. py:class:: AnimalList[AnimalT]
.. py:function:: add[T](a: T, b: T) -> T
See PEP 695 and PEP 696 for details and the full specification.
詳細情報フィールドのリスト¶
Added in version 0.4.
バージョン 3.0 で変更: meta fields are added.
Pythonのオブジェクト説明のためのディレクティブの内側には、適切に情報が明示されて、決まったルールに従ったreSTフィールドを配置できます:
param
,parameter
,arg
,argument
,key
,keyword
: 引数の説明です。type
: 引数の型です。可能ならリンクを生成します。raises
,raise
,except
,exception
: この中から投げられる例外(いつ投げられるか?)を定義しますvar
,ivar
,cvar
: 変数の説明をしますvartype
: 変数の型です。可能ならリンクを生成します。returns
,return
: 返り値の値について説明をしますrtype
: 返り値の型です。可能ならリンクを生成します。meta
: Add metadata to description of the python object. The metadata will not be shown on output document. For example,:meta private:
indicates the python object is private member. It is used insphinx.ext.autodoc
for filtering members.
注釈
In current release, all var
, ivar
and cvar
are represented as
"Variable". There is no difference at all.
フィールドは、 return
, rtype
以外の場合は、上記のキーワードのうち、どれかと、引数を一つが引数として設定されています。 return
, rtype
だけは引数を取りません。サンプルを見ていただくのが一番でしょう:
.. py:function:: send_message(sender, recipient, message_body, [priority=1])
Send a message to a recipient
:param str sender: The person sending the message
:param str recipient: The recipient of the message
:param str message_body: The body of the message
:param priority: The priority of the message, can be a number 1-5
:type priority: integer or None
:return: the message id
:rtype: int
:raises ValueError: if the message_body exceeds 160 characters
:raises TypeError: if the message_body is not a basestring
これは次のようにレンダリングされます:
- send_message(sender, recipient, message_body[, priority=1])¶
Send a message to a recipient
- パラメータ:
- 戻り値:
the message id
- 戻り値の型:
- 例外:
ValueError -- if the message_body exceeds 160 characters
TypeError -- if the message_body is not a basestring
型情報が一語で表せる場合には、属性の型と説明をひとつにまとめることもできます:
:param int priority: The priority of the message, can be a number 1-5
Added in version 1.5.
Container types such as lists and dictionaries can be linked automatically using the following syntax:
:type priorities: list(int)
:type priorities: list[int]
:type mapping: dict(str, int)
:type mapping: dict[str, int]
:type point: tuple(float, float)
:type point: tuple[float, float]
Multiple types in a type field will be linked automatically if separated by the word "or":
:type an_arg: int or None
:vartype a_var: str or int
:rtype: float or str
Pythonオブジェクトのクロススリファンレス¶
以下のロールを使用すると、モジュール内のオブジェクトを参照できます。一致する識別子が見つかれば、ハイパーリンクが作成されます:
- :py:mod:¶
モジュールへの参照です。ドットで区切られた名前も使用できます。これはパッケージ名としても利用可能です。
- :py:func:¶
Pythonの関数への参照です。ドットで区切られた名前も使用できます。ロールのテキストは読みやすさのために括弧を後ろに含める必要はありません。
add_function_parentheses
設定値を ``True``(デフォルト)にしておくと、Sphinxが自動で括弧を追加します。
- :py:data:¶
モジュール変数を参照します。
- :py:const:¶
「定義済み」定数です。変更して欲しくないPythonの変数にも使えます。
- :py:class:¶
クラス名です。ドットで区切られた名前も使用できます。
- :py:meth:¶
オブジェクトのメソッドへの参照です。ロールのテキストには型名とメソッド名を含めなければなりません。ただし、型の記述中に書く場合には省略もできます。ドットで区切られた名前も使用できます。
- :py:attr:¶
オブジェクトの属性への参照です。
注釈
The role is also able to refer to property.
- :py:exc:¶
例外への参照です。ドットで区切られた名前も使用できます。
- :py:obj:¶
型が指定されていないオブジェクトの名前です。
default_role
一緒に使用すると便利です。Added in version 0.4.
このマークアップの中の名前には、モジュール名, クラス名なども含めることができます。例えば、 :py:func:`filter`
は現在のモジュールに定義されている filter
という名前の関数か、その名前を持つ組み込み関数をあらわします。 :py:func:`foo.filter`
と明示的に書くと、 foo
モジュールの中の filter
関数を表します。
通常、これらのロールで使用される名前は、最初は修飾子なしで検索されます。次に現在のモジュール名を前に付けて検索されます。その次に現在のモジュール名とクラス名(あれば)を付けて検索されます。もし、ドットが先頭についた名前が指定された場合には、この探索順は逆になります。例えば、 codecs
というPythonモジュールの定義の中で :py:func:`open`
が定義されると、常に組み込み関数を参照しますが、 :py:func:`.open`
と書かれると、 codecs.open()
を参照するようになります。
属性名が、現在のクラスのものかどうかを決定するのにも、同様の名前検索の仕組みが使用されます。
Also, if the name is prefixed with a dot, and no exact match is found, the
target is taken as a suffix and all object names with that suffix are searched.
For example, :py:meth:`.TarFile.close`
references the
tarfile.TarFile.close()
function, even if the current module is not
tarfile
. Since this can get ambiguous, if there is more than one possible
match, you will get a warning from Sphinx.
~
と .
をオブジェクトの識別子の前に組み合わせることができます。 :py:meth:`~.TarFile.close`
と指定されると、 tarfile.TarFile.close()
が参照されますが、実際に文章中に表示されるのは、 close()
となります。