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

refactor: Small fixes to type annotations #470

Merged
merged 1 commit into from Aug 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/mkdocstrings/extension.py
Expand Up @@ -127,17 +127,18 @@ def run(self, parent: Element, blocks: MutableSequence[str]) -> None:
el.extend(headings)

page = self._autorefs.current_page
for heading in headings:
anchor = heading.attrib["id"] # noqa: WPS440
self._autorefs.register_anchor(page, anchor) # noqa: WPS441

if "data-role" in heading.attrib:
self._handlers.inventory.register(
name=anchor, # noqa: WPS441
domain=handler.domain,
role=heading.attrib["data-role"],
uri=f"{page}#{anchor}", # noqa: WPS441
)
if page:
Copy link
Member

@pawamoy pawamoy Jul 17, 2023

Choose a reason for hiding this comment

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

@oprypin this condition prevents using cross-refs when rendering API docs in the top-level index page. Can you remember why you added it? I guess it was just for type correctness?

I tested without it, and the utilities computing relative URLs work fine when page == "" too.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I think I'm referring to when page is "". But if it is None it won't work indeed. So maybe we should simply differentiate the two cases with if page is not None:.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah my bad, it definitely should be if page is not None. It was to satisfy type checking.

for heading in headings:
anchor = heading.attrib["id"] # noqa: WPS440
self._autorefs.register_anchor(page, anchor) # noqa: WPS441

if "data-role" in heading.attrib:
self._handlers.inventory.register(
name=anchor, # noqa: WPS441
domain=handler.domain,
role=heading.attrib["data-role"],
uri=f"{page}#{anchor}", # noqa: WPS441
)

parent.append(el)

Expand Down
2 changes: 1 addition & 1 deletion src/mkdocstrings/loggers.py
Expand Up @@ -11,7 +11,7 @@
try:
from jinja2 import pass_context
except ImportError: # TODO: remove once Jinja2 < 3.1 is dropped
from jinja2 import contextfunction as pass_context # noqa: WPS440
from jinja2 import contextfunction as pass_context # type: ignore # noqa: WPS440

try:
import mkdocstrings_handlers
Expand Down
8 changes: 6 additions & 2 deletions src/mkdocstrings/plugin.py
Expand Up @@ -111,7 +111,9 @@ def handlers(self) -> Handlers:
return self._handlers

# TODO: remove once watch feature is removed
def on_serve(self, server: LiveReloadServer, builder: Callable, **kwargs: Any): # noqa: W0613 (unused arguments)
def on_serve(
self, server: LiveReloadServer, config: Config, builder: Callable, *args: Any, **kwargs: Any
) -> None: # noqa: W0613 (unused arguments)
"""Watch directories.

Hook for the [`on_serve` event](https://www.mkdocs.org/user-guide/plugins/#on_serve).
Expand All @@ -121,7 +123,9 @@ def on_serve(self, server: LiveReloadServer, builder: Callable, **kwargs: Any):

Arguments:
server: The `livereload` server instance.
config: The MkDocs config object (unused).
builder: The function to build the site.
*args: Additional arguments passed by MkDocs.
**kwargs: Additional arguments passed by MkDocs.
"""
if self.config["watch"]:
Expand Down Expand Up @@ -216,7 +220,7 @@ def inventory_enabled(self) -> bool:
inventory_enabled = any(handler.enable_inventory for handler in self.handlers.seen_handlers)
return inventory_enabled

def on_env(self, env, config: Config, **kwargs):
def on_env(self, env, config: Config, *args, **kwargs) -> None:
"""Extra actions that need to happen after all Markdown rendering and before HTML rendering.

Hook for the [`on_env` event](https://www.mkdocs.org/user-guide/plugins/#on_env).
Expand Down