Skip to content

Commit 2ea8392

Browse files
committed
Merge branch 'develop' of //codra.local/fichiers/DI_Projets/CEA-plotpy/Sources/guidata into develop
2 parents 42b1272 + 59c258f commit 2ea8392

File tree

2 files changed

+33
-51
lines changed

2 files changed

+33
-51
lines changed

doc/autodoc/index.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Three directive are provided:
1212

1313
* :code:`.. autodataset_create:: [module.dataset].create` to document the :code:`create()` classmethod of a :code:`DataSet` using its :code:`DataItem`.
1414
* :code:`.. datasetnote:: [module.dataset] [n]` to display a note on how to instanciate a dataset. Optional parameter :code:`n` gives the number of items to show.
15-
* :code:`.. autodataset:: [module.dataset]` used to document a dataset class. It is derived from the :code:`.. autoclass::` directive and therefore has the same options. By default, it will document a dataset without its constructor signature nor its attributes but will document the :code:`create()` method using the :code:`autodataset_create` directive. Several additional options are available to more finely control the documentation (see examples below).
15+
* :code:`.. autodataset:: [module.dataset]` used to document a dataset class. It is derived from the :code:`.. autoclass::` directive and therefore has the same options. By default, it will document a dataset without its constructor signature but will document its attributes and the :code:`create()` class method using the :code:`autodataset_create` directive. Several additional options are available to more finely control the documentation (see examples below).
1616

1717
Example dataset
1818
---------------
@@ -46,7 +46,7 @@ Advanced usage
4646
The :code:`.. autodataset::` directive behavior can be modified using all :code:`.. autoclass::` options, as well as the the following ones:
4747

4848
* :code:`:showsig:` to show the constructor signature
49-
* :code:`:showattr:` to show the dataset attributes
49+
* :code:`:hideattr:` to hide the dataset attributes
5050
* :code:`:shownote: [n]` to add a note on how to instanciate the dataset with the first :code:`n` items. If :code:`n` is not provided, all items will be shown.
5151
* :code:`:hidecreate:` to hide the :code:`create()` method documentation which is shown by default.
5252

@@ -56,14 +56,14 @@ The following reST example shows how these options can be used.
5656
5757
.. autodataset:: autodoc_example.AutodocExampleParam2
5858
:showsig:
59-
:showattr:
59+
:hideattr:
6060
:hidecreate:
6161
:shownote: 5
6262
:members:
6363
6464
.. autodataset:: autodoc_example.AutodocExampleParam2
6565
:showsig:
66-
:showattr:
66+
:hideattr:
6767
:hidecreate:
6868
:shownote: 5
6969
:members:

guidata/dataset/autodoc.py

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import logging
99
import re
1010
from inspect import Parameter, Signature
11-
from typing import TYPE_CHECKING, Any, Hashable, Type
11+
from typing import TYPE_CHECKING, Any, Type
1212

1313
from docutils import nodes
1414
from docutils.statemachine import StringList
@@ -26,14 +26,6 @@
2626
from docutils.parsers.rst.states import RSTState, RSTStateMachine
2727
from sphinx.application import Sphinx
2828

29-
DATAITEMS_NONE_DEFAULT: tuple[type[gds.DataItem], ...] = (
30-
gds.DictItem,
31-
gds.FloatArrayItem,
32-
gds.DateItem,
33-
gds.FileSaveItem,
34-
gds.FilesOpenItem,
35-
)
36-
3729
IGNORED_AUTO_HELP: tuple[str, ...] = (_("integer"), _("float"), _("string"))
3830

3931

@@ -95,26 +87,6 @@ def datasetnote_option(arg: str) -> tuple[bool, int | None]:
9587
return True, None
9688

9789

98-
def check_item_can_be_documented(dataset: gds.DataSet, item: gds.DataItem) -> None:
99-
"""Checks if an item can be documented depending on its value.
100-
101-
Args:
102-
dataset: DataSet instance.
103-
item: DataItem instance.
104-
105-
Raises:
106-
ValueError: If the item has a default value that is not allowed (not hashable).
107-
"""
108-
value = item.get_value(dataset)
109-
110-
if isinstance(item, DATAITEMS_NONE_DEFAULT) and not isinstance(value, Hashable):
111-
raise ValueError(
112-
f"Item '{item.get_name()}' from {dataset.__class__.__name__} has a "
113-
f"default value of type {type(value)} = {value} which is not allowed. "
114-
"Default value should be an immutable type."
115-
)
116-
117-
11890
def document_choice_item(item: gds.ChoiceItem) -> str:
11991
"""Additional documentation for ChoiceItem containing the available choices.
12092
@@ -224,8 +196,7 @@ class ItemDoc:
224196

225197
def __init__(self, dataset: gds.DataSet, item: gds.DataItem) -> None:
226198
self.item = item
227-
228-
check_item_can_be_documented(dataset, item)
199+
self.item_type = stringify_annotation(type(item))
229200

230201
type_ = item.type
231202
if not type_:
@@ -288,7 +259,7 @@ def to_attribute(self) -> str:
288259
Formated docstring of the item.
289260
"""
290261
return escape_docline(
291-
f"\t{self.name} ({type(self.item)}): {self.label} {self.help_} "
262+
f"\t{self.name} ({self.item_type}): {self.label} {self.help_} "
292263
+ _("Default: %s.") % self.default
293264
)
294265

@@ -305,10 +276,13 @@ class CreateMethodDocumenter(MethodDocumenter):
305276
@classmethod
306277
def can_document_member(cls, member, membername, isattr, parent):
307278
"""Override the parent method to only document the DataSet.create() method."""
308-
try:
309-
return issubclass(parent, gds.DataSet) and membername == "create"
310-
except TypeError:
311-
return False
279+
is_create_method = (
280+
membername == "create"
281+
and isinstance(member, classmethod)
282+
and issubclass(member.__class__, gds.DataSet)
283+
)
284+
285+
return is_create_method
312286

313287
def format_signature(self, **kwargs: Any) -> str:
314288
"""Override the parent method to dynamically generate a signature for the parent
@@ -374,7 +348,7 @@ class DataSetDocumenter(ClassDocumenter):
374348
option_spec = dict(ClassDocumenter.option_spec)
375349
option_spec.update(
376350
{
377-
"showattr": bool_option,
351+
"hideattr": bool_option,
378352
"hidecreate": bool_option,
379353
"showsig": bool_option,
380354
"shownote": datasetnote_option,
@@ -404,8 +378,8 @@ def format_signature(self, **kwargs) -> str:
404378

405379
def get_doc(self) -> list[list[str]]:
406380
"""Override the parent method to dynamically generate a docstring for the
407-
DataSet class depending on the DataItem of the DatSet. DataSet attributes
408-
(DataItems) are documented in the docstring if the 'showattr' option is used.
381+
DataSet class depending on the DataItem of the DatSet. By default the dataset
382+
attributes are documented but can be hidden using the 'hideattr' option.
409383
410384
Returns:
411385
Docstring note_lines.
@@ -420,7 +394,7 @@ def get_doc(self) -> list[list[str]]:
420394
docstring_lines = [
421395
first_line or "",
422396
]
423-
if self.options.get("showattr", False):
397+
if not self.options.get("hideattr", False):
424398
docstring_lines.extend(("", "Attributes:"))
425399
dataset = self.object()
426400
for item in dataset.get_items():
@@ -441,12 +415,20 @@ def add_content(self, more_content: Any | None) -> None:
441415
Args:
442416
more_content: Additional content to show/hide.
443417
"""
444-
super().add_content(more_content)
418+
source = self.get_sourcename()
419+
hide_create: bool = self.options.get("hidecreate", False)
420+
create_method_overwritten = "create" in self.object.__dict__
445421

446-
if not self.options.get("hidecreate", False):
447-
fullname = (
448-
self.object.__module__ + "." + self.object.__qualname__ + ".create"
449-
)
422+
if hide_create or self.options.inherited_members and not hide_create:
423+
if self.options.exclude_members is None:
424+
self.options["exclude-members"] = set(("create",))
425+
else:
426+
self.options["exclude-members"].add("create")
427+
428+
super().add_content(more_content=more_content)
429+
430+
if not hide_create and not create_method_overwritten:
431+
fullname = self.fullname + ".create"
450432
method_documenter = CreateMethodDocumenter(
451433
self.directive, fullname, indent=self.content_indent
452434
)
@@ -458,7 +440,7 @@ def add_content(self, more_content: Any | None) -> None:
458440
".. datasetnote:: "
459441
f"{self.object.__module__ + '.' + self.object.__qualname__} "
460442
f"{example_lines or ''}",
461-
self.get_sourcename(),
443+
source,
462444
)
463445

464446

@@ -593,7 +575,7 @@ def run(self):
593575

594576
example_lines = min(len(items), example_lines) if example_lines else len(items)
595577
code_lines = [
596-
f"dataset = {cls.__name__}()",
578+
f"param = {cls.__name__}()",
597579
*(
598580
f"param.{items[i].get_name()} = "
599581
f"{object_description(items[i].get_value(instance))}"

0 commit comments

Comments
 (0)