Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
style: fix issues found by ruff (#81)
Browse files Browse the repository at this point in the history
### Summary of Changes

* Remove configuration of `pylint` and `flake8`
* Add configuration of `ruff`
* Fix issues found by `ruff`

---------

Co-authored-by: megalinter-bot <[email protected]>
  • Loading branch information
lars-reimann and megalinter-bot authored Apr 2, 2023
1 parent ce6aa35 commit 9d61efa
Show file tree
Hide file tree
Showing 138 changed files with 3,129 additions and 3,734 deletions.
8 changes: 0 additions & 8 deletions .github/linters/.flake8

This file was deleted.

42 changes: 0 additions & 42 deletions .github/linters/.pylintrc

This file was deleted.

105 changes: 105 additions & 0 deletions .github/linters/.ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
ignore-init-module-imports = true
line-length = 120
target-version = "py310"

select = [
"F",
"E",
"W",
"I",
"N",
"D",
"UP",
"YTT",
"BLE",
"FBT",
"B",
"A",
"COM",
"C4",
"DTZ",
"T10",
"ISC",
"ICN",
"G",
"INP",
"PIE",
"T20",
"PYI",
"PT",
"Q",
"RSE",
"RET",
"SLF",
"SIM",
"TID",
"TCH",
"INT",
"ARG",
"PTH",
"ERA",
"PGH",
"PL",
"TRY",
"RUF"
]
ignore = [
# line-too-long (handled by black)
"E501",
# tab-indentation (handled by black)
"W191",
# trailing-whitespace (handled by black)
"W291",
# missing-newline-at-end-of-file (handled by black)
"W292",
# blank-line-with-witespace (handled by black)
"W293",
# boolean-positional-arg-in-function-definition (we leave it to the call-site)
"FBT001",
# boolean-default-value-in-function-definition (we leave it to the call-site)
"FBT002",
# builtin-attribute-shadowing (not an issue)
"A003",
# superfluous-else-return (sometimes it's more readable)
"RET505",
# superfluous-else-raise (sometimes it's more readable)
"RET506",
# superfluous-else-continue (sometimes it's more readable)
"RET507",
# superfluous-else-break (sometimes it's more readable)
"RET508",
# private-member-access (we cannot always avoid it if we want a clean API)
"SLF001",
# if-else-block-instead-of-if-exp (an if-else block can be more readable)
"SIM108",
# compare-to-empty-string (sometimes it's better to be explicit)
"PLC1901",
# too-many-return-statements
"PLR0911",
# too-many-branches
"PLR0912",
# too-many-arguments
"PLR0913",
# too-many-statements
"PLR0915",
# magic-value-comparison
"PLR2004",
# raise-vanilla-args
"TRY003",
]

[per-file-ignores]
"*test*.py" = [
# Undocumented declarations
"D100",
"D101",
"D102",
"D103",
"D104",
"D105",
"D106",
"D107",
]

[pydocstyle]
convention = "numpy"
6 changes: 6 additions & 0 deletions .github/linters/.stylelintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "stylelint-config-standard",
"rules": {
"selector-class-pattern": null
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ htmlcov/
.coverage
coverage.xml

# ruff
.ruff_cache/

# mkdocs
/site/
/docs/reference/safeds/
Expand Down
1 change: 1 addition & 0 deletions docs/reference/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Utilities for creating an API reference."""
13 changes: 7 additions & 6 deletions docs/reference/generate_reference_pages.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Inspired by https://mkdocstrings.github.io/recipes/#bind-pages-to-sections-themselves
"""
Automatically generate reference pages for all public classes and functions in the package.
Inspired by https://mkdocstrings.github.io/recipes/#bind-pages-to-sections-themselves.
"""

import sys
from importlib import import_module
Expand All @@ -12,10 +16,7 @@


def list_class_and_function_names_in_module(module_name: str) -> list[str]:
"""
Returns a list with the names of all classes and function names in the given module.
"""

"""Return a list with the names of all classes and function names in the given module."""
import_module(module_name)
module = sys.modules[module_name]

Expand Down Expand Up @@ -47,7 +48,7 @@ def list_class_and_function_names_in_module(module_name: str) -> list[str]:
doc_path = doc_path.with_name(f"{name}.md")
full_doc_path = full_doc_path.with_name(f"{name}.md")

nav[parts + (name,)] = doc_path.as_posix()
nav[(*parts, name)] = doc_path.as_posix()

# Create one file containing the documentation for the current class or function
with mkdocs_gen_files.open(full_doc_path, "w") as fd:
Expand Down
1 change: 1 addition & 0 deletions src/library_analyzer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Analysis of Python libraries and of code that uses them."""
4 changes: 0 additions & 4 deletions src/library_analyzer/__main__.py

This file was deleted.

4 changes: 4 additions & 0 deletions src/library_analyzer/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
"""The command line interface for the `library-analyzer`."""

from ._cli import cli

__all__ = ["cli"]
42 changes: 12 additions & 30 deletions src/library_analyzer/cli/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def cli() -> None:
if args.command == _API_COMMAND:
_run_api_command(args.package, args.src, args.out)
elif args.command == _USAGES_COMMAND:
_run_usages_command(
args.package, args.client, args.out, args.processes, args.batchsize
)
_run_usages_command(args.package, args.client, args.out, args.processes, args.batchsize)
elif args.command == _ANNOTATIONS_COMMAND:
_run_annotations(args.api, args.usages, args.out)
elif args.command == _ALL_COMMAND:
Expand All @@ -46,9 +44,7 @@ def cli() -> None:

def _get_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Analyze Python code.")
parser.add_argument(
"-v", "--verbose", help="show info messages", action="store_true"
)
parser.add_argument("-v", "--verbose", help="show info messages", action="store_true")

# Commands
subparsers = parser.add_subparsers(dest="command")
Expand Down Expand Up @@ -79,15 +75,11 @@ def _add_api_subparser(subparsers: _SubParsersAction) -> None:
required=False,
default=None,
)
api_parser.add_argument(
"-o", "--out", help="Output directory.", type=Path, required=True
)
api_parser.add_argument("-o", "--out", help="Output directory.", type=Path, required=True)


def _add_usages_subparser(subparsers: _SubParsersAction) -> None:
usages_parser = subparsers.add_parser(
_USAGES_COMMAND, help="Find usages of API elements."
)
usages_parser = subparsers.add_parser(_USAGES_COMMAND, help="Find usages of API elements.")
usages_parser.add_argument(
"-p",
"--package",
Expand All @@ -108,23 +100,19 @@ def _add_usages_subparser(subparsers: _SubParsersAction) -> None:
type=int,
required=False,
default=4,
),
)
usages_parser.add_argument(
"--batchsize",
help="How many files to process in one go. Higher values lead to higher memory usage but better performance.",
type=int,
required=False,
default=100,
)
usages_parser.add_argument(
"-o", "--out", help="Output directory.", type=Path, required=True
)
usages_parser.add_argument("-o", "--out", help="Output directory.", type=Path, required=True)


def _add_annotations_subparser(subparsers) -> None:
generate_parser = subparsers.add_parser(
_ANNOTATIONS_COMMAND, help="Generate Annotations automatically."
)
def _add_annotations_subparser(subparsers: _SubParsersAction) -> None:
generate_parser = subparsers.add_parser(_ANNOTATIONS_COMMAND, help="Generate Annotations automatically.")
generate_parser.add_argument(
"-a",
"--api",
Expand All @@ -139,9 +127,7 @@ def _add_annotations_subparser(subparsers) -> None:
type=Path,
required=True,
)
generate_parser.add_argument(
"-o", "--out", help="Output directory.", type=Path, required=True
)
generate_parser.add_argument("-o", "--out", help="Output directory.", type=Path, required=True)


def _add_all_subparser(subparsers: _SubParsersAction) -> None:
Expand Down Expand Up @@ -172,9 +158,7 @@ def _add_all_subparser(subparsers: _SubParsersAction) -> None:
type=Path,
required=True,
)
all_parser.add_argument(
"-o", "--out", help="Output directory.", type=Path, required=True
)
all_parser.add_argument("-o", "--out", help="Output directory.", type=Path, required=True)
all_parser.add_argument(
"--processes",
help="How many processes should be spawned during processing.",
Expand All @@ -191,7 +175,7 @@ def _add_all_subparser(subparsers: _SubParsersAction) -> None:
)


def _add_migrate_subparser(subparsers) -> None:
def _add_migrate_subparser(subparsers: _SubParsersAction) -> None:
generate_parser = subparsers.add_parser(
_MIGRATE_COMMAND,
help="Migrate Annotations for the new version based on the previous version.",
Expand All @@ -217,6 +201,4 @@ def _add_migrate_subparser(subparsers) -> None:
type=Path,
required=True,
)
generate_parser.add_argument(
"-o", "--out", help="Output directory.", type=Path, required=True
)
generate_parser.add_argument("-o", "--out", help="Output directory.", type=Path, required=True)
13 changes: 6 additions & 7 deletions src/library_analyzer/cli/_read_and_write_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,33 @@
from library_analyzer.cli._json_encoder import CustomEncoder
from library_analyzer.processing.annotations.model import AnnotationStore
from library_analyzer.processing.api.model import API
from library_analyzer.processing.dependencies._parameter_dependencies import APIDependencies
from library_analyzer.processing.usages.model import UsageCountStore
from library_analyzer.utils import ensure_file_exists


def _read_annotations_file(annotations_file_path: Path) -> AnnotationStore:
with open(annotations_file_path, encoding="utf-8") as annotations_file:
with annotations_file_path.open(encoding="utf-8") as annotations_file:
annotations_json = json.load(annotations_file)

return AnnotationStore.from_json(annotations_json)


def _write_annotations_file(
annotations: AnnotationStore, annotations_file_path: Path
) -> None:
def _write_annotations_file(annotations: AnnotationStore, annotations_file_path: Path) -> None:
ensure_file_exists(annotations_file_path)
with annotations_file_path.open("w", encoding="utf-8") as f:
json.dump(annotations.to_json(), f, indent=2)


def _read_api_file(api_file_path: Path) -> API:
with open(api_file_path, encoding="utf-8") as api_file:
with api_file_path.open(encoding="utf-8") as api_file:
api_json = json.load(api_file)

return API.from_json(api_json)


def _read_usages_file(usages_file_path: Path) -> UsageCountStore:
with open(usages_file_path, encoding="utf-8") as usages_file:
with usages_file_path.open(encoding="utf-8") as usages_file:
usages_json = json.load(usages_file)

return UsageCountStore.from_json(usages_json)
Expand All @@ -45,7 +44,7 @@ def _write_api_file(api: API, out_dir_path: Path) -> Path:
return out_file_api


def _write_api_dependency_file(api: API, api_dependencies, out):
def _write_api_dependency_file(api: API, api_dependencies: APIDependencies, out: Path) -> None:
out_file_api_dependencies = out.joinpath(f"{api.package}__api_dependencies.json")
ensure_file_exists(out_file_api_dependencies)
with out_file_api_dependencies.open("w") as f:
Expand Down
Loading

0 comments on commit 9d61efa

Please sign in to comment.