Skip to content
Draft
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
1 change: 1 addition & 0 deletions doc/changelog.d/856.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Resolve type checking errors
17 changes: 10 additions & 7 deletions src/ansys_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from typing import Any

from docutils import nodes
from pydata_sphinx_theme.toctree import traverse_or_findall
from pydata_sphinx_theme.toctree import traverse_or_findall # type: ignore
from sphinx import addnodes
from sphinx.addnodes import toctree
from sphinx.application import Sphinx
Expand All @@ -50,11 +50,11 @@
)

try:
import importlib.metadata as importlib_metadata
import importlib.metadata as importlib_metadata_pkg
except ModuleNotFoundError: # pragma: no cover
import importlib_metadata
import importlib_metadata as importlib_metadata_pkg # type: ignore

__version__ = importlib_metadata.version(__name__.replace(".", "-"))
__version__ = importlib_metadata_pkg.version(__name__.replace(".", "-"))
logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -222,7 +222,7 @@ def fix_edit_html_page_context(
see https://github.com/pyvista/pyvista/pull/4113
"""

def fix_edit_link_page(link: str) -> str:
def fix_edit_link_page(link: str) -> str | None:
"""Transform "edit on GitHub" links to the correct URL.

This function fixes the URL for the "edit this page" link.
Expand Down Expand Up @@ -270,6 +270,7 @@ def fix_edit_link_page(link: str) -> str:
except ValueError as e:
logger.error(f"An error occurred: {e}") # Log the exception as debug info
return link
return link

elif "api" in pagename:
for obj_node in list(doctree.findall(addnodes.desc)):
Expand All @@ -288,6 +289,7 @@ def fix_edit_link_page(link: str) -> str:
return f"http://github.com/{github_user}/{github_repo}/edit/{kind}/{github_source}/{modname}.{domain}" # noqa: E501
else:
return f"http://github.com/{github_user}/{github_repo}/edit/{kind}/{modname}.{domain}" # noqa: E501
return link

else:
return link
Expand Down Expand Up @@ -439,7 +441,8 @@ def add_sidebar_context(

if whatsnew_pages and pagename in whatsnew_pages:
whatsnew = context.get("whatsnew", [])
whatsnew.extend(app.env.whatsnew)
if hasattr(app.env, "whatsnew"):
whatsnew.extend(app.env.whatsnew)
context["whatsnew"] = whatsnew
sidebars_to_add.append("whatsnew")

Expand Down Expand Up @@ -584,7 +587,7 @@ def setup(app: Sphinx) -> dict:
"""
# Add the theme configuration
theme_path = get_html_theme_path()
app.add_html_theme("ansys_sphinx_theme", theme_path)
app.add_html_theme("ansys_sphinx_theme", str(theme_path))
app.config.templates_path.append(str(THEME_PATH / "components"))

# Add default HTML configuration
Expand Down
2 changes: 1 addition & 1 deletion src/ansys_sphinx_theme/cheatsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def build_quarto_cheatsheet(app: Sphinx) -> None:
"--to",
"cheat_sheet-pdf",
"--output-dir",
output_dir_path,
str(output_dir_path),
"-V",
f"version={version}",
],
Expand Down
2 changes: 1 addition & 1 deletion src/ansys_sphinx_theme/extension/autoapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def add_autoapi_theme_option(app: Sphinx, config: Dict[str, Any]) -> None:
app : ~sphinx.application.Sphinx
Application instance for rendering the documentation.
"""
autoapi = config.html_theme_options.get("ansys_sphinx_theme_autoapi", {})
autoapi = app.config.html_theme_options.get("ansys_sphinx_theme_autoapi", {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am surprised about this. This is not a type hint error. It is a different object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think mypy raised this error due to an object type mismatch. We typed config as a dict, but later we tried to access its attributes, which caused the mypy error. In practice, both approaches behave the same in this case, so I added the change. We can safely ignore this mypy error.

if not autoapi:
return
autoapi_template_dir = autoapi.get("templates", "")
Expand Down
13 changes: 8 additions & 5 deletions src/ansys_sphinx_theme/extension/linkcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@

def sphinx_linkcode_resolve(
domain: str, info: dict, library: str, source_path: str, github_version: str, edit: bool = False
) -> str or None:
) -> str | None:
"""
Resolve the URL corresponding to a Python object for linking to the source code.

Expand Down Expand Up @@ -144,19 +144,22 @@ def sphinx_linkcode_resolve(
except Exception:
return None

fn = Path(fn).resolve()
if not fn:
return None

fn_path = Path(fn).resolve()

# Ensure fn is within the expected repository directory
base_path = Path(__file__).resolve().parent
if not fn.is_relative_to(base_path):
if not fn_path.is_relative_to(base_path):
return None # Avoid errors due to files outside the expected path

try:
fn = fn.relative_to(base_path)
fn_path = fn_path.relative_to(base_path)
except ValueError:
return None # Avoid errors when the file is not within the base path

fn_components = list(fn.parts)
fn_components = list(fn_path.parts)

if not source_path:
module = modname.split(".")[0]
Expand Down
5 changes: 3 additions & 2 deletions src/ansys_sphinx_theme/navbar_dropdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import bs4
from docutils import nodes
import sphinx
from sphinx.application import Sphinx
from sphinx.util import logging
from sphinx.util.nodes import make_refnode
import yaml
Expand Down Expand Up @@ -71,7 +72,7 @@ def load_navbar_configuration(app: sphinx.application.Sphinx) -> None:


def update_template_context(
app: sphinx, pagename: str, templatename: str, context: dict, doctree: nodes.document | None
app: Sphinx, pagename: str, templatename: str, context: dict, doctree: nodes.document | None
) -> None:
"""Inject navbar rendering logic into the Sphinx HTML template context."""

Expand All @@ -83,7 +84,7 @@ def render_navbar_links_html() -> bs4.BeautifulSoup:

nav_root = nodes.container(classes=["navbar-content"])
nav_root.append(build_navbar_nodes(app.config.navbar_contents))
rendered = app.builder.render_partial(nav_root)["fragment"]
rendered = app.builder.render_partial(nav_root)["fragment"] # type: ignore
return add_navbar_chevrons(bs4.BeautifulSoup(rendered, "html.parser"))

def build_navbar_nodes(entries: list[NavEntry], is_top_level: bool = True) -> nodes.bullet_list:
Expand Down
28 changes: 18 additions & 10 deletions src/ansys_sphinx_theme/whatsnew.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

import pathlib
import re
from typing import Iterable
from typing import Iterator

from docutils import nodes
from sphinx.application import Sphinx
Expand Down Expand Up @@ -133,7 +133,7 @@ def add_whatsnew_changelog(app: Sphinx, doctree: nodes.document) -> None:
# Get the semantic version number from the section title link
next_node = node.next_node(nodes.reference)
# Get the name of the section title link
version = next_node.get("name")
version = next_node.get("name") if next_node else None

if version:
# Create the minor version from the patch version
Expand Down Expand Up @@ -188,7 +188,7 @@ def get_whatsnew_data(whatsnew_file: pathlib.Path) -> dict:

# Create a dictionary containing the what's new data for each minor version
# For example: { minor_version: [fragment1_dict, fragment2_dict, ...] }
minor_version_whatsnew_data = {}
minor_version_whatsnew_data: dict[str, list] = {}
for fragment in whatsnew_data["fragments"]:
# Get the minor version from the fragment version
whatsnew_minor_version = ".".join(fragment["version"].split(".")[:2])
Expand All @@ -201,6 +201,8 @@ def get_whatsnew_data(whatsnew_file: pathlib.Path) -> dict:

return minor_version_whatsnew_data

return {}


def add_whatsnew_section(minor_version: str, whatsnew_data: dict) -> nodes.section:
"""Add the what's new section and dropdowns for each fragment in the whatsnew.yml file.
Expand Down Expand Up @@ -258,7 +260,7 @@ def add_whatsnew_section(minor_version: str, whatsnew_data: dict) -> nodes.secti
content_iterator = iter(content_lines)

# Navigate to first line in the iterator
line = next(content_iterator, None)
line = next(iter(content_iterator), None)

while line is not None:
if ".. code" in line or ".. sourcecode" in line:
Expand Down Expand Up @@ -290,7 +292,9 @@ def add_whatsnew_section(minor_version: str, whatsnew_data: dict) -> nodes.secti
return minor_version_whatsnew


def fill_code_block(content_iterator: Iterable, code_block: nodes.container) -> nodes.container:
def fill_code_block(
content_iterator: Iterator[str], code_block: nodes.container
) -> tuple[nodes.container, str | None]:
"""Fill the code block.

Parameters
Expand Down Expand Up @@ -355,8 +359,8 @@ def fill_code_block(content_iterator: Iterable, code_block: nodes.container) ->


def fill_paragraph(
content_iterator: Iterable, paragraph: nodes.paragraph, next_line: str
) -> nodes.paragraph:
content_iterator: Iterator[str], paragraph: nodes.paragraph, next_line: str | None
) -> tuple[nodes.paragraph, str | None]:
"""Fill the paragraph node.

Parameters
Expand Down Expand Up @@ -505,7 +509,11 @@ def extract_whatsnew(app: Sphinx, doctree: nodes.document, docname: str) -> None
return

whatsnew = []
app.env.whatsnew = []
# Initialize the whatsnew attribute if it doesn't exist
if not hasattr(app.env, "whatsnew"):
app.env.whatsnew = [] # type: ignore
else:
app.env.whatsnew = [] # type: ignore

# Get a list of nodes whose ids start with "version" that contain "What's new" sections
versions_nodes = []
Expand Down Expand Up @@ -557,10 +565,10 @@ def extract_whatsnew(app: Sphinx, doctree: nodes.document, docname: str) -> None

whatsnew.append(contents)

app.env.whatsnew = whatsnew
app.env.whatsnew = whatsnew # type: ignore


def whatsnew_sidebar_pages(app: Sphinx) -> list:
def whatsnew_sidebar_pages(app: Sphinx) -> list | None:
"""Get the pages the what's new section should be displayed on and return the list of pages.

Parameters
Expand Down
Loading