Skip to content

Commit 227e41d

Browse files
authored
Add configuration to not show args in class signature (#193)
* New option: class_signature If the option `class_signature` is `True`, iit will display the constructor argument list in the class signature. The default value is `False` as this is closer to MATLAB. * Reorder configuration values by importance. * Fixed wording * Wording * Fix failing test.
1 parent bd2364a commit 227e41d

File tree

6 files changed

+80
-50
lines changed

6 files changed

+80
-50
lines changed

README.rst

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,17 @@ Additional Configuration
5858

5959
``matlab_src_dir``
6060
In order for the Sphinx MATLAB domain to auto-document MATLAB source code,
61-
set the config value of ``matlab_src_dir`` to the absolute path. Currently
62-
only one MATLAB path can be specified, but all subfolders in that tree will
63-
be searched.
64-
65-
``matlab_src_encoding``
66-
The encoding of the MATLAB files. By default, the files will be read as utf-8
67-
and parsing errors will be replaced using ? chars. *Added in Version 0.9.0*.
68-
69-
``matlab_keep_package_prefix``
70-
Determines if the MATLAB package prefix ``+`` is displayed in the
71-
generated documentation. Default is ``False``. When ``False``, packages are
72-
still referred to in ReST using ``+pakage.+subpkg.func`` but the output
73-
will be ``pakage.other.func()``. *Added in Version
74-
0.11.0*.
75-
76-
``matlab_show_property_default_value``
77-
Show property default values in the rendered document. Default is ``False``,
78-
which is what MathWorks does in their documentation. *Added in Version
79-
0.16.0*.
61+
set the config value of ``matlab_src_dir`` to an absolute path. Currently
62+
only one MATLAB path can be specified, but that folder and all the subfolders
63+
in that tree will be searched.
8064

8165
``matlab_short_links``
8266
Shorten all class, package and functions to the minimum length. This assumes
8367
that everything is in the path as we would expect it in MATLAB. This will
8468
resemble a more MATLAB-like presentation. If it is ``True`` is forces
85-
``matlab_keep_package_prefix = False``. Further, it allows for much shorter and cleaner references.
86-
Example, given a path to a class like ``target.subfolder.ClassFoo``.
69+
``matlab_keep_package_prefix = False``. Further, it allows for much shorter
70+
and cleaner references. Example, given a path to a class like
71+
``target.subfolder.ClassFoo``.
8772

8873
* With ``False``::
8974

@@ -95,9 +80,6 @@ Additional Configuration
9580

9681
Default is ``False``. *Added in Version 0.19.0*.
9782

98-
If you want the closest to MATLAB documentation style, use ``matlab_short_links
99-
= True`` in your ``conf.py`` file.
100-
10183
``matlab_auto_link``
10284
Automatically convert the names of known entities (e.g. classes, functions,
10385
properties, methods) to links Valid values are ``"basic"``
@@ -119,6 +101,30 @@ If you want the closest to MATLAB documentation style, use ``matlab_short_links
119101

120102
Default is ``None``. *Added in Version 0.20.0*.
121103

104+
``matlab_show_property_default_value``
105+
Show property default values in the rendered document. Default is ``False``,
106+
which is what MathWorks does in their documentation. *Added in Version
107+
0.16.0*.
108+
109+
``matlab_class_signature``
110+
Shows the constructor argument list in the class signature if ``True``.
111+
Default is ``False``. *Added in Version 0.20.0*.
112+
113+
``matlab_keep_package_prefix``
114+
Determines if the MATLAB package prefix ``+`` is displayed in the generated
115+
documentation. Default is ``False``. When ``False``, packages are still
116+
referred to in ReST using ``+pakage.+subpkg.func`` but the output will be
117+
``pakage.subpkg.func()``. Forced to ``False`` if ``matlab_short_links`` is
118+
``True``. *Added in Version 0.11.0*.
119+
120+
``matlab_src_encoding``
121+
The encoding of the MATLAB files. By default, the files will be read as utf-8
122+
and parsing errors will be replaced using ? chars. *Added in Version 0.9.0*.
123+
124+
If you want the closest to MATLAB documentation style, use ``matlab_short_links
125+
= True`` and ``matlab_auto_link = "basic"`` or ``matlab_auto_link = "all"`` in
126+
your ``conf.py`` file.
127+
122128

123129
Roles and Directives
124130
--------------------

sphinxcontrib/mat_documenters.py

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,6 @@ def can_document_member(cls, member, membername, isattr, parent):
913913
def format_args(self):
914914
if self.object.args:
915915
return "(" + ", ".join(self.object.args) + ")"
916-
else:
917-
return None
918916

919917
def document_members(self, all_members=False):
920918
pass
@@ -973,20 +971,20 @@ def import_object(self):
973971
return ret
974972

975973
def format_args(self):
976-
# for classes, the relevant signature is the "constructor" method,
977-
# which has the same name as the class definition
978-
initmeth = self.get_attr(self.object, self.object.name, None)
979-
# classes without constructor method, default constructor or
980-
# constructor written in C?
981-
if initmeth is None or not isinstance(initmeth, MatMethod):
974+
"""Format arguments
975+
976+
We use the method named the same as the class for arguments, but we only
977+
renders the arguments if `matlab_class_signature` is True and the method
978+
exist.
979+
"""
980+
ctor = self.get_attr(self.object, self.object.name, None)
981+
982+
if ctor is None or not isinstance(ctor, MatMethod):
982983
return None
983-
if initmeth.args:
984-
if initmeth.args[0] in ("obj", "self"):
985-
return "(" + ", ".join(initmeth.args[1:]) + ")"
986-
else:
987-
return "(" + ", ".join(initmeth.args) + ")"
988-
else:
984+
if ctor and not self.env.config.matlab_class_signature:
989985
return None
986+
if ctor.args:
987+
return "(" + ", ".join(ctor.args) + ")"
990988

991989
def format_signature(self):
992990
if self.doc_as_attr:
@@ -1277,8 +1275,18 @@ def import_object(self):
12771275
return ret
12781276

12791277
def format_args(self):
1278+
"""Format argument
1279+
1280+
We omit `obj` and `self` from the output if they are the first argument
1281+
unless it's a class constructor.
1282+
1283+
Rendering -> "(arg1, arg2, arg3)"
1284+
1285+
"""
1286+
is_ctor = self.object.cls.name == self.object.name
1287+
12801288
if self.object.args:
1281-
if self.object.args[0] in ("obj", "self"):
1289+
if self.object.args[0] in ("obj", "self") and not is_ctor:
12821290
return "(" + ", ".join(self.object.args[1:]) + ")"
12831291
else:
12841292
return "(" + ", ".join(self.object.args) + ")"

sphinxcontrib/matlab.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,7 @@ def setup(app):
864864
app.add_config_value("matlab_show_property_default_value", False, "env")
865865
app.add_config_value("matlab_short_links", False, "env")
866866
app.add_config_value("matlab_auto_link", None, "env")
867+
app.add_config_value("matlab_class_signature", False, "env")
867868

868869
app.registry.add_documenter("mat:module", doc.MatModuleDocumenter)
869870
app.add_directive_to_domain(

tests/test_autodoc.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_target(make_app, rootdir):
3636
assert len(content) == 1
3737
assert (
3838
content[0].astext()
39-
== "target\n\n\n\nclass target.ClassExample(a)\n\nBases: handle\n\nExample class\n\nClassExample Properties:\n\na - first property of ClassExample\nb - second property of ClassExample\nc - third property of ClassExample\n\nClassExample Methods:\n\nClassExample - the constructor and a reference to mymethod()\nmymethod - a method in ClassExample\n\nSee also BaseClass, baseFunction, b, unknownEntity, mymethod.\n\nProperty Summary\n\n\n\n\n\na\n\na property\n\n\n\nb\n\na property with default value\n\n\n\nc\n\na property with multiline default value\n\nMethod Summary\n\n\n\n\n\nmymethod(b)\n\nA method in ClassExample\n\nParameters\n\nb – an input to mymethod()"
39+
== "target\n\n\n\nclass target.ClassExample\n\nBases: handle\n\nExample class\n\nClassExample Properties:\n\na - first property of ClassExample\nb - second property of ClassExample\nc - third property of ClassExample\n\nClassExample Methods:\n\nClassExample - the constructor and a reference to mymethod()\nmymethod - a method in ClassExample\n\nSee also BaseClass, baseFunction, b, unknownEntity, mymethod.\n\nProperty Summary\n\n\n\n\n\na\n\na property\n\n\n\nb\n\na property with default value\n\n\n\nc\n\na property with multiline default value\n\nMethod Summary\n\n\n\n\n\nmymethod(b)\n\nA method in ClassExample\n\nParameters\n\nb – an input to mymethod()"
4040
)
4141
assert (
4242
property_section.rawsource
@@ -61,7 +61,7 @@ def test_target_show_default_value(make_app, rootdir):
6161
assert len(content) == 1
6262
assert (
6363
content[0].astext()
64-
== "target\n\n\n\nclass target.ClassExample(a)\n\nBases: handle\n\nExample class\n\nClassExample Properties:\n\na - first property of ClassExample\nb - second property of ClassExample\nc - third property of ClassExample\n\nClassExample Methods:\n\nClassExample - the constructor and a reference to mymethod()\nmymethod - a method in ClassExample\n\nSee also BaseClass, baseFunction, b, unknownEntity, mymethod.\n\nProperty Summary\n\n\n\n\n\na\n\na property\n\n\n\nb = 10\n\na property with default value\n\n\n\nc = [10; ... 30]\n\na property with multiline default value\n\nMethod Summary\n\n\n\n\n\nmymethod(b)\n\nA method in ClassExample\n\nParameters\n\nb – an input to mymethod()"
64+
== "target\n\n\n\nclass target.ClassExample\n\nBases: handle\n\nExample class\n\nClassExample Properties:\n\na - first property of ClassExample\nb - second property of ClassExample\nc - third property of ClassExample\n\nClassExample Methods:\n\nClassExample - the constructor and a reference to mymethod()\nmymethod - a method in ClassExample\n\nSee also BaseClass, baseFunction, b, unknownEntity, mymethod.\n\nProperty Summary\n\n\n\n\n\na\n\na property\n\n\n\nb = 10\n\na property with default value\n\n\n\nc = [10; ... 30]\n\na property with multiline default value\n\nMethod Summary\n\n\n\n\n\nmymethod(b)\n\nA method in ClassExample\n\nParameters\n\nb – an input to mymethod()"
6565
)
6666
assert (
6767
property_section.rawsource
@@ -135,7 +135,7 @@ def test_classfolder(make_app, rootdir):
135135
assert len(content) == 1
136136
assert (
137137
content[0].astext()
138-
== "classfolder\n\n\n\nclass target.@ClassFolder.ClassFolder(p)\n\nA class in a folder\n\nProperty Summary\n\n\n\n\n\np\n\na property of a class folder\n\nMethod Summary\n\n\n\n\n\nmethod_inside_classdef(a, b)\n\nMethod inside class definition"
138+
== "classfolder\n\n\n\nclass target.@ClassFolder.ClassFolder\n\nA class in a folder\n\nProperty Summary\n\n\n\n\n\np\n\na property of a class folder\n\nMethod Summary\n\n\n\n\n\nmethod_inside_classdef(a, b)\n\nMethod inside class definition"
139139
)
140140

141141

@@ -252,7 +252,7 @@ def test_root(make_app, rootdir):
252252
assert len(content) == 1
253253
assert (
254254
content[0].astext()
255-
== "root\n\n\n\nclass BaseClass(args)\n\nA class in the very root of the directory\n\nBaseClass Methods:\n\nBaseClass - the constructor, whose description extends\n\nto the next line\n\nDoBase - another BaseClass method\n\nSee Also\n\ntarget.ClassExample, baseFunction, ClassExample\n\nConstructor Summary\n\n\n\n\n\nBaseClass(args)\n\nThe constructor\n\nMethod Summary\n\n\n\n\n\nDoBase()\n\nDo the Base thing\n\n\n\nbaseFunction(x)\n\nReturn the base of x\n\nSee Also:\n\ntarget.submodule.ClassMeow\ntarget.package.ClassBar\nClassMeow\npackage.ClassBar"
255+
== "root\n\n\n\nclass BaseClass\n\nA class in the very root of the directory\n\nBaseClass Methods:\n\nBaseClass - the constructor, whose description extends\n\nto the next line\n\nDoBase - another BaseClass method\n\nSee Also\n\ntarget.ClassExample, baseFunction, ClassExample\n\nConstructor Summary\n\n\n\n\n\nBaseClass(obj, args)\n\nThe constructor\n\nMethod Summary\n\n\n\n\n\nDoBase()\n\nDo the Base thing\n\n\n\nbaseFunction(x)\n\nReturn the base of x\n\nSee Also:\n\ntarget.submodule.ClassMeow\ntarget.package.ClassBar\nClassMeow\npackage.ClassBar"
256256
)
257257

258258

@@ -267,7 +267,7 @@ def test_root_show_default_value(make_app, rootdir):
267267
assert len(content) == 1
268268
assert (
269269
content[0].astext()
270-
== "root\n\n\n\nclass BaseClass(args)\n\nA class in the very root of the directory\n\nBaseClass Methods:\n\nBaseClass - the constructor, whose description extends\n\nto the next line\n\nDoBase - another BaseClass method\n\nSee Also\n\ntarget.ClassExample, baseFunction, ClassExample\n\nConstructor Summary\n\n\n\n\n\nBaseClass(args)\n\nThe constructor\n\nMethod Summary\n\n\n\n\n\nDoBase()\n\nDo the Base thing\n\n\n\nbaseFunction(x)\n\nReturn the base of x\n\nSee Also:\n\ntarget.submodule.ClassMeow\ntarget.package.ClassBar\nClassMeow\npackage.ClassBar"
270+
== "root\n\n\n\nclass BaseClass\n\nA class in the very root of the directory\n\nBaseClass Methods:\n\nBaseClass - the constructor, whose description extends\n\nto the next line\n\nDoBase - another BaseClass method\n\nSee Also\n\ntarget.ClassExample, baseFunction, ClassExample\n\nConstructor Summary\n\n\n\n\n\nBaseClass(obj, args)\n\nThe constructor\n\nMethod Summary\n\n\n\n\n\nDoBase()\n\nDo the Base thing\n\n\n\nbaseFunction(x)\n\nReturn the base of x\n\nSee Also:\n\ntarget.submodule.ClassMeow\ntarget.package.ClassBar\nClassMeow\npackage.ClassBar"
271271
)
272272

273273

@@ -297,5 +297,20 @@ def test_root_auto_link_basic(make_app, rootdir):
297297
)
298298

299299

300+
@pytest.mark.skipif(sys.version_info < (3, 6), reason="requires python3.6 or higher")
301+
def test_root_class_signature(make_app, rootdir):
302+
srcdir = rootdir / "roots" / "test_autodoc"
303+
confdict = {"matlab_class_signature": True}
304+
app = make_app(srcdir=srcdir, confoverrides=confdict)
305+
app.builder.build_all()
306+
307+
content = pickle.loads((app.doctreedir / "index_root.doctree").read_bytes())
308+
assert len(content) == 1
309+
assert (
310+
content[0].astext()
311+
== "root\n\n\n\nclass BaseClass(obj, args)\n\nA class in the very root of the directory\n\nBaseClass Methods:\n\nBaseClass - the constructor, whose description extends\n\nto the next line\n\nDoBase - another BaseClass method\n\nSee Also\n\ntarget.ClassExample, baseFunction, ClassExample\n\nConstructor Summary\n\n\n\n\n\nBaseClass(obj, args)\n\nThe constructor\n\nMethod Summary\n\n\n\n\n\nDoBase()\n\nDo the Base thing\n\n\n\nbaseFunction(x)\n\nReturn the base of x\n\nSee Also:\n\ntarget.submodule.ClassMeow\ntarget.package.ClassBar\nClassMeow\npackage.ClassBar"
312+
)
313+
314+
300315
if __name__ == "__main__":
301316
pytest.main([__file__])

tests/test_autodoc_short_links.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_target(make_app, rootdir):
3737
assert len(content) == 1
3838
assert (
3939
content[0].astext()
40-
== "target\n\n\n\nclass ClassExample(a)\n\nBases: handle\n\nExample class\n\nClassExample Properties:\n\na - first property of ClassExample\nb - second property of ClassExample\nc - third property of ClassExample\n\nClassExample Methods:\n\nClassExample - the constructor and a reference to mymethod()\nmymethod - a method in ClassExample\n\nSee also BaseClass, baseFunction, b, unknownEntity, mymethod.\n\nProperty Summary\n\n\n\n\n\na\n\na property\n\n\n\nb\n\na property with default value\n\n\n\nc\n\na property with multiline default value\n\nMethod Summary\n\n\n\n\n\nmymethod(b)\n\nA method in ClassExample\n\nParameters\n\nb – an input to mymethod()"
40+
== "target\n\n\n\nclass ClassExample\n\nBases: handle\n\nExample class\n\nClassExample Properties:\n\na - first property of ClassExample\nb - second property of ClassExample\nc - third property of ClassExample\n\nClassExample Methods:\n\nClassExample - the constructor and a reference to mymethod()\nmymethod - a method in ClassExample\n\nSee also BaseClass, baseFunction, b, unknownEntity, mymethod.\n\nProperty Summary\n\n\n\n\n\na\n\na property\n\n\n\nb\n\na property with default value\n\n\n\nc\n\na property with multiline default value\n\nMethod Summary\n\n\n\n\n\nmymethod(b)\n\nA method in ClassExample\n\nParameters\n\nb – an input to mymethod()"
4141
)
4242
assert (
4343
property_section.rawsource
@@ -62,7 +62,7 @@ def test_target_show_default_value(make_app, rootdir):
6262
assert len(content) == 1
6363
assert (
6464
content[0].astext()
65-
== "target\n\n\n\nclass ClassExample(a)\n\nBases: handle\n\nExample class\n\nClassExample Properties:\n\na - first property of ClassExample\nb - second property of ClassExample\nc - third property of ClassExample\n\nClassExample Methods:\n\nClassExample - the constructor and a reference to mymethod()\nmymethod - a method in ClassExample\n\nSee also BaseClass, baseFunction, b, unknownEntity, mymethod.\n\nProperty Summary\n\n\n\n\n\na\n\na property\n\n\n\nb = 10\n\na property with default value\n\n\n\nc = [10; ... 30]\n\na property with multiline default value\n\nMethod Summary\n\n\n\n\n\nmymethod(b)\n\nA method in ClassExample\n\nParameters\n\nb – an input to mymethod()"
65+
== "target\n\n\n\nclass ClassExample\n\nBases: handle\n\nExample class\n\nClassExample Properties:\n\na - first property of ClassExample\nb - second property of ClassExample\nc - third property of ClassExample\n\nClassExample Methods:\n\nClassExample - the constructor and a reference to mymethod()\nmymethod - a method in ClassExample\n\nSee also BaseClass, baseFunction, b, unknownEntity, mymethod.\n\nProperty Summary\n\n\n\n\n\na\n\na property\n\n\n\nb = 10\n\na property with default value\n\n\n\nc = [10; ... 30]\n\na property with multiline default value\n\nMethod Summary\n\n\n\n\n\nmymethod(b)\n\nA method in ClassExample\n\nParameters\n\nb – an input to mymethod()"
6666
)
6767
assert (
6868
property_section.rawsource
@@ -137,7 +137,7 @@ def test_classfolder(make_app, rootdir):
137137
assert len(content) == 1
138138
assert (
139139
content[0].astext()
140-
== "classfolder\n\n\n\nclass ClassFolder(p)\n\nA class in a folder\n\nProperty Summary\n\n\n\n\n\np\n\na property of a class folder\n\nMethod Summary\n\n\n\n\n\nmethod_inside_classdef(a, b)\n\nMethod inside class definition"
140+
== "classfolder\n\n\n\nclass ClassFolder\n\nA class in a folder\n\nProperty Summary\n\n\n\n\n\np\n\na property of a class folder\n\nMethod Summary\n\n\n\n\n\nmethod_inside_classdef(a, b)\n\nMethod inside class definition"
141141
)
142142

143143

@@ -257,7 +257,7 @@ def test_root(make_app, rootdir):
257257
assert len(content) == 1
258258
assert (
259259
content[0].astext()
260-
== "root\n\n\n\nclass BaseClass(args)\n\nA class in the very root of the directory\n\nBaseClass Methods:\n\nBaseClass - the constructor, whose description extends\n\nto the next line\n\nDoBase - another BaseClass method\n\nSee Also\n\ntarget.ClassExample, baseFunction, ClassExample\n\nConstructor Summary\n\n\n\n\n\nBaseClass(args)\n\nThe constructor\n\nMethod Summary\n\n\n\n\n\nDoBase()\n\nDo the Base thing\n\n\n\nbaseFunction(x)\n\nReturn the base of x\n\nSee Also:\n\ntarget.submodule.ClassMeow\ntarget.package.ClassBar\nClassMeow\npackage.ClassBar"
260+
== "root\n\n\n\nclass BaseClass\n\nA class in the very root of the directory\n\nBaseClass Methods:\n\nBaseClass - the constructor, whose description extends\n\nto the next line\n\nDoBase - another BaseClass method\n\nSee Also\n\ntarget.ClassExample, baseFunction, ClassExample\n\nConstructor Summary\n\n\n\n\n\nBaseClass(obj, args)\n\nThe constructor\n\nMethod Summary\n\n\n\n\n\nDoBase()\n\nDo the Base thing\n\n\n\nbaseFunction(x)\n\nReturn the base of x\n\nSee Also:\n\ntarget.submodule.ClassMeow\ntarget.package.ClassBar\nClassMeow\npackage.ClassBar"
261261
)
262262

263263

@@ -272,7 +272,7 @@ def test_root_show_default_value(make_app, rootdir):
272272
assert len(content) == 1
273273
assert (
274274
content[0].astext()
275-
== "root\n\n\n\nclass BaseClass(args)\n\nA class in the very root of the directory\n\nBaseClass Methods:\n\nBaseClass - the constructor, whose description extends\n\nto the next line\n\nDoBase - another BaseClass method\n\nSee Also\n\ntarget.ClassExample, baseFunction, ClassExample\n\nConstructor Summary\n\n\n\n\n\nBaseClass(args)\n\nThe constructor\n\nMethod Summary\n\n\n\n\n\nDoBase()\n\nDo the Base thing\n\n\n\nbaseFunction(x)\n\nReturn the base of x\n\nSee Also:\n\ntarget.submodule.ClassMeow\ntarget.package.ClassBar\nClassMeow\npackage.ClassBar"
275+
== "root\n\n\n\nclass BaseClass\n\nA class in the very root of the directory\n\nBaseClass Methods:\n\nBaseClass - the constructor, whose description extends\n\nto the next line\n\nDoBase - another BaseClass method\n\nSee Also\n\ntarget.ClassExample, baseFunction, ClassExample\n\nConstructor Summary\n\n\n\n\n\nBaseClass(obj, args)\n\nThe constructor\n\nMethod Summary\n\n\n\n\n\nDoBase()\n\nDo the Base thing\n\n\n\nbaseFunction(x)\n\nReturn the base of x\n\nSee Also:\n\ntarget.submodule.ClassMeow\ntarget.package.ClassBar\nClassMeow\npackage.ClassBar"
276276
)
277277

278278

0 commit comments

Comments
 (0)