Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autodoc: add :novalue: option #8222

Closed
wants to merge 3 commits into from

Conversation

marlonjames
Copy link
Contributor

@marlonjames marlonjames commented Sep 18, 2020

Subject: Add :novalue: option to display type hint but remove value

Feature or Bugfix

  • Feature

Purpose

When documenting using the .. autodata:: directive, the :annotation: option only allows displaying nothing or replacing the annotation manually.
This adds the :novalue: option which will display the type hint but not the value.

Detail

When the :annotation: option is used, the current behavior is preserved, and :novalue: option does nothing.
When :novalue: is used by itself, the value will not be displayed but any type hint will.

Relates

#8209

When the :annotation: option is not used, the :novalue: option may be used to remove the value.
This is useful when you want to display the type hint but not the value, and you don't want to specify the annotation manually.
marlonjames and others added 2 commits September 22, 2020 11:05
Add tests for autodata directive options :annotation: and :novalue:.
@ktbarrett
Copy link

To clarify, this exists to support Python 3.5 and 2.7 compliant codebases where uninitialized variables cannot exist, but they still want to use type hints with Sphinx autodoc. Support for this case is currently missing.

Copy link
Member

@tk0miya tk0miya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now both python 3.5 and 2.7 reached EOL. So I wonder this is still needed.

@@ -326,6 +326,15 @@ inserting them into the page source under a suitable :rst:dir:`py:module`,
By default, without ``annotation`` option, Sphinx tries to obtain the value of
the variable and print it after the name.

The ``novalue`` option can be used instead of a blank ``annotation`` to show the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In autodoc, we usually use no- prefix to disable some feature (ex. no-members). So it is better to rename this to no-value (if we adopted this proposal)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The ``novalue`` option can be used instead of a blank ``annotation`` to show the
The ``no-value`` option can be used instead of a blank ``annotation`` to show the

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that imply this PR would require implementing the non-negated version of :value: as well?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If somebody requests it. But there are no such requests now, AFAIK.

@tk0miya tk0miya added this to the 3.4.0 milestone Oct 3, 2020
@@ -326,6 +326,15 @@ inserting them into the page source under a suitable :rst:dir:`py:module`,
By default, without ``annotation`` option, Sphinx tries to obtain the value of
the variable and print it after the name.

The ``novalue`` option can be used instead of a blank ``annotation`` to show the

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The ``novalue`` option can be used instead of a blank ``annotation`` to show the
The ``no-value`` option can be used instead of a blank ``annotation`` to show the

type hint but not the value::

.. autodata:: CD_DRIVE
:novalue:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:novalue:
:no-value:

.. autodata:: CD_DRIVE
:novalue:

If both the ``annotation`` and ``novalue`` options are used, ``novalue`` has no

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If both the ``annotation`` and ``novalue`` options are used, ``novalue`` has no
If both the ``annotation`` and ``no-value`` options are used, ``no-value`` has no

@@ -365,6 +374,9 @@ inserting them into the page source under a suitable :rst:dir:`py:module`,
option.
.. versionchanged:: 2.0
:rst:dir:`autodecorator` added.
.. versionchanged:: 3.3
:rst:dir:`autodata` and :rst:dir:`autoattribute` now have a ``novalue``

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
:rst:dir:`autodata` and :rst:dir:`autoattribute` now have a ``novalue``
:rst:dir:`autodata` and :rst:dir:`autoattribute` now have a ``no-value``

@@ -1598,6 +1598,7 @@ class DataDocumenter(ModuleLevelDocumenter):
priority = -10
option_spec = dict(ModuleLevelDocumenter.option_spec)
option_spec["annotation"] = annotation_option
option_spec["novalue"] = bool_option

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
option_spec["novalue"] = bool_option
option_spec["no-value"] = bool_option

@@ -326,6 +326,15 @@ inserting them into the page source under a suitable :rst:dir:`py:module`,
By default, without ``annotation`` option, Sphinx tries to obtain the value of
the variable and print it after the name.

The ``novalue`` option can be used instead of a blank ``annotation`` to show the

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would that imply this PR would require implementing the non-negated version of :value: as well?

@tk0miya tk0miya modified the milestones: 3.4.0, 3.3.0 Nov 1, 2020
@@ -326,6 +326,15 @@ inserting them into the page source under a suitable :rst:dir:`py:module`,
By default, without ``annotation`` option, Sphinx tries to obtain the value of
the variable and print it after the name.

The ``novalue`` option can be used instead of a blank ``annotation`` to show the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If somebody requests it. But there are no such requests now, AFAIK.

@@ -365,6 +374,9 @@ inserting them into the page source under a suitable :rst:dir:`py:module`,
option.
.. versionchanged:: 2.0
:rst:dir:`autodecorator` added.
.. versionchanged:: 3.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I determined to merge this into 3.4. Please update this.

@@ -11,3 +11,16 @@
.. autofunction:: target.typehints.incr

.. autofunction:: target.typehints.tuple_args

.. autodata:: target.typed_vars_py35.attr1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, we don't need to update this file because we have do_autodoc() helper.

@@ -0,0 +1,11 @@
#: attr1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

py35 suffix is no longer needed because Sphinx supports py35 or above.

I think no-value option is not related to "typed variables" at all. So no reason to add new examples. How about using target.integer instead?

@@ -2024,6 +2025,151 @@ def test_pyclass_for_ClassLevelDocumenter(app):
]


@pytest.mark.skipif(sys.version_info < (3, 6), reason='py36+ is available since python3.6.')
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_pydata_for_DataDeclarationDocumenter(app):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to test with all of data-declaration, py36-style typed attributes and py35-style typed attributes?

@tk0miya
Copy link
Member

tk0miya commented Nov 14, 2020

Now I posted #8424 as a refined version of this PR. I'd like to merge it instead of this in a few days. Please let me know your opinion :-)

tk0miya added a commit to tk0miya/sphinx that referenced this pull request Nov 15, 2020
tk0miya added a commit to tk0miya/sphinx that referenced this pull request Nov 15, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants