Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevents reaching inner blocks that contains if TYPE_CHECKING #211

Merged
merged 3 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)")
gaborbernat marked this conversation as resolved.
Show resolved Hide resolved
_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
gaborbernat marked this conversation as resolved.
Show resolved Hide resolved
"""This method do something."""


__all__ = [
"a",
"ValueError",
Expand Down