sphinx-autodoc-typehints icon indicating copy to clipboard operation
sphinx-autodoc-typehints copied to clipboard

sphinx.ext.autosummary extension overrides the typing.TYPE_CHECKING flag (to False)

Open lmmx opened this issue 4 years ago • 2 comments

Describe the bug The sphinx autosummary extension turns off the typing.TYPE_CHECKING flag used by the sphinx-autodoc-typehints extension to do conditional imports of annotation types (among other things), you may want to document this in your README (I just lost a day to searching it out)

To Reproduce Add "sphinx.ext.autosummary" to the extensions list (before "sphinx-autodoc-typehints") in your docs/conf.py and add a print statement for the typing.TYPE_CHECKING flag (compare before and after to see that it is switched off)

  • If it makes life any easier, I made a minimal reproducible repo to show this here but I'm sure it's trivial to observe this yourself.
    • Since this repo is now 'fixed', add "sphinx.ext.autosummary" to the extensions to observe it break (all the library does is print the typing.TYPE_CHECKING variable)

Moving it after "sphinx-autodoc-typehints" reverts the behaviour (the print out goes back to TYPE_CHECKING = True).

Perhaps worth adding a note on this to the README (like you already had for Napoleon), I couldn't figure out what was causing this!

Also to be constructive, do you think it’d be wise to add a test for this? Along the lines of “if the set_check_typing_flag param was set to True and the TYPE_CHECKING flag does not end up being True, raise an error”?

detected_check_typing_flag = SomehowDetectValueOf("typing.TYPE_CHECKING")
if app.config.set_check_typing_flag and not detected_check_typing_flag:
    raise ValueError`

From looking at the code, I’m not sure if there is any such ‘view’ into the process where you’d be able to run such a function SomehowDetectValueOf, any suggestion/thoughts?

  • The typing.TYPE_CHECKING is just a module-level constant
  • I suspect it'd perhaps be possible to check after the app runs if typing.TYPE_CHECKING has a different value

lmmx avatar Jul 17 '21 20:07 lmmx

As explained in the attached issue, this can be fixed by changing the priority of the connect call for the builder-inited event from its default of 500 to any lower integer. I suggested 400 in the associated bug report, awaiting comment for/against I'll just leave this note here and come back to it in a week or so.

lmmx avatar Jul 19 '21 22:07 lmmx

A PR for this would be welcome.

gaborbernat avatar Jan 08 '22 11:01 gaborbernat