Skip to content

NewType and TypeVar don't resolve correctly #8415

Closed

Description

@anselor

Describe the bug
Autodoc generates warnings and doesn't resolve NewType or TypeVar correctly.

To Reproduce
Steps to reproduce the behavior:

$ git clone https://github.com/anselor/sphinx-issues-examples/
$ cd sphinx-issues-examples/typevar-namespace
$ python -m sphinx -b html docs/ doc-out/ -nvWT

Expected behavior
Documentation generated with TypeVar and NewType names that link to the documented declaration

Your project
https://github.com/anselor/sphinx-issues-examples/tree/main/typevar-namespace

Screenshots
If applicable, add screenshots to help explain your problem.

Environment info

  • OS: Ubuntu 18.04, 20.04
  • Python version: 3.6.9, 3.8.2
  • Sphinx version: 3.2.1
  • Sphinx extensions: sphinx.ext.autodoc
  • Extra tools:

Additional context
Add any other context about the problem here.

  • [e.g. URL or Ticket]

Activity

  1. anselor commented on Nov 12, 2020

    @anselor
    Author

    @tk0miya FYI - this is the new issue I created as you suggested.

  2. tk0miya commented on Nov 14, 2020

    @tk0miya
    Member

    @anselor I can't reproduce any warnings from your example.

    This is a Dockerfile to reproduce the warning:

    FROM python:3.8-slim
    
    RUN apt update; apt install -y build-essential curl git make unzip vim
    RUN git clone https://github.com/anselor/sphinx-issues-examples/
    RUN pip install Sphinx
    WORKDIR /sphinx-issues-examples/typevar-namespace/src
    RUN pip install -e .
    WORKDIR /sphinx-issues-examples/typevar-namespace/docs
    RUN sphinx-build . _build/html
    

    And its result:

    Running Sphinx v3.3.1
    making output directory... done
    loading intersphinx inventory from https://docs.python.org/3/objects.inv...
    building [mo]: targets for 0 po files that are out of date
    building [html]: targets for 1 source files that are out of date
    updating environment: [new config] 1 added, 0 changed, 0 removed
    reading sources... [100%] index
    
    looking for now-outdated files... none found
    pickling environment... done
    checking consistency... done
    preparing documents... done
    writing output... [100%] index
    
    generating indices... genindex py-modindex done
    writing additional pages... search done
    copying static files... done
    copying extra files... done
    dumping search index in English (code: en)... done
    dumping object inventory... done
    build succeeded.
    
    The HTML pages are in _build/html.
    
  3. anselor commented on Nov 14, 2020

    @anselor
    Author

    I was using 3.2.1 - just updated to sphinx 3.3.1 to make sure. The steps in your docker container do not match the same steps/options that I documented in the ticket.

    I tried running it the way you have in your docker container and can confirm that sphinx doesn't report any errors but silently failed to resolve symbols or link them. Note that a locally declared TypeVar resolves correctly but a TypeVar imported from another module fails. I've underlined them in green and red in the output here:
    image

    For NewType it doesn't recognize the NewType name and generates this instead:
    image

    I would expect it to say:
    special() -> SpecialType and link to the decleration of SpecialType

  4. tk0miya commented on Nov 15, 2020

    @tk0miya
    Member

    Sorry, I did not pass -n option to sphinx-build command. Now I reproduced the error. Okay, I'll take a look again.

    FROM python:3.8-slim
    
    RUN apt update; apt install -y build-essential curl git make unzip vim
    RUN git clone https://github.com/anselor/sphinx-issues-examples/
    RUN pip install Sphinx
    WORKDIR /sphinx-issues-examples/typevar-namespace/src
    RUN pip install -e .
    WORKDIR /sphinx-issues-examples/typevar-namespace/docs
    RUN sphinx-build -nvWT .  _build/html
    
    Running Sphinx v3.3.1
    making output directory... done
    loading intersphinx inventory from https://docs.python.org/3/objects.inv...
    building [mo]: targets for 0 po files that are out of date
    building [html]: targets for 1 source files that are out of date
    updating environment: [new config] 1 added, 0 changed, 0 removed
    reading sources... [100%] index
    
    looking for now-outdated files... none found
    pickling environment... done
    checking consistency... done
    preparing documents... done
    writing output... [100%] index
    
    
    Traceback (most recent call last):
      File "/usr/local/lib/python3.8/site-packages/sphinx/cmd/build.py", line 280, in build_main
        app.build(args.force_all, filenames)
      File "/usr/local/lib/python3.8/site-packages/sphinx/application.py", line 352, in build
        self.builder.build_update()
      File "/usr/local/lib/python3.8/site-packages/sphinx/builders/__init__.py", line 297, in build_update
        self.build(to_build,
      File "/usr/local/lib/python3.8/site-packages/sphinx/builders/__init__.py", line 361, in build
        self.write(docnames, list(updated_docnames), method)
      File "/usr/local/lib/python3.8/site-packages/sphinx/builders/__init__.py", line 535, in write
        self._write_serial(sorted(docnames))
      File "/usr/local/lib/python3.8/site-packages/sphinx/builders/__init__.py", line 545, in _write_serial
        self.write_doc(docname, doctree)
      File "/usr/local/lib/python3.8/contextlib.py", line 120, in __exit__
        next(self.gen)
      File "/usr/local/lib/python3.8/site-packages/sphinx/util/logging.py", line 213, in pending_warnings
        memhandler.flushTo(logger)
      File "/usr/local/lib/python3.8/site-packages/sphinx/util/logging.py", line 178, in flushTo
        logger.handle(record)
      File "/usr/local/lib/python3.8/logging/__init__.py", line 1587, in handle
        self.callHandlers(record)
      File "/usr/local/lib/python3.8/logging/__init__.py", line 1649, in callHandlers
        hdlr.handle(record)
      File "/usr/local/lib/python3.8/logging/__init__.py", line 946, in handle
        rv = self.filter(record)
      File "/usr/local/lib/python3.8/logging/__init__.py", line 807, in filter
        result = f.filter(record)
      File "/usr/local/lib/python3.8/site-packages/sphinx/util/logging.py", line 421, in filter
        raise exc
    sphinx.errors.SphinxWarning: /sphinx-issues-examples/typevar-namespace/src/issue/typevar/support.py:docstring of issue.typevar.support.MyClass.special::py:class reference target not found: NewType.<locals>.new_type
    
    Warning, treated as error:
    /sphinx-issues-examples/typevar-namespace/src/issue/typevar/support.py:docstring of issue.typevar.support.MyClass.special::py:class reference target not found: NewType.<locals>.new_type
    The command '/bin/sh -c sphinx-build -nvWT  .  _build/html' returned a non-zero code: 2
    
  5. tk0miya commented on Nov 15, 2020

    @tk0miya
    Member

    I posted #8426 to fix the TypeVar warnings.

    On the other hand, it is difficult to fix the case of NewType. It is hard to get the original name of the type in runtime. So please use autodoc_type_hints to resolve the name. It will resolve your case.
    https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_type_aliases

  6. tk0miya commented on Nov 15, 2020

    @tk0miya
    Member

    Do you know how to detect is the type a kind of NewType? When should we access __name__?

  7. anselor commented on Nov 15, 2020

    @anselor
    Author

    They way it's implemented, NewType() returns a function that has 2 additional attributes: __name__ and __supertype__.
    In my code that does some runtime type validation I check for these things:

    Does __qualname__ equal NewType.<locals>.new_type ?
    Do __name__ and __supertype__ exist as attributes?

  8. tk0miya commented on Nov 15, 2020

    @tk0miya
    Member

    Hmm... I feel it's a bit fragile. But, I understand it is better to support NewType as possible by default. Additionally, I need to know how to get the module of the new type. In this case, we need to get issue.typevar.support.SpecialInt from the NewType object. I'll take a look later.

  9. anselor commented on Nov 15, 2020

    @anselor
    Author

    Hmm, I see what you mean. It looks like the __name__ field is from the string passed into the typing.NewType() function. The original module/namespace context doesn't appear to be preserved in any way.

    Something probably needs to be submitted to the Python project if we want that changed.

    Would it be possible to have the option to automatically register any NewType declarations in the autodoc_type_aliases dictionary?
    Also, I noticed in the generated output that NewType and TypeVar don't have any automatic indication of what type they're declared as. Perhaps that's something that can be resolved in the context of where it is declared? For example, it would be nice if TypeVar auto-documented what, if any, type it is bound to and whether it is invariant, covariant, or contravariant (https://github.com/python/cpython/blob/2837241f22be33a5597707b2aa723cb2cf6f3967/Lib/typing.py#L567).
    And for NewType, it would be nice to auto-document the __supertype__. Here is the current generated output for TypeVar and NewType:
    image

    If the above suggestions don't work, a reasonable fallback approach would be to acknowledge that it is a NewType alias and link to the provided __supertype__.

  10. 4 remaining items

  11. modified the milestones: 3.5.0, 4.0.0 on Jan 24, 2021
  12. anselor commented on Feb 9, 2021

    @anselor
    Author

    I've been unable to try the latest changes because there are regressions on 3.4 that fail to run for me.

  13. tk0miya commented on Feb 28, 2021

    @tk0miya
    Member

    I just merge #8426. It will be released as 4.0 this April.
    Thanks!

  14. anselor commented on Mar 4, 2021

    @anselor
    Author

    @tk0miya I tried various ways to use autodoc_type_aliases and it doesn't seem to do anything useful. The only way I'm able to move forward is adding them all as nitpick ignore.

  15. tk0miya commented on Mar 5, 2021

    @tk0miya
    Member

    If you're saying about NewType, there is nothing to do from Sphinx. Please request python to store the location to NewType objects to document it.

  16. anselor commented on Mar 5, 2021

    @anselor
    Author

    I thought you'd suggested that using autodoc_type_aliases to manually map the name over would be a work-around? When I added the NewType names to the map it had no effect on the output.

  17. anselor commented on Mar 6, 2021

    @anselor
    Author

    I figured out a work-around for making NewType resolve. @tk0miya

    After a NewType declaration, If I add a line afterwards to manually specify the __qualname__ Sphinx seems to figure things out.

    
    SpecialInt = NewType('SpecialInt', int)
    SpecialInt.__qualname__ = f'{__name__}.{SpecialInt.__name__}'
    
    
  18. anselor commented on Mar 8, 2021

    @anselor
    Author

    @tk0miya
    Would it be possible to, on startup, have Sphinx monkeypatch NewType. Then, implement a NewType wrapper that inspects the call stack to figure out the context from which it was called. Using that, fill out qualname before returning it?

  19. added a commit that references this issue on Mar 11, 2021
  20. locked as resolved and limited conversation to collaborators on Jul 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions