Skip to content

Commit

Permalink
Add Ruff, fix Checks
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Dec 2, 2024
1 parent 5441487 commit 7c32799
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 40 deletions.
23 changes: 6 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,35 +62,24 @@ repos:
rev: v0.1.8
hooks:
- id: ripsecrets
- repo: https://github.com/asottile/pyupgrade
rev: v3.19.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.1
hooks:
- id: pyupgrade
- id: ruff-format
args:
- --py39-plus
- repo: https://github.com/PyCQA/autoflake
rev: v2.3.1
hooks:
- id: autoflake
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 24.10.0
hooks:
- id: black
- --line-length=110
- repo: https://github.com/PyCQA/prospector
rev: v1.13.3
hooks:
- id: prospector
args:
- --tool=pydocstyle
- --tool=ruff
- --die-on-tool-error
- --output-format=pylint
additional_dependencies:
- prospector-profile-duplicated==1.8.0 # pypi
- prospector-profile-utils==1.12.2 # pypi
- ruff==0.8.1 # pypi
- repo: https://github.com/sbrunner/jsonschema-validator
rev: 0.3.2
hooks:
Expand Down
8 changes: 8 additions & 0 deletions .prospector.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
inherits:
- duplicated
- utils:base
- utils:fix
- utils:no-design-checks
- utils:unsafe

pycodestyle:
disable:
- E704 # multiple statements on one line (def) (not compatible with protocol)

ruff:
disable:
- D101 # Missing docstring in public class
- D102 # Missing docstring in public method
- D103 # Missing docstring in public function
38 changes: 18 additions & 20 deletions c2c/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import sys
import traceback
from argparse import ArgumentParser, Namespace
from ast import literal_eval
from string import Formatter
from subprocess import CalledProcessError # nosec
from typing import Any, Callable, Optional, Protocol, Union, cast
Expand Down Expand Up @@ -80,7 +81,7 @@ def transform_path(
"The key '%s' in '%s' is not present in: [%s]",
key,
current_path,
", ".join([f"'{k}'" for k in value.keys()]),
", ".join([f"'{k}'" for k in value]),
)
else:
if len(path) == 1:
Expand Down Expand Up @@ -190,10 +191,7 @@ def __init__(
self.all_environment_dict[env["name"]] = os.environ[env["name"]]

def path_in(self, path_list: list[str], list_: list[str]) -> bool:
for path in path_list:
if path in list_:
return True
return False
return any(path in list_ for path in path_list)

def format_walker(
self, current_vars: dict[str, Any], path: Optional[str] = None, path_list: Optional[list[str]] = None
Expand Down Expand Up @@ -231,7 +229,7 @@ def format_walker(

elif isinstance(current_vars, dict):
skip = []
for key in current_vars.keys():
for key in current_vars:
if path is None:
current_path = key
current_path_list = [key]
Expand Down Expand Up @@ -392,12 +390,16 @@ def set_path(item: tuple[dict[str, Any], str], value: str) -> None:

def _proceed(files: list[tuple[str, str]], used_vars: dict[str, Any], options: Namespace) -> None:
if options.engine == "jinja":
from bottle import jinja2_template as engine # type: ignore # pylint: disable=import-outside-toplevel
from bottle import (
jinja2_template as engine, # type: ignore # pylint: disable=import-outside-toplevel
)

bottle_template(files, used_vars, engine)

elif options.engine == "mako":
from bottle import mako_template as engine # pylint: disable=import-outside-toplevel
from bottle import (
mako_template as engine, # pylint: disable=import-outside-toplevel
)

bottle_template(files, used_vars, engine)

Expand Down Expand Up @@ -586,11 +588,9 @@ def __init__(self, interpreter: dict[str, Any]):

def __call__(self, expression: str, current_path: str) -> Value: # type: ignore
try:
return cast(Value, eval(expression, globs)) # nosec # pylint: disable=eval-used
return cast(Value, literal_eval(expression, globs)) # nosec # pylint: disable=eval-used
except Exception: # pragma: nocover # pylint: disable=broad-except
error = "When evaluating {} expression '{}' in '{}' as Python:\n{}".format(
var_name, expression, current_path, traceback.format_exc()
)
error = f"When evaluating {var_name} expression '{expression}' in '{current_path}' as Python:\n{traceback.format_exc()}"
LOG.error(error)
if interpreter.get("ignore_error", False):
return "ERROR: " + error
Expand Down Expand Up @@ -622,8 +622,8 @@ def __call__(self, value: str, current_path: str) -> Value:
try:
return cast(dict[str, Any], json.loads(value))
except ValueError as exception: # pragma: nocover
error = "When evaluating {} expression '{}' in '{}' as JSON: {}".format(
key, value, current_path, exception
error = (
f"When evaluating {key} expression '{value}' in '{current_path}' as JSON: {exception}"
)
LOG.error(error)
if interpreter.get("ignore_error", False):
Expand All @@ -639,8 +639,8 @@ def __call__(self, value: str, current_path: str) -> Value:
try:
return cast(dict[str, Any], yaml.safe_load(value))
except ParserError as exception: # pragma: nocover
error = "When evaluating {} expression '{}' in '{}' as YAML: {}".format(
key, value, current_path, exception
error = (
f"When evaluating {key} expression '{value}' in '{current_path}' as YAML: {exception}"
)
LOG.error(error)
if self.interpreter.get("ignore_error", False):
Expand Down Expand Up @@ -678,11 +678,9 @@ def __call__(self, value: str, current_path: str) -> Value: # type: ignore
expression = self.postprocess["expression"] # [:] to clone
expression = expression.format(repr(value))
try:
return cast(Value, eval(expression, globs)) # nosec # pylint: disable=eval-used
return cast(Value, literal_eval(expression, globs)) # nosec # pylint: disable=eval-used
except ValueError as exception: # pragma: nocover
error = "When interpreting the expression '{}' in '{}': {}".format(
expression, current_path, exception
)
error = f"When interpreting the expression '{expression}' in '{current_path}': {exception}"
LOG.error(error)
if ignore_error:
return "ERROR: " + error
Expand Down
32 changes: 30 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ PyYAML = "6.0.2"
pyyaml-include = "2.2"

[tool.poetry.group.dev.dependencies]
prospector = { version = "1.13.3", extras = ["with_bandit", "with_mypy", "with_pyroma"] }
prospector = { version = "1.13.3", extras = ["with_bandit", "with_mypy", "with_pyroma", "with_ruff"] }
prospector-profile-duplicated = "1.8.0"
prospector-profile-utils = "1.13.0"
pytest = "8.3.4"
Expand Down

0 comments on commit 7c32799

Please sign in to comment.