From cee217500068e15203e007d844c808ae427f2ac8 Mon Sep 17 00:00:00 2001 From: Nathanael Bosch Date: Wed, 26 Aug 2020 11:07:23 +0200 Subject: [PATCH] Global pylint config and new CI check (#176) * Added union of all exceptions to the global pylint config * Hacky script to ensure that tox.ini and pyproject.toml have fitting pylint exceptions * Better and more explanatory script * Moved script and added it to CI * Added a small message to state that everything worked fine --- .github/workflows/linting.yml | 8 ++++ .github/workflows/pylint_check.py | 79 +++++++++++++++++++++++++++++++ pyproject.toml | 27 +++++++++++ 3 files changed, 114 insertions(+) create mode 100644 .github/workflows/pylint_check.py diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index de5be332f..be6e1421d 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -21,3 +21,11 @@ jobs: run: pip install tox - name: Run pylint through tox run: tox -e pylint + + pylint-tox-config-check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + - name: Check if ./tox.ini and ./pyproject.toml are correctly synchronized + run: python .github/workflows/pylint_check.py diff --git a/.github/workflows/pylint_check.py b/.github/workflows/pylint_check.py new file mode 100644 index 000000000..173804301 --- /dev/null +++ b/.github/workflows/pylint_check.py @@ -0,0 +1,79 @@ +from pathlib import Path + +TOXINI_FILE = Path("tox.ini") +PYPROJECTTOML_FILE = Path("pyproject.toml") +GLOBAL_DISABLES = { + "invalid-name", + "no-else-raise", + "no-else-return", +} + + +# Parse ./tox.ini +tox_lines = TOXINI_FILE.read_text().splitlines() +tox_pylint_lines = [l for l in tox_lines if l.strip().startswith("pylint")] +tox_disables = {i for l in tox_pylint_lines for i in l.split('"')[1].split(",")} + + +# Parse ./pyproject.toml +pyproject_lines = PYPROJECTTOML_FILE.read_text().splitlines() +is_in_disable_string = False +pyproject_disables = set() +for line in pyproject_lines: + if is_in_disable_string and line.startswith('"""'): + is_in_disable_string = False + + if is_in_disable_string and line: + pyproject_disables.add(line.strip().strip(",")) + + if line.startswith("disable"): + is_in_disable_string = True + + +# Check correctness and raise errors +try: + in_pyproject_butnot_tox = pyproject_disables.difference(tox_disables).difference( + GLOBAL_DISABLES + ) + assert not in_pyproject_butnot_tox +except AssertionError: + raise Exception( + f""" + The following pylint messages seem to be disabled in `./pyproject.toml`, but not in + any of the pylint calls in `./tox.ini`: {in_pyproject_butnot_tox}. This could have + two reasons with different solutions: + 1. You fixed one or many pylint errors in one of the modules and there is no module + left that needs this specific message disabled. Then, please also remove it from + the `./pyproject.toml` file such that pylint uses the most up-to-date + configuration file. + 2. You added a new global exception to `./pyproject.toml` after deciding that this + is a message that we do not want to enforce anywhere currently. Then, please add + this exception also to the `GLOBAL_DISABLES` variable in this this python script + (`./github/workflows/pylint_check.py`). + + If you are not sure what exactly you are supposed to do, or if you think that this + message is wrong please feel free to ping @nathanaelbosch. + """ + ) + + +try: + in_tox_butnot_pyproject = tox_disables.difference(pyproject_disables) + assert not in_tox_butnot_pyproject +except AssertionError: + raise Exception( + f""" + The following pylint messages seem to be disabled in `./tox.ini`, but not in + `./pyproject.toml`: {in_tox_butnot_pyproject}. Please make sure to add them to + `./pyproject.toml` such that pylint does not raise any warnings that are not + supposed to be fixed right now. + + If you are not sure what exactly you are supposed to do, or if you think that this + message is wrong please feel free to ping @nathanaelbosch. + """ + ) + + +print( + "The pylint exceptions in `./tox.ini` and `./pyproject.toml` seem to be correctly synchronized." +) diff --git a/pyproject.toml b/pyproject.toml index 8001cfe5c..eee6dfe2e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,33 @@ invalid-name, no-else-return, no-else-raise, + +abstract-class-instantiated, +too-many-locals, +too-few-public-methods, +too-many-arguments, +unused-argument, +missing-module-docstring, +missing-function-docstring, +duplicate-code, +protected-access, +no-self-use, +arguments-differ, +attribute-defined-outside-init, +too-many-statements, +too-many-instance-attributes, +too-complex, +too-many-lines, +redefined-builtin, +abstract-method, +too-many-branches, +fixme, +broad-except, +raise-missing-from, +line-too-long, +missing-class-docstring, +unnecessary-pass, +unused-variable, """ # Many more `disable`s are defined in `./tox.ini` on a per-module basis!