From 2af7193c67c7426fd2b2d4f86e62feb3bd3e9e10 Mon Sep 17 00:00:00 2001 From: Pablo Aguilar Date: Tue, 18 Jan 2022 00:18:26 -0300 Subject: [PATCH 1/3] Prevents reaching inner blocks that contains `if TYPE_CHECKING` --- src/sphinx_autodoc_typehints/__init__.py | 2 +- .../roots/test-resolve-typing-guard/demo_typing_guard.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/sphinx_autodoc_typehints/__init__.py b/src/sphinx_autodoc_typehints/__init__.py index d973331a..0876ca74 100644 --- a/src/sphinx_autodoc_typehints/__init__.py +++ b/src/sphinx_autodoc_typehints/__init__.py @@ -271,7 +271,7 @@ def get_all_type_hints(obj: Any, name: str) -> dict[str, Any]: return _get_type_hint(name, obj) -_TYPE_GUARD_IMPORT_RE = re.compile(r"if (typing.)?TYPE_CHECKING:[^\n]*([\s\S]*?)(?=\n\S)") +_TYPE_GUARD_IMPORT_RE = re.compile(r"\nif (typing.)?TYPE_CHECKING:[^\n]*([\s\S]*?)(?=\n\S)") _TYPE_GUARD_IMPORTS_RESOLVED = set() diff --git a/tests/roots/test-resolve-typing-guard/demo_typing_guard.py b/tests/roots/test-resolve-typing-guard/demo_typing_guard.py index 281de406..1399bf44 100644 --- a/tests/roots/test-resolve-typing-guard/demo_typing_guard.py +++ b/tests/roots/test-resolve-typing-guard/demo_typing_guard.py @@ -29,6 +29,15 @@ def a(f: Decimal, s: AnyStr) -> Sequence[AnyStr | Decimal]: return [f, s] +class SomeClass: + """This class do something.""" + + if TYPE_CHECKING: # Classes doesn't have `__globals__` attribute + + def __getattr__(self, item: str): # noqa: U100 + """This method do something.""" + + __all__ = [ "a", "ValueError", From c20c03440ba234794157c81425c026d4547af8ea Mon Sep 17 00:00:00 2001 From: Pablo Aguilar Date: Tue, 18 Jan 2022 14:17:03 -0300 Subject: [PATCH 2/3] Updates `CHANGELOG.md` --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a1e3de5..abcd72f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## WIP + +- Prevents reaching inner blocks that contains `if TYPE_CHECKING` + ## 1.15.2 - Log a warning instead of crashing when a type guard import fails to resolve From b13d0a35f40b15a44563df95e1fa9387de670121 Mon Sep 17 00:00:00 2001 From: Pablo Aguilar Date: Tue, 18 Jan 2022 14:27:20 -0300 Subject: [PATCH 3/3] Updates `CHANGELOG.md` --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index abcd72f0..5237d3b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Changelog -## WIP +## 1.15.3 - Prevents reaching inner blocks that contains `if TYPE_CHECKING`