Sphinx: Function/class signatures fail with Py3 style annotations and typing.TYPE_CHECKING

Created on 2 Mar 2017  路  3Comments  路  Source: sphinx-doc/sphinx

Problem and how to reproduce

Function/class signatures do not work properly when using Py3 style annotations, and type definitions are conditionally imported using typing.TYPE_CHECKING.

Minimal example:

import typing

if typing.TYPE_CHECKING:
    from .session import Session


class Test:
    def __init__(self, param: 'Session') -> None:
        pass

I use autodoc to write documentation about Test class in my index.rst:

.. autoclass:: mytest.test.Test
   :members:

This causes the following error

..../classes.rst:4: WARNING: error while formatting arguments for ...test.Test: name 'Session' is not defined

Documentation does not display class initialization signature (nor any other function/method possible signatures) at all.

Expected results

Documentation displays class and its signatures properly.

Environment info

  • OS: Linux
  • Python version: 3.6
  • Sphinx version: 1.5.3
autodoc bug

Most helpful comment

What is the current status of this issue? The problem is that with typing you can do special imports, if typing.TYPE_CHECKING and import modules you only need for type checking to avoid circular imports and sphinx is not importing those modules.

All 3 comments

What is the current status of this issue? The problem is that with typing you can do special imports, if typing.TYPE_CHECKING and import modules you only need for type checking to avoid circular imports and sphinx is not importing those modules.

Is there an update on this issue? Does anyone have a workaround until a fix is merged? other than removing type hints

As always, big thank you to the maintainers for an incredible tool.

It seems the latest Sphinx (v3.1.2) does not raise any warnings on building document using the reported code. I don't know which release fixed this.
Anyway, this issue was already resolved. Now I'm closing this.
Please file a new issue if you still have the same error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oonid picture oonid  路  3Comments

webknjaz picture webknjaz  路  3Comments

shimizukawa picture shimizukawa  路  3Comments

shimizukawa picture shimizukawa  路  3Comments

jfbu picture jfbu  路  3Comments