Skip to content

Commit 3492d49

Browse files
Prevents reaching inner blocks that contains if TYPE_CHECKING (#211)
1 parent 01b3a1a commit 3492d49

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 1.15.3
4+
5+
- Prevents reaching inner blocks that contains `if TYPE_CHECKING`
6+
37
## 1.15.2
48

59
- Log a warning instead of crashing when a type guard import fails to resolve

src/sphinx_autodoc_typehints/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def get_all_type_hints(obj: Any, name: str) -> dict[str, Any]:
271271
return _get_type_hint(name, obj)
272272

273273

274-
_TYPE_GUARD_IMPORT_RE = re.compile(r"if (typing.)?TYPE_CHECKING:[^\n]*([\s\S]*?)(?=\n\S)")
274+
_TYPE_GUARD_IMPORT_RE = re.compile(r"\nif (typing.)?TYPE_CHECKING:[^\n]*([\s\S]*?)(?=\n\S)")
275275
_TYPE_GUARD_IMPORTS_RESOLVED = set()
276276

277277

tests/roots/test-resolve-typing-guard/demo_typing_guard.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ def a(f: Decimal, s: AnyStr) -> Sequence[AnyStr | Decimal]:
2929
return [f, s]
3030

3131

32+
class SomeClass:
33+
"""This class do something."""
34+
35+
if TYPE_CHECKING: # Classes doesn't have `__globals__` attribute
36+
37+
def __getattr__(self, item: str): # noqa: U100
38+
"""This method do something."""
39+
40+
3241
__all__ = [
3342
"a",
3443
"ValueError",

0 commit comments

Comments
 (0)