Skip to content

Commit

Permalink
fix: Add missing support for from __future__ import annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
dangotbanned committed Jan 4, 2025
1 parent 4944b80 commit 7cd2a77
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions sphinxext/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def create_generic_image(
"""


def _parse_source_file(filename: str) -> tuple[ast.Module | None, str]:
def _parse_source_file(filename: str | Path) -> tuple[ast.Module | None, str]:
"""
Parse source file into AST node.
Expand Down Expand Up @@ -88,7 +88,7 @@ def _parse_source_file(filename: str) -> tuple[ast.Module | None, str]:
return node, content


def get_docstring_and_rest(filename: str) -> tuple[str, str | None, str, int]:
def get_docstring_and_rest(filename: str | Path) -> tuple[str, str | None, str, int]: # noqa: C901
"""
Separate ``filename`` content between docstring and the rest.
Expand All @@ -115,6 +115,7 @@ def get_docstring_and_rest(filename: str) -> tuple[str, str | None, str, int]:
This function adapted from the sphinx-gallery project; license: BSD-3
https://github.com/sphinx-gallery/sphinx-gallery/
"""
FUTURE_STATEMENT = "from __future__ import annotations"
node, content = _parse_source_file(filename)

# Find the category comment
Expand Down Expand Up @@ -157,10 +158,16 @@ def get_docstring_and_rest(filename: str) -> tuple[str, str | None, str, int]:

except AttributeError:
# this block can be removed when python 3.6 support is dropped
if (
isinstance(node.body[0], ast.ImportFrom)
and node.body[0].module == "__future__"
):
node.body.pop(0)
content = content.replace(FUTURE_STATEMENT, "").lstrip("\n")
if (
node.body
and isinstance(node.body[0], ast.Expr)
and isinstance(node.body[0].value, (ast.Str, ast.Constant))
and isinstance(node.body[0].value, ast.Constant)
):
docstring_node = node.body[0]
docstring = docstring_node.value.s # pyright: ignore[reportAttributeAccessIssue]
Expand Down

0 comments on commit 7cd2a77

Please sign in to comment.