Skip to content

Commit cba1453

Browse files
committed
Clean sys.modules if TYPE_CHECKING=True import fails
autodoc now attempts to importing with `typing.TYPE_CHECKING = True` first, and then falls back to `typing.TYPE_CHECKING = False` if that fails. Unfortunately, the first import can leave behind some partially-imported modules in `sys.modules` such that the retry fails. Attempt to work around this by detecting what modules were added to `sys.modules` by the first attempt and removing them before retrying. Signed-off-by: Matt Wozniski <[email protected]>
1 parent 2f6ea14 commit cba1453

File tree

6 files changed

+21
-1
lines changed

6 files changed

+21
-1
lines changed

sphinx/ext/autodoc/importer.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import importlib
6+
import sys
67
import traceback
78
import typing
89
from typing import TYPE_CHECKING, Any, Callable, NamedTuple
@@ -82,13 +83,18 @@ def import_object(modname: str, objpath: list[str], objtype: str = '',
8283
objpath = list(objpath)
8384
while module is None:
8485
try:
86+
orig_modules = set(sys.modules)
8587
try:
8688
# try importing with ``typing.TYPE_CHECKING == True``
8789
typing.TYPE_CHECKING = True
8890
module = import_module(modname, warningiserror=warningiserror)
8991
except ImportError:
9092
# if that fails (e.g. circular import), retry with
91-
# ``typing.TYPE_CHECKING == False``
93+
# ``typing.TYPE_CHECKING == False`` after reverting
94+
# changes made to ``sys.modules`` by the failed try
95+
for m in set(sys.modules) - orig_modules:
96+
sys.modules.pop(m)
97+
9298
typing.TYPE_CHECKING = False
9399
module = import_module(modname, warningiserror=warningiserror)
94100
finally:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from circular_import.c import SomeClass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
X = 42
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import typing
2+
3+
if typing.TYPE_CHECKING:
4+
from circular_import import SomeClass
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import circular_import.a
2+
import circular_import.b
3+
4+
5+
class SomeClass:
6+
X = circular_import.a.X

tests/roots/test-ext-autodoc/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@
1313
.. autofunction:: target.overload.sum
1414

1515
.. autofunction:: target.typehints.tuple_args
16+
17+
.. automodule:: circular_import

0 commit comments

Comments
 (0)