Skip to content

Commit

Permalink
Prevents reaching inner blocks that contains if TYPE_CHECKING (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
thepabloaguilar authored Jan 18, 2022
1 parent 01b3a1a commit 3492d49
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.15.3

- 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
Expand Down
2 changes: 1 addition & 1 deletion src/sphinx_autodoc_typehints/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
9 changes: 9 additions & 0 deletions tests/roots/test-resolve-typing-guard/demo_typing_guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 3492d49

Please sign in to comment.