Sphinx: Forward references guarded by typing.TYPE_CHECKING raises warnings with Sphinx 1.8.0/1.8.1

Created on 23 Sep 2018  路  4Comments  路  Source: sphinx-doc/sphinx

Building the documentation that uses the typing.TYPE_CHECKING constant to break import cycles as suggested in the mypy docs here results in a Warning with 1.8.0/1 that was not raised with 1.7.9. I am trying to build the docs of our project that unfortunately has a circular reference of this type warning free.

This is similar to #4072 but a new change with Sphinx 1.8.0 that does not relate to the use of the sphinx-autodoc-typehints extension.

I believe that the output is functionally identical to 1.7.9 but now raises a warning that was silent before. Raising the warning is probably correct since the conditionally imported type annotation was never expanded correctly. I have not looked into pep563 style postponed evaluation

Procedure to reproduce the problem

Build html docs of the attached project with make html

Error logs / results

A warning of an Ignored NameError is raised

WARNING: Invalid type annotation found on <function AClass.__init__ at 0x0000028BD9998620>. Ignored: NameError("name 'B' is not defined",)

Expected results

Build without warnings

Reproducible project / your project

The latest tip of https://github.com/jenshnielsen/sphinx_issue builds without warnings with
Sphinx 1.7.9 but produces Warning with 1.8.0/1. This is a slightly modified version of the code used
to reproduce #4072

Environment info

  • OS: Windows 10
  • Python version: 3.6.6
  • Sphinx version: 1.8.1
autodoc bug

All 4 comments

Fixed by #5535.
Thank you for reporting.

@tk0miya Thanks

A note for future people who come across this - circular imports can still be an issue if using sphinx-autodoc-typehints version 1.4.0, because it sets typing.TYPE_CHECKING = True. The most recent version released to PyPI without this change is 1.3.0.

Their README suggests a solution - to import the module instead of the object you're using in the annotation.

Additional note to future readers, sphinx-autodoc-typehints >= 1.5.0 allows you to disable type checking via set_type_checking_flag = False in conf.py to silence this warning.

Was this page helpful?
0 / 5 - 0 ratings