chore: lint source code using Ruff

This commit is contained in:
jakob.scheid committed 2026-09-21 20:10:09 +02:00
1 parent aa4a139878
commit de3eac3bb1
35 files changed
+161 -130

No files matched your search

+1
View File
@@ -13,6 +13,7 @@
# limitations under the License.
import sys
from .cli.cli import main
if __name__ == '__main__':
+1 -2
View File
@@ -12,8 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from . import cli
from . import languages
from . import cli, languages
from .cli import main
__all__ = [
+2 -1
View File
@@ -19,6 +19,7 @@ The command line interface
import argparse
import pathlib
import sys
from .languages.python import VALID_SECTIONS as VALID_PYTHON_SECTIONS
__all__ = [
@@ -169,7 +170,7 @@ def main(argv=None) -> int:
except SyntaxError:
# TODO: add a custom exception class to clearify that a project
# parsing syntax error is meant.
return error(f'syntax error in project source code')
return error('syntax error in project source code')
except KeyboardInterrupt:
return 130
except BrokenPipeError:
+5 -5
View File
@@ -13,11 +13,11 @@
# limitations under the License.
import pathlib
from ...core.python import (
PythonDefinitionDocumentationIncludeSections,
PythonDocumentationGenerator,
)
from typing import Optional
__all__ = [
'VALID_SECTIONS',
@@ -31,7 +31,7 @@ VALID_SECTIONS = list(
def parse_sections_list(
include_sections: Optional[list[str]] = None,
include_sections: list[str] | None = None,
exclude_sections: list[str] = [],
) -> PythonDefinitionDocumentationIncludeSections:
"""
@@ -41,7 +41,7 @@ def parse_sections_list(
the section name.
:param include_sections: The list of sections to be included.
:type include_sections: Optional[list[str]]
:type include_sections: list[str] | None
:param exclude_sections: The list of sections to be excluded.
:type exclude_sections: list[str]
@@ -110,7 +110,7 @@ def generate(
project_directory: pathlib.Path,
output_directory: pathlib.Path,
*,
include_sections: Optional[list[str]] = None,
include_sections: list[str] | None = None,
exclude_sections: list[str] = [],
allow_html: bool = False,
allow_tables: bool = False,
@@ -124,7 +124,7 @@ def generate(
:type output_directory: pathlib.Path
:param include_sections: The sections to be included in the
documentation.
:type include_sections: Optional[list[str]]
:type include_sections: list[str] | None
:param exclude_sections: The sections to be excluded in the
documentation.
:type exclude_sections: list[str]
+1 -1
View File
@@ -12,9 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from . import namespaces
from ._core import *
from ._core import __all__ as _core__all__
from . import namespaces
__all__ = [
*_core__all__,
+21 -20
View File
@@ -13,25 +13,26 @@
# limitations under the License.
from __future__ import annotations
from ...utils import non_empty_str, get_relative_path_segments
from ...exceptions import PythonArgumentStructureError, InvalidNamespaceError
from .namespaces import PythonModuleNamespace, PythonPackageNamespace
import ast
import pathlib
from collections.abc import Iterator
from dataclasses import dataclass
from typing import TypeAlias
import docstring_parser
from ...exceptions import InvalidNamespaceError, PythonArgumentStructureError
from ...utils import get_relative_path_segments, non_empty_str
from .arguments import PythonArgumentKind, PythonFunctionArgument
from .definitions import (
PythonAsyncFunctionDefinition,
PythonClassDefinition,
PythonDefinition,
PythonFunctionDefinition,
)
from .arguments import PythonArgumentKind, PythonFunctionArgument
from .docstrings import PythonDocstring
from collections.abc import Iterator
from typing import TypeAlias, Union
from dataclasses import dataclass
from enum import Enum
import docstring_parser
import pathlib
import ast
import types
from .namespaces import PythonModuleNamespace, PythonPackageNamespace
__all__ = [
'PythonDefinitionDocumentationIncludeSections',
@@ -63,9 +64,9 @@ def _collect_definitions(tree_or_node) -> Iterator[PythonDefinition]:
_Arguments: TypeAlias = tuple[
list[PythonFunctionArgument],
list[PythonFunctionArgument],
Union[PythonFunctionArgument, None],
PythonFunctionArgument | None,
list[PythonFunctionArgument],
Union[PythonFunctionArgument, None],
PythonFunctionArgument | None,
]
@@ -298,12 +299,12 @@ def _markdown_table(
return table.strip()
def _is_docstring(docstring: Union[str, PythonDocstring, None]) -> bool:
def _is_docstring(docstring: str | PythonDocstring | None) -> bool:
"""
Checks whether the docstring is a docstring or an empty docstring.
:param docstring: The docstring.
:type docstring: Union[str, PythonDocstring, None]
:type docstring: str | PythonDocstring | None
:return: Whether there is a docstring.
:rtype: bool
@@ -320,8 +321,8 @@ def _is_docstring(docstring: Union[str, PythonDocstring, None]) -> bool:
@dataclass
class _FunctionParamInfo:
annotation: Union[str, None]
description: Union[str, None]
annotation: str | None
description: str | None
@dataclass
@@ -908,7 +909,7 @@ class PythonModuleDocumentationGenerator:
:param src_directory: The directory of the source code. It is
optional and only used for making the module
identifiers in the documentation relative.
:type src_directory: Union[pathlib.Path, None]
:type src_directory: pathlib.Path | None
:param include_sections: The sections that will be included.
:type include_sections: PythonDefinitionDocumentationIncludeSections
:param kwargs: The options for ``PythonDefinitionDocumentationGenerator``.
@@ -917,7 +918,7 @@ class PythonModuleDocumentationGenerator:
def __init__(
self,
module_path: pathlib.Path,
src_directory: Union[pathlib.Path, None] = None,
src_directory: pathlib.Path | None = None,
include_sections: PythonDefinitionDocumentationIncludeSections = (
PythonDefinitionDocumentationIncludeSections()
),
+10 -9
View File
@@ -13,17 +13,18 @@
# limitations under the License.
from __future__ import annotations
from enum import Enum
from typing import Union
import ast
from ...utils import assert_that_is_instance
import keyword
import types
from enum import Enum
from ...exceptions import (
InvalidPythonAnnotationError,
InvalidPythonIdentifierError,
PythonArgumentStructureError,
)
import keyword
import types
from ...utils import assert_that_is_instance
__all__ = [
'PythonArgumentKind',
@@ -49,17 +50,17 @@ class PythonFunctionArgument:
:param kind: The kind of the argument.
:type kind: ArgumentKind
:param default: The default value of the argument.
:type default: Union[str, None]
:type default: str | None
:param annotation: The type annotation of the argument.
:type annotation: Union[str, None]
:type annotation: str | None
"""
def __init__(
self,
name: str,
kind: PythonArgumentKind,
default: Union[str, None],
annotation: Union[str, None],
default: str | None,
annotation: str | None,
) -> None:
assert_that_is_instance(name, str)
assert_that_is_instance(kind, PythonArgumentKind)
+18 -17
View File
@@ -13,14 +13,15 @@
# limitations under the License.
from __future__ import annotations
from typing import Union
import ast
from .arguments import PythonFunctionArgument, PythonASTArgumentsListParser
from .docstrings import PythonDocstring
import keyword
import types
from ...exceptions import InvalidPythonIdentifierError
from ...utils import assert_that_is_instance
import types
from .arguments import PythonASTArgumentsListParser, PythonFunctionArgument
from .docstrings import PythonDocstring
__all__ = [
'PythonDefinition',
@@ -30,16 +31,16 @@ __all__ = [
]
def _python_docstring(doc: Union[str, None]) -> Union[PythonDocstring, None]:
def _python_docstring(doc: str | None) -> PythonDocstring | None:
"""
Returns the PythonDocstring object if the docstring is a string and
not ``None``.
:param doc: The docstring.
:type doc: Union[str, None]
:type doc: str | None
:return: The PythonDocstring object or ``None``.
:rtype: Union[PythonDocstring, None]
:rtype: PythonDocstring | None
"""
return PythonDocstring(doc) if doc is not None else None
@@ -53,7 +54,7 @@ class PythonDefinition:
:param name: The name of the definition.
:type name: str
:param doc: The docstring.
:type doc: Union[PythonDocstring, None]
:type doc: PythonDocstring | None
:param decorators: The definition decorators.
:type decorators: list[str]
@@ -64,7 +65,7 @@ class PythonDefinition:
def __init__(
self,
name: str,
doc: Union[PythonDocstring, None],
doc: PythonDocstring | None,
decorators: list[str],
) -> None:
if keyword.iskeyword(name) or not name.isidentifier() or not name:
@@ -120,9 +121,9 @@ class PythonFunctionDefinition(PythonDefinition):
:param args: The arguments.
:type args: list[PythonFunctionArgument]
:param returns: The return type of the function or class.
:type returns: Union[str, None]
:type returns: str | None
:param doc: The docstring.
:type doc: Union[PythonDocstring, None]
:type doc: PythonDocstring | None
:param decorators: The definition decorators.
:type decorators: list[str]
@@ -134,8 +135,8 @@ class PythonFunctionDefinition(PythonDefinition):
self,
name: str,
args: list[PythonFunctionArgument],
returns: Union[str, None],
doc: Union[PythonDocstring, None],
returns: str | None,
doc: PythonDocstring | None,
decorators: list[str],
) -> None:
super().__init__(name, doc, decorators)
@@ -179,9 +180,9 @@ class PythonAsyncFunctionDefinition(PythonFunctionDefinition):
:param args: The arguments.
:type args: list[PythonFunctionArgument]
:param returns: The return type of the function or class.
:type returns: Union[str, None]
:type returns: str | None
:param doc: The docstring.
:type doc: Union[PythonDocstring, None]
:type doc: PythonDocstring | None
:param decorators: The definition decorators.
:type decorators: list[str]
@@ -199,7 +200,7 @@ class PythonClassDefinition(PythonDefinition):
:param bases: The base classes.
:type bases: list[str]
:param doc: The docstring.
:type doc: Union[PythonDocstring, None]
:type doc: PythonDocstring | None
:param decorators: The definition decorators.
:type decorators: list[str]
:param body:
@@ -213,7 +214,7 @@ class PythonClassDefinition(PythonDefinition):
self,
name: str,
bases: list[str],
doc: Union[PythonDocstring, None],
doc: PythonDocstring | None,
decorators: list[str],
body: list[PythonDefinition],
) -> None:
@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from enum import Enum
import docstring_parser
__all__ = ['PythonDocstring']
+8 -6
View File
@@ -13,13 +13,15 @@
# limitations under the License.
from __future__ import annotations
from typing import TypeAlias, Union
from ...exceptions import (
InvalidNamespaceError,
NamespaceNotFoundError,
NamespaceExistsError,
NamespaceNotFoundError,
)
from ...utils import assert_that_is_instance
from typing import Union, TypeAlias, Dict, List
__all__ = [
'PythonNamespace',
@@ -51,7 +53,7 @@ class PythonNamespace:
return f'{type(self).__name__}({self.name!r})'
PythonPackageNamespaceDictType: TypeAlias = Dict[
PythonPackageNamespaceDictType: TypeAlias = dict[
str,
Union[
str,
@@ -73,7 +75,7 @@ class PythonPackageNamespace(PythonNamespace):
def __init__(
self,
name: str,
sub_namespaces: List[PythonNamespace],
sub_namespaces: list[PythonNamespace],
) -> None:
assert_that_is_instance(sub_namespaces, list)
@@ -81,7 +83,7 @@ class PythonPackageNamespace(PythonNamespace):
self._sub_namespaces = sub_namespaces
def namespace(self, sub_namespaces: List[str]) -> PythonNamespace:
def namespace(self, sub_namespaces: list[str]) -> PythonNamespace:
"""
Returns the namespace object with a specific identifier, such as
``['package', 'subpackage', 'module']``.
@@ -205,7 +207,7 @@ class PythonPackageNamespace(PythonNamespace):
self._sub_namespaces.append(namespace)
@property
def sub_namespaces(self) -> List[PythonNamespace]:
def sub_namespaces(self) -> list[PythonNamespace]:
return self._sub_namespaces
+7 -11
View File
@@ -16,11 +16,11 @@
The utilities for jcloud_docsgen
"""
import pathlib
import os
import errno
from typing import Union
import os
import pathlib
import types
from typing import Union
try:
from warnings import deprecated
@@ -152,11 +152,7 @@ def _list_type_names(types: list[type]) -> list[str]:
def assert_that_is_instance(
obj: object,
class_or_tuple: Union[
type,
types.UnionType,
tuple[type, ...],
],
class_or_tuple: type | types.UnionType | tuple[type, ...],
) -> None:
if not isinstance(class_or_tuple, Union[type, types.UnionType, tuple]):
raise TypeError(
@@ -185,7 +181,7 @@ def assert_that_is_instance(
)
def non_empty_str(value: Union[str, None]) -> Union[str, None]:
def non_empty_str(value: str | None) -> str | None:
"""
Returns the value or ``None``.
@@ -195,10 +191,10 @@ def non_empty_str(value: Union[str, None]) -> Union[str, None]:
of spaces, it is also considered empty.
:param value: The value.
:type value: Union[str, None]
:type value: str | None
:return: The value or ``None``.
:rtype: Union[str, None]
:rtype: str | None
"""
if value is None:
+1
View File
@@ -13,6 +13,7 @@
# limitations under the License.
import pytest
from jcloud_docsgen.cli.cli import create_argument_parser
+4 -1
View File
@@ -13,8 +13,11 @@
# limitations under the License.
import pathlib
import pytest
from jcloud_docsgen.cli.cli import create_argument_parser, main as cli_main
from jcloud_docsgen.cli.cli import create_argument_parser
from jcloud_docsgen.cli.cli import main as cli_main
from jcloud_docsgen.cli.languages.python import parse_sections_list
from jcloud_docsgen.core.python import (
PythonDefinitionDocumentationIncludeSections,
@@ -12,21 +12,22 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from src.jcloud_docsgen.core.python._core import (
PythonDefinitionDocumentationGenerator,
PythonDefinitionDocumentationIncludeSections,
)
from src.jcloud_docsgen.core.python.arguments import (
PythonArgumentKind,
PythonFunctionArgument,
)
from src.jcloud_docsgen.core.python.definitions import (
PythonFunctionDefinition,
PythonAsyncFunctionDefinition,
PythonClassDefinition,
)
from src.jcloud_docsgen.core.python.arguments import (
PythonFunctionArgument,
PythonArgumentKind,
PythonFunctionDefinition,
)
from src.jcloud_docsgen.core.python.docstrings import PythonDocstring
import pytest
def _signature_section(signature: str) -> str:
@@ -12,13 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pathlib
import pytest
from src.jcloud_docsgen.core.python import PythonDocumentationGenerator
from src.jcloud_docsgen.core.python.namespaces import (
PythonModuleNamespace,
PythonPackageNamespace,
)
import pytest
import pathlib
@pytest.mark.parametrize(
@@ -12,10 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pathlib
import pytest
from src.jcloud_docsgen.core.python import PythonModuleDocumentationGenerator
from src.jcloud_docsgen.exceptions import InvalidNamespaceError
import pathlib
import pytest
@pytest.mark.parametrize(
@@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from src.jcloud_docsgen.core.python._core import _argument_kind_human_readable
from src.jcloud_docsgen.core.python.arguments import PythonArgumentKind
import pytest
@pytest.mark.parametrize(
@@ -12,18 +12,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import ast
import pytest
from src.jcloud_docsgen.core.python._core import _collect_definitions
from src.jcloud_docsgen.core.python.arguments import (
PythonArgumentKind,
PythonFunctionArgument,
)
from src.jcloud_docsgen.core.python.definitions import (
PythonClassDefinition,
PythonFunctionDefinition,
)
from src.jcloud_docsgen.core.python.arguments import (
PythonFunctionArgument,
PythonArgumentKind,
)
from src.jcloud_docsgen.core.python.docstrings import PythonDocstring
import pytest
import ast
def module(source_code: str) -> ast.Module:
@@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.jcloud_docsgen.core.python._core import _fit_row_length
import pytest
from src.jcloud_docsgen.core.python._core import _fit_row_length
@pytest.mark.parametrize(
'expected_length,row,expected',
@@ -12,16 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import docstring_parser
import pytest
from src.jcloud_docsgen.core.python._core import (
_function_params_info,
_FunctionParamInfo,
)
from src.jcloud_docsgen.core.python.arguments import (
PythonFunctionArgument,
PythonArgumentKind,
PythonFunctionArgument,
)
import pytest
import docstring_parser
def docstring_args_list(
@@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.jcloud_docsgen.core.python._core import _markdown_table
import pytest
from src.jcloud_docsgen.core.python._core import _markdown_table
@pytest.mark.parametrize(
(
@@ -12,12 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from src.jcloud_docsgen.core.python._core import _signature_argument_list
from src.jcloud_docsgen.core.python.arguments import (
PythonFunctionArgument,
PythonArgumentKind,
PythonFunctionArgument,
)
import pytest
@pytest.mark.parametrize(
@@ -12,13 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import ast
import pytest
from src.jcloud_docsgen.core.python.arguments import (
PythonArgumentKind,
PythonASTArgumentsListParser,
PythonFunctionArgument,
PythonArgumentKind,
)
import pytest
import ast
def argument_list(argument_list: str) -> ast.arguments:
@@ -12,16 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from src.jcloud_docsgen.core.python.arguments import (
PythonFunctionArgument,
PythonArgumentKind,
PythonFunctionArgument,
)
from src.jcloud_docsgen.exceptions import (
InvalidPythonIdentifierError,
InvalidPythonAnnotationError,
InvalidPythonIdentifierError,
PythonArgumentStructureError,
)
import pytest
@pytest.mark.parametrize(
@@ -12,18 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.jcloud_docsgen.core.python.definitions import (
PythonFunctionDefinition,
PythonClassDefinition,
)
import pytest
from src.jcloud_docsgen.core.python.arguments import (
PythonFunctionArgument,
PythonArgumentKind,
PythonFunctionArgument,
)
from src.jcloud_docsgen.core.python.definitions import (
PythonClassDefinition,
PythonFunctionDefinition,
)
from src.jcloud_docsgen.core.python.docstrings import PythonDocstring
from src.jcloud_docsgen.exceptions import InvalidPythonIdentifierError
from tests.utils.ast_node import ast_node
import pytest
@pytest.mark.parametrize(
@@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pytest
from src.jcloud_docsgen.core.python.definitions import PythonDefinition
from src.jcloud_docsgen.core.python.docstrings import PythonDocstring
from src.jcloud_docsgen.exceptions import InvalidPythonIdentifierError
import pytest
@pytest.mark.parametrize(
@@ -12,15 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.jcloud_docsgen.core.python.definitions import PythonFunctionDefinition
import pytest
from src.jcloud_docsgen.core.python.arguments import (
PythonFunctionArgument,
PythonArgumentKind,
PythonFunctionArgument,
)
from src.jcloud_docsgen.core.python.definitions import PythonFunctionDefinition
from src.jcloud_docsgen.core.python.docstrings import PythonDocstring
from src.jcloud_docsgen.exceptions import InvalidPythonIdentifierError
from tests.utils.ast_node import ast_node
import pytest
@pytest.mark.parametrize(
@@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.jcloud_docsgen.core.python.namespaces import PythonModuleNamespace
import pytest
from src.jcloud_docsgen.core.python.namespaces import PythonModuleNamespace
class StrSubclass(str): ...
@@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.jcloud_docsgen.core.python.namespaces import PythonNamespace
import pytest
from src.jcloud_docsgen.core.python.namespaces import PythonNamespace
class StrSubclass(str): ...
@@ -12,14 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.jcloud_docsgen.core.python.namespaces import (
PythonPackageNamespace,
PythonModuleNamespace,
)
import pytest
from src.jcloud_docsgen.core.python.namespaces import (
PythonModuleNamespace,
PythonPackageNamespace,
)
from src.jcloud_docsgen.exceptions import (
NamespaceNotFoundError,
InvalidNamespaceError,
NamespaceNotFoundError,
)
@@ -12,10 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.jcloud_docsgen.utils import assert_that_is_instance
import pytest
import types
import pytest
from src.jcloud_docsgen.utils import assert_that_is_instance
class TestType: ...
@@ -12,10 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.jcloud_docsgen.utils import get_relative_path_segments
import pytest
import pathlib
import pytest
from src.jcloud_docsgen.utils import get_relative_path_segments
@pytest.mark.parametrize(
'path,root,expected',
+2 -1
View File
@@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.jcloud_docsgen.utils import human_readable_list
import pytest
from src.jcloud_docsgen.utils import human_readable_list
@pytest.mark.parametrize(
'ls,final_separator,quotation_mark,expected',
+2 -1
View File
@@ -12,9 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from src.jcloud_docsgen.utils import non_empty_str
import pytest
from src.jcloud_docsgen.utils import non_empty_str
@pytest.mark.parametrize(
'value,expected',
+2 -3
View File
@@ -12,13 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Union
import ast
__all__ = ['ast_node']
def ast_node(statement: str) -> Union[ast.stmt, None]:
def ast_node(statement: str) -> ast.stmt | None:
"""
Returns an AST node from the statement.
@@ -26,7 +25,7 @@ def ast_node(statement: str) -> Union[ast.stmt, None]:
:type statement: str
:return: The node.
:rtype: Union[ast.stmt, None]
:rtype: ast.stmt | None
"""
body = ast.parse(statement).body