pdoc icon indicating copy to clipboard operation
pdoc copied to clipboard

Documentation should be generated with `typing.TYPE_CHECKING == True`

Open USSX-Hares opened this issue 5 months ago • 2 comments

TL;DR

pdoc should set typing.TYPE_CHECKING to True.

Long Read

I was investigating a root cause of #459, and to apply the workaround, I added an import within the if (typing.TYPE_CHECKING) condition. However, this didn't work, and it forced me to extract the import in question outside of the if statement. Then I've searched for the TYPE_CHECKING in the entire source tree, and found only two occurrences inside _version.py.

Enabling this will allow "heavy" or otherwise not needed imports to occur, and thus will allow proper linking between the modules.

Looks like this is an easy fix, and the flag should be set before generating the modules' documentation, probably in pdoc.Module class or in cli.main.

USSX-Hares avatar Jul 01 '25 19:07 USSX-Hares

Suggestion: Import all PDoc's stuff, then start rendering. Each module is rendered within the context manager, which sets typing.TYPE_CHECKING before the import, imports the module, and then resets the value. If the import was unsuccessful, unload that module and try without typing.TYPE_CHECKING.

Also I can suggest making this an optional behaviour, controlled by command-line args and/or configs.

USSX-Hares avatar Jul 21 '25 18:07 USSX-Hares

Can you also make the source code suggestion? 😅

Trying both ways of having it configurable is imho the only good solution, because some heavy deps can be hidden exactly behind if TYPE_CHECKING guards, which may be considered statically evaluated.

kernc avatar Jul 23 '25 22:07 kernc