Skip to content

Commit

Permalink
style: Format
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Dec 6, 2023
1 parent 4ffb6c9 commit c8aacd7
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 35 deletions.
1 change: 0 additions & 1 deletion scripts/gen_credits.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from jinja2 import StrictUndefined
from jinja2.sandbox import SandboxedEnvironment


# TODO: Remove once support for Python 3.10 is dropped.
if sys.version_info >= (3, 11):
import tomllib
Expand Down
2 changes: 1 addition & 1 deletion src/mkdocstrings_handlers/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

from mkdocstrings_handlers.python.handler import get_handler

__all__ = ["get_handler"] # noqa: WPS410
__all__ = ["get_handler"]
53 changes: 29 additions & 24 deletions src/mkdocstrings_handlers/python/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@
import sys
import traceback
from collections import ChainMap
from subprocess import PIPE, Popen # noqa: S404
from typing import Any, BinaryIO, Iterator, List, Optional, Sequence, Tuple
from subprocess import PIPE, Popen
from typing import TYPE_CHECKING, Any, BinaryIO, Iterator

from markdown import Markdown

from mkdocstrings.extension import PluginError
from mkdocstrings.handlers.base import BaseHandler, CollectionError, CollectorItem
from mkdocstrings.inventory import Inventory
from mkdocstrings.loggers import get_logger
from mkdocstrings_handlers.python.rendering import (
do_brief_xref,
rebuild_category_lists,
Expand All @@ -28,6 +22,14 @@
sort_object,
)

from mkdocstrings.extension import PluginError
from mkdocstrings.handlers.base import BaseHandler, CollectionError, CollectorItem
from mkdocstrings.inventory import Inventory
from mkdocstrings.loggers import get_logger

if TYPE_CHECKING:
from markdown import Markdown

# TODO: add a deprecation warning once the new handler handles 95% of use-cases

logger = get_logger(__name__)
Expand Down Expand Up @@ -116,12 +118,12 @@ class PythonHandler(BaseHandler):
- `show_bases` (`bool`): Show the base classes of a class. Default: `True`.
- `show_source` (`bool`): Show the source code of this object. Default: `True`.
""" # noqa: E501
"""

def __init__( # noqa: WPS231
def __init__(
self,
*args,
setup_commands: Optional[List[str]] = None,
setup_commands: list[str] | None = None,
config_file_path: str | None = None,
paths: list[str] | None = None,
**kwargs,
Expand Down Expand Up @@ -150,17 +152,16 @@ def __init__( # noqa: WPS231
paths.append(os.path.dirname(config_file_path))
search_paths = []
for path in paths:
if not os.path.isabs(path):
if config_file_path:
path = os.path.abspath(os.path.join(os.path.dirname(config_file_path), path))
if not os.path.isabs(path) and config_file_path:
path = os.path.abspath(os.path.join(os.path.dirname(config_file_path), path))
if path not in search_paths:
search_paths.append(path)
self._paths = search_paths

commands = []

if search_paths:
commands.extend([f"sys.path.insert(0, {path!r})" for path in reversed(search_paths)]) # noqa: WPS441
commands.extend([f"sys.path.insert(0, {path!r})" for path in reversed(search_paths)])

if setup_commands:
# prevent the Python interpreter or the setup commands
Expand All @@ -172,7 +173,7 @@ def __init__( # noqa: WPS231
*setup_commands,
"sys.stdout.flush()",
"sys.stdout = sys.__stdout__", # restore stdout
]
],
)

if commands:
Expand All @@ -186,7 +187,7 @@ def __init__( # noqa: WPS231
else:
cmd = [sys.executable, "-m", "pytkdocs", "--line-by-line"]

self.process = Popen( # noqa: S603,S607 (we trust the input, and we don't want to use the absolute path)
self.process = Popen(
cmd,
universal_newlines=True,
stdout=PIPE,
Expand All @@ -198,8 +199,12 @@ def __init__( # noqa: WPS231

@classmethod
def load_inventory(
cls, in_file: BinaryIO, url: str, base_url: Optional[str] = None, **kwargs
) -> Iterator[Tuple[str, str]]:
cls,
in_file: BinaryIO,
url: str,
base_url: str | None = None,
**kwargs,
) -> Iterator[tuple[str, str]]:
"""Yield items and their URLs from an inventory file streamed from `in_file`.
This implements mkdocstrings' `load_inventory` "protocol" (see plugin.py).
Expand All @@ -216,10 +221,10 @@ def load_inventory(
if base_url is None:
base_url = posixpath.dirname(url)

for item in Inventory.parse_sphinx(in_file, domain_filter=("py",)).values(): # noqa: WPS526
for item in Inventory.parse_sphinx(in_file, domain_filter=("py",)).values():
yield item.name, posixpath.join(base_url, item.uri)

def collect(self, identifier: str, config: dict) -> CollectorItem: # noqa: WPS231
def collect(self, identifier: str, config: dict) -> CollectorItem:
"""Collect the documentation tree given an identifier and selection options.
In this method, we feed one line of JSON to the standard input of the subprocess that was opened
Expand Down Expand Up @@ -338,9 +343,9 @@ def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore


def get_handler(
theme: str, # noqa: W0613 (unused argument config)
custom_templates: Optional[str] = None,
setup_commands: Optional[List[str]] = None,
theme: str,
custom_templates: str | None = None,
setup_commands: list[str] | None = None,
config_file_path: str | None = None,
paths: list[str] | None = None,
**config: Any,
Expand Down
6 changes: 4 additions & 2 deletions src/mkdocstrings_handlers/python/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
from __future__ import annotations

import sys
from typing import Any, Callable
from typing import TYPE_CHECKING, Any, Callable

from markupsafe import Markup

from mkdocstrings.handlers.base import CollectorItem
from mkdocstrings.loggers import get_logger

if TYPE_CHECKING:
from mkdocstrings.handlers.base import CollectorItem

log = get_logger(__name__)


Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def fixture_mkdocs_conf(request, tmp_path):
MkDocs configuration object.
"""
conf = MkDocsConfig()
while hasattr(request, "_parent_request") and hasattr(request._parent_request, "_parent_request"): # noqa: WPS437
request = request._parent_request # noqa: WPS437
while hasattr(request, "_parent_request") and hasattr(request._parent_request, "_parent_request"):
request = request._parent_request

conf_dict = {
"site_name": "foo",
Expand Down
3 changes: 1 addition & 2 deletions tests/test_collector.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# -*- coding: utf-8 -*-
"""Tests for the `collector` module."""
from unittest import mock

import pytest
from mkdocstrings_handlers.python import get_handler

from mkdocstrings.handlers.base import CollectionError
from mkdocstrings_handlers.python import get_handler


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from copy import deepcopy

from mkdocstrings_handlers.python.rendering import ( # noqa: WPS450
from mkdocstrings_handlers.python.rendering import (
rebuild_category_lists,
sort_key_alphabetical,
sort_key_source,
Expand Down
3 changes: 1 addition & 2 deletions tests/test_themes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Tests for the different themes we claim to support."""

import sys

import pytest

Expand Down Expand Up @@ -34,6 +33,6 @@ def test_render_themes_templates(module, plugin):
plugin: The plugin instance (parametrized fixture).
"""
handler = plugin.handlers.get_handler("python")
handler._update_env(plugin.md, plugin.handlers._config) # noqa: WPS437
handler._update_env(plugin.md, plugin.handlers._config)
data = handler.collect(module, {})
handler.render(data, {})

0 comments on commit c8aacd7

Please sign in to comment.