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

Allow unqualified type hint names in generated docs #7119

Closed
alkasm opened this issue Feb 9, 2020 · 23 comments
Closed

Allow unqualified type hint names in generated docs #7119

alkasm opened this issue Feb 9, 2020 · 23 comments
Labels
extensions:autodoc type:enhancement enhance or introduce a new feature
Milestone

Comments

@alkasm
Copy link

alkasm commented Feb 9, 2020

It would be great if the current type hinting work would support removing qualification from type names. For some modules, the types may be deeply nested which will more easily break the page layout.

Definitions (by example)

  • Qualified name: pkg.module.ClassName
  • Unqualified name: ClassName

Example

from typing import Tuple
from numpy.polynomial import polynomial

def multiply_polynomial(
    p: polynomial.Polynomial, value: int
) -> Tuple[polynomial.Polynomial, polynomial.Polynomial]:
    return p, polynomial.Polynomial(*(value * p.coef))

In this code, the type is polynomial.Polynomial but the fully qualified name is numpy.polynomial.polynomial.Polynomial which is what will show up in the docs.

With the following in conf.py

extensions = [
    "sphinx.ext.autodoc",
    "sphinx.ext.autodoc.typehints",
]
autodoc_typehints = "description"

We'd see some generated docs similar to

mypkg.mymodule.multiply_polynomial(p, value)
    Parameters:     * p (numpy.polynomial.polynomial.Polynomial) –
                    * value (int) –
    Return type:    Tuple[numpy.polynomial.polynomial.Polynomial, numpy.polynomial.polynomial.Polynomial]

Instead, with unqualified names, we'd have a much simpler

mypkg.mymodule.multiply_polynomial(p, value)
    Parameters:     * p (Polynomial) –
                    * value (int) –
    Return type:    Tuple[Polynomial, Polynomial]

Rationale

Full qualification inherently gives more context, so it is a sane default. However, intersphinx linkage to the correct types gives even better context, since you can just follow directly to the actual type definition. With both intersphinx linkage and unqualified type names, you can have the best of both worlds: readability and quick disambiguation.

Similar prior work

The external extension https://github.com/agronholm/sphinx-autodoc-typehints collapses the name to the unqualified name by default, unless otherwise opted for, for an example of how this might work.

Variant

One possible issue with the above is that there's still no direct control of the type name---either you get the ClassName, or you get the fully.qualified.ClassName. An alternative may be to use whatever is used in the source code. E.g., if the code has import fully.qualified as qual and uses qual.ClassName in the signature, then Sphinx can respect that. +1 because the docs follow the code, but -1 because no consistency is guaranteed. All three versions could possibly be supported, though, and left up to the maintainer which style they go with.

With the above code example, this variant would turn the generated docs into:

mypkg.mymodule.multiply_polynomial(p, value)
    Parameters:     * p (polynomial.Polynomial) –
                    * value (int) –
    Return type:    Tuple[polynomial.Polynomial, polynomial.Polynomial]
@alkasm alkasm added the type:enhancement enhance or introduce a new feature label Feb 9, 2020
@alkasm alkasm changed the title Allow unqualified type hints Allow unqualified type hint names in generated HTML Feb 9, 2020
@alkasm alkasm changed the title Allow unqualified type hint names in generated HTML Allow unqualified type hint names in generated docs Feb 9, 2020
@tk0miya
Copy link
Member

tk0miya commented Feb 9, 2020

+1: Could you give an example please? It would be helpful to me for improvement.

@tk0miya tk0miya added this to the 3.0.0 milestone Feb 9, 2020
@alkasm
Copy link
Author

alkasm commented Feb 9, 2020

Added examples to the original post, with another variant as a suggestion.

@tk0miya
Copy link
Member

tk0miya commented Feb 9, 2020

Thank you for update. Indeed, it should be improved more. I'll consider the way to modify its name.

@tk0miya tk0miya modified the milestones: 3.0.0, 3.1.0 Mar 14, 2020
@tk0miya tk0miya modified the milestones: 3.1.0, 3.2.0 Jun 4, 2020
@tk0miya tk0miya modified the milestones: 3.2.0, 3.3.0 Aug 7, 2020
@mquinson
Copy link

This would also be particularly precious for the cpp domain, with the namespace being sometimes implicit and given by the context (by the englobing namespace constructs) only.

I am wondering. Is my request about the cpp domain really related to the original request? It's hard to know for me because the answer depends on implementation details that I don't know. If it's another request, I'm happy to open a separate issue.

Thanks for your work,

@jakobandersen
Copy link
Contributor

This would also be particularly precious for the cpp domain, with the namespace being sometimes implicit and given by the context (by the englobing namespace constructs) only.

I am wondering. Is my request about the cpp domain really related to the original request? It's hard to know for me because the answer depends on implementation details that I don't know. If it's another request, I'm happy to open a separate issue.

All domains are almost entirely separate in their implementation, so in general I recommend opening separate issues as default. I'm not sure if the exact details of this issue, so please open a C++ specific issue for that discussion.

@mquinson
Copy link

Hello, for the record, I opened #8228 for the specificity of the C++ domain. Sorry for the noise.

@tk0miya tk0miya modified the milestones: 3.3.0, 3.4.0 Nov 1, 2020
tk0miya added a commit to tk0miya/sphinx that referenced this issue Nov 15, 2020
So far, a TypeVar is rendered without module name. As a result, it
could not be resolved if it is imported from other modules.  This
prepends its module name and make it resolvable.

As a side effect, all of TypeVars are displayed with module name. It
should be fixed in latter step (refs: sphinx-doc#7119)
tk0miya added a commit to tk0miya/sphinx that referenced this issue Nov 15, 2020
So far, a TypeVar is rendered without module name. As a result, it
could not be resolved if it is imported from other modules.  This
prepends its module name and make it resolvable.  This is available
only in Python 3.6 or above.

As a side effect, all of TypeVars are displayed with module name. It
should be fixed in latter step (refs: sphinx-doc#7119)
tk0miya added a commit to tk0miya/sphinx that referenced this issue Nov 15, 2020
So far, a TypeVar is rendered without module name. As a result, it
could not be resolved if it is imported from other modules.  This
prepends its module name and make it resolvable.  This is available
only in Python 3.7 or above.

As a side effect, all of TypeVars are displayed with module name. It
should be fixed in latter step (refs: sphinx-doc#7119)
tk0miya added a commit to tk0miya/sphinx that referenced this issue Nov 22, 2020
So far, a TypeVar is rendered without module name. As a result, it
could not be resolved if it is imported from other modules.  This
prepends its module name and make it resolvable.  This is available
only in Python 3.7 or above.

As a side effect, all of TypeVars are displayed with module name. It
should be fixed in latter step (refs: sphinx-doc#7119)
@tk0miya tk0miya modified the milestones: 3.4.0, 3.5.0 Dec 19, 2020
tk0miya added a commit to tk0miya/sphinx that referenced this issue Dec 25, 2020
So far, a TypeVar is rendered without module name. As a result, it
could not be resolved if it is imported from other modules.  This
prepends its module name and make it resolvable.  This is available
only in Python 3.7 or above.

As a side effect, all of TypeVars are displayed with module name. It
should be fixed in latter step (refs: sphinx-doc#7119)
@tk0miya tk0miya removed this from the 3.5.0 milestone Jan 24, 2021
@tk0miya tk0miya added this to the 4.0.0 milestone Jan 24, 2021
tk0miya added a commit to tk0miya/sphinx that referenced this issue Feb 28, 2021
So far, a TypeVar is rendered without module name. As a result, it
could not be resolved if it is imported from other modules.  This
prepends its module name and make it resolvable.  This is available
only in Python 3.7 or above.

As a side effect, all of TypeVars are displayed with module name. It
should be fixed in latter step (refs: sphinx-doc#7119)
tk0miya added a commit to tk0miya/sphinx that referenced this issue Feb 28, 2021
So far, a TypeVar is rendered without module name. As a result, it
could not be resolved if it is imported from other modules.  This
prepends its module name and make it resolvable.  This is available
only in Python 3.7 or above.

As a side effect, all of TypeVars are displayed with module name. It
should be fixed in latter step (refs: sphinx-doc#7119)
tk0miya added a commit that referenced this issue Mar 6, 2021
Fix #7119: Show type hint names unqualified when resolving succeeded
@tk0miya tk0miya closed this as completed Mar 6, 2021
@alkasm
Copy link
Author

alkasm commented Mar 6, 2021

@tk0miya Hey thanks for adding this support! I'm trying to build this example off master but when I try to use the new conf option, I get an exception

Exception occurred:
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/writers/html5.py", line 776, in unknown_visit
    raise NotImplementedError('Unknown node: ' + node.__class__.__name__)
NotImplementedError: Unknown node: pending_xref_condition
full output log
# Sphinx version: 4.0.0+
# Python version: 3.7.7 (CPython)
# Docutils version: 0.16 release
# Jinja2 version: 2.11.3
# Last messages:
#   
#   looking for now-outdated files...
#   none found
#   pickling environment...
#   done
#   checking consistency...
#   done
#   preparing documents...
#   done
#   writing output... [100%] index
# Loaded extensions:
#   sphinx.ext.mathjax (4.0.0+) from /Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/ext/mathjax.py
#   sphinxcontrib.applehelp (1.0.2) from /Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinxcontrib/applehelp/__init__.py
#   sphinxcontrib.devhelp (1.0.2) from /Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinxcontrib/devhelp/__init__.py
#   sphinxcontrib.htmlhelp (1.0.3) from /Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinxcontrib/htmlhelp/__init__.py
#   sphinxcontrib.serializinghtml (1.1.4) from /Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinxcontrib/serializinghtml/__init__.py
#   sphinxcontrib.qthelp (1.0.3) from /Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinxcontrib/qthelp/__init__.py
#   alabaster (0.7.12) from /Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/alabaster/__init__.py
#   sphinx.ext.autodoc.type_comment (4.0.0+) from /Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/ext/autodoc/type_comment.py
#   sphinx.ext.autodoc (4.0.0+) from /Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/ext/autodoc/__init__.py
Traceback (most recent call last):
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/cmd/build.py", line 280, in build_main
    app.build(args.force_all, filenames)
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/application.py", line 347, in build
    self.builder.build_specific(filenames)
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 284, in build_specific
    summary=__('%d source files given on command line') % len(to_write))
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 357, in build
    self.write(docnames, list(updated_docnames), method)
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 531, in write
    self._write_serial(sorted(docnames))
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/builders/__init__.py", line 541, in _write_serial
    self.write_doc(docname, doctree)
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/builders/html/__init__.py", line 616, in write_doc
    self.docwriter.write(doctree, destination)
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/docutils/writers/__init__.py", line 78, in write
    self.translate()
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/writers/html.py", line 70, in translate
    self.document.walkabout(visitor)
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/docutils/nodes.py", line 214, in walkabout
    if child.walkabout(visitor):
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/docutils/nodes.py", line 214, in walkabout
    if child.walkabout(visitor):
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/docutils/nodes.py", line 214, in walkabout
    if child.walkabout(visitor):
  [Previous line repeated 4 more times]
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/docutils/nodes.py", line 206, in walkabout
    visitor.dispatch_visit(self)
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/util/docutils.py", line 472, in dispatch_visit
    super().dispatch_visit(node)
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/docutils/nodes.py", line 1995, in dispatch_visit
    return method(node)
  File "/Users/alkasm/prog/test-sphinx/venv/lib/python3.7/site-packages/sphinx/writers/html5.py", line 776, in unknown_visit
    raise NotImplementedError('Unknown node: ' + node.__class__.__name__)
NotImplementedError: Unknown node: pending_xref_condition

I am able to build the given test case example in the PR, but not my example in my first post above. For a minimal example, everything works as intended with

# bar.py
class Bar: 
    pass
# foo.py
import bar
def make_bar() -> bar.Bar:
    return bar.Bar()

but if I import an installed module (even something in the stdlib) instead of my local bar.py, like

import typing
def make_tuple() -> typing.Tuple: 
    return ()

I get the above error.

@tk0miya
Copy link
Member

tk0miya commented Mar 6, 2021

@alkasm Sorry, I can't reproduce the error. Could you share the whole of your example?

@alkasm
Copy link
Author

alkasm commented Mar 6, 2021

@tk0miya sure, here's a full repro for me: https://github.com/alkasm/sphinx-unqual-types. Not sure if I just have something setup incorrectly, or what.

KunKaxx pushed a commit to KunKaxx/sphinx that referenced this issue Mar 11, 2021
* Fix missing module: warnings

* Update CHANGES for PR sphinx-doc#8070

* Makefiles: Include clean in help message

The make clean help command was missing from all make files.

* Update make.bat_t

* Update Makefile_t

* Reword section on Third Party Themes

- No longer attempt to be the location for listing themes -- only Sphinx-RTD-Theme was listed here.
- Mention the classifier used when searching on PyPI.
- Emphasize sphinx-themes.org as a gallery of themes.

* Fix mypy violations

* Fix sphinx-doc#8342: Emit a warning if a unknown domain is given for directive or role

Currently, Sphinx mention nothing if users use unknown domain in their
documents (ex. `.. unknown:directive::`, `:unknown:role` and so on).
This starts to warn them to be clear non acceptable mark-ups.

refs: sphinx-doc#8342

* Close sphinx-doc#7996: manpage: Make a section directory on build manpage by default

* Test and document support for Python 3.9 release

* Drop code for supporting py35

* Remove additional mentions of Python 3.5

* Fix flake8 issue

* Fix sphinx-doc#8380: html search: search results are wrapped with <p> instead of <div>

* Drop code for py35

* Do test with Ubuntu 18.04

Sphinx-4.0 will drop support for Ubuntu 16.04.  So CI Platform should be
also updated to Ubuntu 18.04.

* Do isort

* Fix flake8 violation

* Remove config variable: source_parsers

It was already deprecated at 1.8.0 and removed at 3.0.0.  So the
definition is no longer used.

refs: dc45877

* Fix flake8 violations

* Remove code for python3.5

* Fix importing error

* Fix flake8 warnings

* Change html_logo & favicon config value process to handle url as values

* Fix sphinx-doc#8508: LaTeX: uplatex becomes a default setting of latex_engine for Japanese

Since v2.3, Sphinx supports uplatex as an alternative of latex_engine for Japanese
docs (refs: sphinx-doc#4186, sphinx-doc#6841). uplatex is able to build a document without conversion
character encoding internally. It allows using unicode characters in documents.
Additionally, uplatex is compatible with platex (current default latex_engine for
Japanese docs).

This changes the default latex_engine for Japanese document to uplatex.

* Fix basic layout and html_logo & favicon config value process to support url as values

* Update layouts to reference logo & favicon without static

* Fix style check errors

* Prevent page brake in the middle of a `seealso` 

* Fold long line

* Fix a flake8 violation

* Remove deprecated function: sphinx.ext.autodoc.importer:_getmro()

* Update the sphinx core events in appappi.rst

I'm very happy to see that the events in Sphinx have been laid out in order as they have been in this doc.

With that being said the notation I noticed was a bit odd. Some of the items in the list refer to events and as a result,
parenthesis are used to indicate the beginning and end of the parameters that the event takes.

In other items in the list, parenthesis are used for additional context for an event. The combination of some particularly very long lines,
the intermixing of comments, pseudo-code, indentation of items following a `for` or an `if` made this very difficult to read on a mobile device.

As a result, I've added whitespace, fixed a typo, reduced the length of some lines by moving them to a new line, and removed all `()` that were not
present to indicate parameters fed to a function.

* Fix mypy violations

* test: py domain: Add a testcase for :var: field

* Close sphinx-doc#5977: :var: field do not create a cross-reference

Since its beginning, `:var:` field has created a cross-reference to the
attribute having the same name.  It is meaningful only if the attribute
is documented by `py:attribute` directive.  It means the `:var:` field
and `:attr:` role are almost the same and conflicted.  Additionally,
the cross-reference points incorrect variable if the target is not
documented.

Thus, the cross-reference feature of `:var:` field is disabled.

* refactor: Move CSS tags in basic/layout.html to ``css_files`` variable

To make CSS customizable, all CSS files in basic/layout.html has their
priority: 200.  Therefore, extensions and users can insert their own
custom CSS files before or just after them.

As a side effect, the CSS tags in basic/layout.html are removed.  These
CSS files will be rendered via `css_files` template variable.

refs: sphinx-doc#8634, c5f0398

* refactor: Do not import sphinx.util.smartypants because of deprecated

* Fix sphinx-doc#4550: The align attribute of figure nodes becomes None by default

To keep compatibility with the standard doctree model of docutils,
this stops to use 'default' value as a default value of the align
attribute for figure and table nodes.

* refactor: test: Do not use deprecated function: execfile_()

* Close sphinx-doc#5560: napoleon_use_param also affect "other parameters" section

* LaTeX: update default font configuration

This replaces times package with tgtermes and tgheros (clones of Times and
Helvetica with better LaTeX support) and the monospace font from txfonts
package (txtt). This font is better matched with Times-like fonts than
Courier clones.

The changes applies to pdflatex/platex/uplatex.

Fixes: sphinx-doc#8711

* pour tester token

	new file:   dummy

* test

* test

* Cleaning up accidental mess

My apologies.  I was testing authentication token, pushing master to my
forked repo.  But I ended up accidentally pushing to sphinx-doc/sphinx,
and force pushing afterwards to clean up then was rejected.

	deleted:    dummy

* refactor: html theme: Insert documentation_options.js via script_files

* refactor: Index class becomes subclasses of abc.ABC

The `Index` class becomes subclasses of `abc.ABC` to indicate
methods that must be overrided in the concrete classes.

* Update the link to the new sphinx-contrib organization

This should be backported to at least 3.x, too.

* refactor: Add sphinx.util:isurl()

* html theme: Add `favicon_url` and `logo_url`

To embed the external favicon and logo image, this adds new template
variable `favicon_url` and `logo_url` that point the external URL or
relative path for the favicon/logo file from current file.  It helps to
use it on template files.

* Update CHANGES

* doc: html_favicon and html_logo accept URL now

* Update CHANGES for PR sphinx-doc#8510

* refactor: Remove unused method

* Fix sphinx-doc#6550: Emit a deprecation warning for html_add_permalinks

Let's start to emit a deprecation warning for html_add_permalinks since
the major release v4.0.

* Fix sphinx-doc#8737: html: broken img tag is appeared when html_logo not set

* LaTeX: sync pdftex engine default imageresolution with pxunit

Closes: sphinx-doc#8253

The 'pxunit' key from latex_elements instructs how to handle image
dimensions specified in px units.

But pdftex has \pdfimageresolution which is used when an image file does
not provide readable or legit values for the x and/or y resolution.

This commit syncs them: from 'pxunit' the default image resolution in
pixels per inch (an integer) is computed.

This way an image will behave the same if:

- it is loaded with no explicit size set, _and_ no readable image
  resolution data is readable from the file (or that data matches the
  'pxunit' setting)

- or a size is set in figure directive using px units and equal to the
  natural pixel size of the image,

This also with 'lualatex' but is ignored by with 'xelatex' and
'uplatex'.

* html: html_codeblock_linenos_style defaults to 'inline' (refs: sphinx-doc#7849)

As discussed in sphinx-doc#7879, the default style of line numbers for code
blocks in HTML output becames 'inline' by default.  And 'table' style
is now deprecated and will be removed in Sphinx-6.0.

* Refactor LaTeX [1/2]: split sphinx.sty into separate components

The latex macros from sphinx.sty were already partitioned into
successive sections.  The file sphinx.sty is split into multiple
files in concordance with this pre-existing sectioning.

The files are loaded via \input. File extension is .sty not .tex
to not confuse the Makefile.

* Refactor LaTeX [2/2]: renamings of auxiliary Sphinx LaTeX packages

sphinxcyrillic -> sphinxpackagecyrillic
sphinxmulticell -> sphinxpackagemulticell
footnotehyper-sphinx -> sphinxpackagefootnote

* Update CHANGES for PR sphinx-doc#8769

* Refactor LaTeX [3/2]: Add all missing .sty in the \ProvidesFile

Sorry for oversight

* Refactor LaTeX [4/2]: suppress extra bracket from bad latex merge :(

* Revert alteration at c9480f9 merge of 3.x of doc/conf.py

Compare doc/conf.py after merge at c9480f9 to what it was at 2ee0338.

It loses the modification from sphinx-doc#8716 (merged at 38c6143) and thus
reverts doc/conf.py to former font config using mathpazo.

* Refactor LaTeX style files

This is a (continuation and) re-work of sphinx-doc#8769 (e6bf914)

I have reintegrated option handling and most package loading into the
original file sphinx.sty and reorganized completely the filenames of
secondary style files.

sphinx.sty had become too big and first sphinx-doc#8769 now this more definitive
refactoring is necessary to clarify structure, dependencies, and ease up
future maintenance.

Unfortunately this means a lot of moving around hunks of latex code with
some alterations.  I tried to carefully check everything is defined in
right order (as LaTeX being a macro expansion language, often one can
manipulate things before them being defined, nevertheless I checked
things are done in order).

Only simple thing is to review is that I added missing EOLs at last
lines of the extracted files...

* Fix markup in docs (from d6e11b8)

* LaTeX: document what the style files provide and require

* Fix accidental revert of f937fac (sphinx-doc#8767) by sphinx-doc#8790 merge

Due to file renaming

* Fix lost LaTeX in merge

* Re-insert if isinstance(node, ast.Constant): into py _parse_annotation

As master drop python 3.5 support, conditional

    if sys.version_info >= (3, 6):

not needed anymore.  This hunk had got lost in merge.

	modified:   sphinx/domains/python.py

* refactor: py domain: Put if-block for ast.Constant to the root level

* Make code block types more visible

* Update type annotations

* Update latex comment

* fix potential getitem error in resolve_anyref

* refactor: Remove meaningless type annotations

* migrate html_add_permalinks=None and html_add_permalinks=""

The docs list: Set it to None or the empty string to disable permalinks.

* C++, cpp_index_common_prefix remove longest

Fixes sphinx-doc#8911

* Fix sphinx-doc#8915: html theme: The translation of sphinx_rtd_theme does not work

Since sphinx_rtd_theme-0.5.0, it supports translations.  But Sphinx core
disallows to enable it because theming framework gives special treatment
for the theme for a long time.

This goes to load it via setuptools at first to enable the translations.

Note: The special treatment for sphinx_rtd_theme (< 0.2.5) is not
removed yet.  But it will be removed in the future release.

* Close sphinx-doc#8924: autodoc: Support `bound` argument for TypeVar

* C, properly error on keywords as function parameters

* C, remove dead code

* C, remove more dead code

* C, simplify tests

* C, test namespace revamp

* LaTeX: let underfull calculation in wrapped code lines ignore last line

Closes: sphinx-doc#8925

* Update CHANGES

* Gather LaTeX items in CHANGES for 4.0.0

* Fix sphinx-doc#8917: autodoc: Raises a warning if function has wrong __globals__ value

`sphinx.util.inspect:signature()` crashes with AttributeError when
subject has wrong `__globals__` value.  This ignores the error on
building.

* Fix sphinx-doc#8933: viewcode: Failed to create back-links on parallel build

On parallel build mode, viewcode losts the back-links information on
gathering results from each process.  As a result, some back-links are
missing in the generated viewcode pages.

This fixes the merging back-links process for parallel builds.

* refactor: LaTeX: Use raw strings for LaTeX macros

* Use explicit title for titlenode, when no title is provided

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Fix sphinx-doc#8938: imgconverter: Show the error of the command availability check

imgconverter extension suppresses an OSError like "Cannot allocate
memory" unexpectedly.  So the error should be shown with the warning.

* autodoc: an imported TypeVar is not resolved (refs: sphinx-doc#8415)

So far, a TypeVar is rendered without module name. As a result, it
could not be resolved if it is imported from other modules.  This
prepends its module name and make it resolvable.  This is available
only in Python 3.7 or above.

As a side effect, all of TypeVars are displayed with module name. It
should be fixed in latter step (refs: sphinx-doc#7119)

* Close sphinx-doc#8326: Rename master_doc to root_doc

To describe the purpose more accurately, the `master_doc` is now renamed
to `root_doc`.  The old name is still available.  But it is recommeneded
to use new one from now on.

* Update sphinx/builders/html/__init__.py

* Update sphinx/builders/html/__init__.py

* Update CHANGES for PR sphinx-doc#8905

* Apply code review suggestions

Signed-off-by: Joaquin Anton <janton@nvidia.com>

* Deprecate SphinxComponentRegistry.get_source_input()

The source_input system was deprecated at v2.0.  So no client uses it
longer now.  Therefore this deprecate the getter interface and its
usage.

* Update CHANGES for PR sphinx-doc#8937

* C++, support spaceship operator

Fixes sphinx-doc#8942

* format translatable strings in one go.  This enables translation tools like msgfmt to verify that an incorrect translation cannot crash sphinx-build.  This will fix current crashes for Hungarian and Greek (Spanish was fixed in sphinx-doc#8941)

* BUG: Fix rebuild regression

* C and C++, fix nested paramter lists

* Fix py.typed file not being included in source archive

* Add pending_xref_condition node

To choose appropriate content for pending_xref node on resolving,
this introduces a new custom node `pending_xref_condition`.  It only
has a condition for the filtering and contents of the reference.

* Filter pending_xref_condition node on failed resolution

* Fix sphinx-doc#7199: py domain: Add a new confval: python_use_unqualified_type_names

Add a new config variable: python_use_unqualified_type_names.  If enabled,
it goes to suppress the module name of the python reference if it can be
resolved.

* intersphinx: Support pending_xref_condition

* Fix sphinx-doc#759: autodoc: Add sphinx.ext.autodoc.preserve_defaults extension

Add a new extension `sphinx.ext.autodoc.preserve_defaults`.

It preserves the default argument values of function signatures in source code
and keep them not evaluated for readability.  This is an experimental
extension and it will be integrated into autodoc core in Sphinx-4.0.

* doc: Update document for autodoc :members: option

* doc: Update document for autodoc :undoc-members: option

* doc: Update document for autodoc :private-members: option

* doc: Update document for autodoc :special-members: option

* doc: Fix indentation

* Fix wrong directive name in warning messages

* Sphinx is available on Chocolatey

* lint

* Close sphinx-doc#8487: csv-table now considers abspath as relpath from srcdir

To make directives' behavior consistent, the :file: option for
csv-table directive now recognizes an absolute path as a relative
path from source directory.

* doc: Create autodoc extension tutorial

* doc: Added autodoc extension tutorial to tutorials index

* doc: Link autodoc tutorial in add_autodocumenter docstring

Uses :ref: link because :doc: does not work.

* doc: Added reflink to autodoc tutorial

Used in add_autodocumenter docstring

* Close sphinx-doc#7549: autosummary: Enable autosummary_generate by default

Co-authored-by: Takeshi KOMIYA <i.tkomiya@gmail.com>
Co-authored-by: Aaron Carlisle <carlisle.b3d@gmail.com>
Co-authored-by: Aaron Carlisle <carlisle.aaron00@gmail.com>
Co-authored-by: Pradyun Gedam <3275593+pradyunsg@users.noreply.github.com>
Co-authored-by: Jon Dufresne <jon.dufresne@gmail.com>
Co-authored-by: François Freitag <mail@franek.fr>
Co-authored-by: Mardelor <remy.zirnheld@laposte.net>
Co-authored-by: Toni Ruža <toni.ruza@gmail.com>
Co-authored-by: Faris A Chugthai <20028782+farisachugthai@users.noreply.github.com>
Co-authored-by: jfbu <jfbu@free.fr>
Co-authored-by: Steve Piercy <web@stevepiercy.com>
Co-authored-by: Harrissou Sant-anna <delazj@gmail.com>
Co-authored-by: Matthias C. M. Troffaes <matthias.troffaes@gmail.com>
Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Co-authored-by: Jakob Lykke Andersen <Jakob@caput.dk>
Co-authored-by: Jakob Lykke Andersen <jakobandersen@users.noreply.github.com>
Co-authored-by: Joaquin Anton <janton@nvidia.com>
Co-authored-by: Ask Hjorth Larsen <asklarsen@gmail.com>
Co-authored-by: Eric Larson <larson.eric.d@gmail.com>
Co-authored-by: igo95862 <igo95862@yandex.ru>
Co-authored-by: Naveen M K <naveen@syrusdark.website>
tk0miya added a commit to tk0miya/sphinx that referenced this issue Mar 13, 2021
…_resolver

The builtin_resolver() generates broken doctree unexpectedly if it
contains pending_xref_condition nodes.
@tk0miya
Copy link
Member

tk0miya commented Mar 13, 2021

Thank you for the example. It's very helpful to me! I believe #8996 will resolve the bug.

@alkasm
Copy link
Author

alkasm commented Mar 14, 2021

Thank you for the example. It's very helpful to me! I believe #8996 will resolve the bug.

Hey @tk0miya, I tried out your branch and I was able to verify it works! 🎉

However I also tried it out with the numpy example I gave in my OP for this issue and found a slight complication. It does work with default settings, but does not unqualify the types with autodoc_typehints = "description" in conf.py as I have in my OP. I still have the same output as in the OP. However if I remove that config value then I get a nice

multiply_polynomial(p: Polynomial, value: int) -> Tuple[Polynomial, Polynomial]

I updated the repo example to add the numpy method and have a commented line in conf.py to toggle the description type hints if you want to play with that example. Happy to have this discussion in a new issue if that's better for tracking btw, since the original intent of this issue is indeed solved.

tk0miya added a commit that referenced this issue Mar 14, 2021
Fix #7119: autodoc: Broken doctree was generated by builtin_resolver
@tk0miya
Copy link
Member

tk0miya commented Mar 14, 2021

Thank you for your confirmation. I merged the PR now. And I'll take a look the case of autodoc_typehints = "description" tomorrow. It would be helpful to me if you'll post another issue.
Thank you for your support!

@RuRo
Copy link
Contributor

RuRo commented May 16, 2021

@tk0miya I am having the same problem with sphinx 4.0.1, which seems to have 8996 already applied. I think, that I've tracked the issue to the sphinx-qt-documentation plugin. This plugin connects itself to missing-reference events like this

def setup(app: Sphinx) -> Dict[str, Any]:
    ... # snip
    app.connect("missing-reference", missing_reference)
    ... # snip

however, their missing_reference implementation doesn't have the

    content = find_pending_xref_condition(node, 'resolved')
    if content:
        contnode = content.children[0]  # type: ignore

snippet, which eventually leads to the above Unknown node: pending_xref_condition error.

I could submit this as a bug report to the author of sphinx-qt-documentation, but I wanted to make sure, that this behaviour is intended. It seems to me like a potential antipattern. For example, the above snippet (with minor variations) is already duplicated internally in

  • sphinx/domains/python.py:PythonDomain.resolve_xref
  • sphinx/domains/python.py:PythonDomain.resolve_any_xref
  • sphinx/domains/python.py:builtin_resolver
  • sphinx/ext/intersphinx.py:missing_reference

And any plugin that connects to the missing-reference event must also now add this snippet (which is complicated by the fact that find_pending_xref_condition doesn't seem to be available in sphinx<4). Do you think that maybe the contnode value should be resolved when the missing-reference event is created in sphinx/transforms/post_transforms/__init__.py?

P.S. I am not very familiar with the reference resolution code, so I might be 100% wrong.

@tk0miya
Copy link
Member

tk0miya commented May 16, 2021

@RuRo Could you file a new issue, please? I think your topic is surely related to this issue, but a different one

@RuRo
Copy link
Contributor

RuRo commented May 16, 2021

Sure, #9240.

@sidneycadot
Copy link

Hi - I came here by googling because I want autodoc to generate unqualified typenames for my Sphinx documentation, as suggested by the top post, eg. to prevent ugly type signatures like shown in the attachment.

Reading this thread it seems work was done to address this and the issue was closed, but I can't seem to find anything in the current Sphinx documentation that describes this feature, so I don't know how to get this to work (if it is even working).

Could anyone point me in the right direction?

Thanks!

image

@RuRo
Copy link
Contributor

RuRo commented May 24, 2021

@sidneycadot The python_use_unqualified_type_names config option is available since Sphinx 4.0.

However, bear in mind, that it's still a little buggy (experimental) and currently only shortens the type name if the reference could be resolved.

@sidneycadot
Copy link

sidneycadot commented May 24, 2021

@RuRo Interesting! I'll try tomorrow.

Is there a good reason why this is implemented as a top-level configuration variable rather than an auto doc-specific configuration variable? Are there contexts outside of autodoc where this variable could potentially be used?

(Sorry if that's a silly question@RuRu -- I am not very well versed in the innards of Sphinx.)

@RuRo
Copy link
Contributor

RuRo commented May 24, 2021

(Sorry if that's a silly question -- I am not very well versed in the innards of Sphinx.)

No problem, I also don't know, why was this implemented in the python domain instead of directly in autodoc :^).

@sidneycadot
Copy link

Well it doesn't do what I want it to do, unfortunately; but thanks for the suggestion anyway. :)

@RuRo
Copy link
Contributor

RuRo commented May 25, 2021

Well it doesn't do what I want it to do, unfortunately; but thanks for the suggestion anyway. :)

Really? Why not? From your description, I would've expected python_use_unqualified_type_names = True together with add_module_names = False to remove most of the noise in the signature. If you have some free time, consider putting together a minimal example and submitting it as an issue (either as a bug in python_use_unqualified_type_names or as a feature request).

@sidneycadot
Copy link

Really? Why not?

The variables you mention do influence the observed behavior, but they leave some module prefixes in place. See the attached image.

If you have some free time, consider putting together a minimal example and submitting it as an issue

I will consider it. But, frankly; if I were to do that for all sphinx and autodoc issues I have encountered since starting to write documentation in earnest, it would cost way too much time. It has not been a great experience, I am sorry to say.

I am now strongly considering to do a bit of docutils diving to make an extension that will be able to clean up the generated documentation at a late processing stage by simply doing a regexp-based search-and-replace on anything and everything inside the doctree that isn't to my liking. This is a big and rather unsophisticated hammer that could fix the effect of this particular issue, and several more I have come across. I think my time is spent better doing that; chasing all the different bugs on their own is not very enticing and would take a lot longer.

image

@sidneycadot
Copy link

sidneycadot commented May 26, 2021

@RuRo I accidentally bumped in the solution.

As you can see in the examples I present I have an enum type with an initializer. Such an initializer is rendered by autodoc using the repr() function, which for Enum types does something nasty: it emits eg <EnumClass.enumvalue: 10>, rather than the more natural EnumClass.enumValue.

This somehow confuses the code in autodoc that is responsible for signature handling. In particular, after overriding the __repr__ function of my enum class to emit a sane string, everything works like a charm and I get what I want:

https://pydwf.readthedocs.io/en/latest/pydwf_api/pydwf_utilities.html

So the root cause of my issue was that the standard library enum.Enum.__repr__ doesn't honor the convention of returning a string that can be eval'ed. As I understand it, there's ongoing discussion on the Python mailing list to fix it:

https://bugs.python.org/issue40066

In the mean time, I have filed a bug report with Sphinx to see if they can implement a special-case workaround for this type:

#9272

So as for this particular issue, I am satisfied now — I implemented custom __repr__ methods, as suggested as a workaround in my bug report, for all my enum classes and all tools are now happily humming along.

Thanks for your assistance. Most specifically, what triggered me to keep digging was your surprise a few posts back that it didn't work.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
extensions:autodoc type:enhancement enhance or introduce a new feature
Projects
None yet
Development

No branches or pull requests

6 participants