enable pep8-naming and use ruff for autofixer

This commit is contained in:
burnettk 2023-05-30 07:15:49 -04:00
parent cb995b53a6
commit a567b990e6
5 changed files with 18 additions and 28 deletions

View File

@ -30,29 +30,17 @@ function get_python_dirs() {
(git ls-tree -r HEAD --name-only | grep -E '\.py$' | awk -F '/' '{print $1}' | sort | uniq | grep -v '\.' | grep -Ev '^(bin|migrations)$') || echo '' (git ls-tree -r HEAD --name-only | grep -E '\.py$' | awk -F '/' '{print $1}' | sort | uniq | grep -v '\.' | grep -Ev '^(bin|migrations)$') || echo ''
} }
function run_autoflake() { function run_autofixers() {
# checking command -v autoflake8 is not good enough, since the asdf shim may be installed, which will make command -v succeed, # checking command -v ruff is not good enough, since the asdf shim may be installed, which will make command -v succeed,
# but autoflake8 may not have been pip installed inside the correct version of python. # but ruff may not have been pip installed inside the correct version of python.
if ! autoflake8 --help >/dev/null ; then if ! ruff --help >/dev/null 2>&1; then
pip install autoflake8 pip install ruff
asdf reshim python
fi
if ! autoflake --help >/dev/null ; then
pip install autoflake
asdf reshim python
fi
if ! autopep8 --help >/dev/null ; then
pip install autopep8
asdf reshim python asdf reshim python
fi fi
python_dirs=$(get_python_dirs) python_dirs=$(get_python_dirs)
python_files=$(find $python_dirs -type f -name "*.py" ! -name '.null-ls*' ! -name '_null-ls*') python_files=$(find $python_dirs -type f -name "*.py" ! -name '.null-ls*' ! -name '_null-ls*')
autoflake8 --in-place --remove-unused-variables --remove-duplicate-keys --expand-star-imports --exit-zero-even-if-changed $python_files ruff --fix $python_files
autoflake --in-place --remove-all-unused-imports $python_files
autopep8 --in-place $python_files
} }
function run_pre_commmit() { function run_pre_commmit() {
@ -71,7 +59,7 @@ done
for python_project in "${python_projects[@]}" ; do for python_project in "${python_projects[@]}" ; do
if [[ "$subcommand" != "pre" ]] || [[ -n "$(git status --porcelain "$python_project")" ]]; then if [[ "$subcommand" != "pre" ]] || [[ -n "$(git status --porcelain "$python_project")" ]]; then
pushd "$python_project" pushd "$python_project"
run_autoflake || run_autoflake run_autofixers || run_autofixers
popd popd
fi fi
done done

View File

@ -171,7 +171,7 @@ select = [
"E", # pycodestyle error "E", # pycodestyle error
# "ERA", # eradicate # "ERA", # eradicate
"F", # pyflakes "F", # pyflakes
# "N", # pep8-naming "N", # pep8-naming
# "PL", # pylint # "PL", # pylint
# "S", # flake8-bandit # "S", # flake8-bandit
"UP", # pyupgrade "UP", # pyupgrade

View File

@ -46,12 +46,14 @@ class JsonFormatter(logging.Formatter):
self.default_msec_format = msec_format self.default_msec_format = msec_format
self.datefmt = None self.datefmt = None
def usesTime(self) -> bool: def usesTime(self) -> bool: # noqa: N802, this is overriding a method from python's stdlib
"""Overwritten to look for the attribute in the format dict values instead of the fmt string.""" """Overwritten to look for the attribute in the format dict values instead of the fmt string."""
return "asctime" in self.fmt_dict.values() return "asctime" in self.fmt_dict.values()
# we are overriding a method that returns a string and returning a dict, hence the Any # we are overriding a method that returns a string and returning a dict, hence the Any
def formatMessage(self, record: logging.LogRecord) -> Any: def formatMessage( # noqa: N802, this is overriding a method from python's stdlib
self, record: logging.LogRecord
) -> Any:
"""Overwritten to return a dictionary of the relevant LogRecord attributes instead of a string. """Overwritten to return a dictionary of the relevant LogRecord attributes instead of a string.
KeyError is raised if an unknown attribute is provided in the fmt_dict. KeyError is raised if an unknown attribute is provided in the fmt_dict.

View File

@ -26,11 +26,11 @@ class NoTestCasesFoundError(Exception):
pass pass
class MissingInputTaskData(Exception): class MissingInputTaskDataError(Exception):
pass pass
class UnsupporterRunnerDelegateGiven(Exception): class UnsupporterRunnerDelegateGivenError(Exception):
pass pass
@ -236,7 +236,7 @@ class ProcessModelTestRunner:
self.test_case_identifier = test_case_identifier self.test_case_identifier = test_case_identifier
if not issubclass(process_model_test_runner_delegate_class, ProcessModelTestRunnerDelegate): if not issubclass(process_model_test_runner_delegate_class, ProcessModelTestRunnerDelegate):
raise UnsupporterRunnerDelegateGiven( raise UnsupporterRunnerDelegateGivenError(
"Process model test runner delegate must inherit from ProcessModelTestRunnerDelegate. Given" "Process model test runner delegate must inherit from ProcessModelTestRunnerDelegate. Given"
f" class '{process_model_test_runner_delegate_class}' does not" f" class '{process_model_test_runner_delegate_class}' does not"
) )
@ -342,7 +342,7 @@ class ProcessModelTestRunner:
task_data_length = len(test_case_task_properties["data"]) task_data_length = len(test_case_task_properties["data"])
test_case_index = self.task_data_index[test_case_task_key] test_case_index = self.task_data_index[test_case_task_key]
if task_data_length <= test_case_index: if task_data_length <= test_case_index:
raise MissingInputTaskData( raise MissingInputTaskDataError(
f"Missing input task data for task: {test_case_task_key}. " f"Missing input task data for task: {test_case_task_key}. "
f"Only {task_data_length} given in the json but task was called {test_case_index + 1} times" f"Only {task_data_length} given in the json but task was called {test_case_index + 1} times"
) )

View File

@ -5,7 +5,7 @@ from flask import Flask
from flask import current_app from flask import current_app
from spiffworkflow_backend.services.process_model_test_runner_service import NoTestCasesFoundError from spiffworkflow_backend.services.process_model_test_runner_service import NoTestCasesFoundError
from spiffworkflow_backend.services.process_model_test_runner_service import ProcessModelTestRunner from spiffworkflow_backend.services.process_model_test_runner_service import ProcessModelTestRunner
from spiffworkflow_backend.services.process_model_test_runner_service import UnsupporterRunnerDelegateGiven from spiffworkflow_backend.services.process_model_test_runner_service import UnsupporterRunnerDelegateGivenError
from tests.spiffworkflow_backend.helpers.base_test import BaseTest from tests.spiffworkflow_backend.helpers.base_test import BaseTest
@ -34,7 +34,7 @@ class TestProcessModelTestRunner(BaseTest):
app: Flask, app: Flask,
with_db_and_bpmn_file_cleanup: None, with_db_and_bpmn_file_cleanup: None,
) -> None: ) -> None:
with pytest.raises(UnsupporterRunnerDelegateGiven): with pytest.raises(UnsupporterRunnerDelegateGivenError):
ProcessModelTestRunner( ProcessModelTestRunner(
os.path.join(self.root_path(), "DNE"), process_model_test_runner_delegate_class=NoTestCasesFoundError os.path.join(self.root_path(), "DNE"), process_model_test_runner_delegate_class=NoTestCasesFoundError
) )