oxsecurity / megalinter

🦙 MegaLinter analyzes 50 languages, 22 formats, 21 tooling formats, excessive copy-pastes, spelling mistakes and security issues in your repository sources with a GitHub Action, other CI tools or locally.
https://megalinter.io
GNU Affero General Public License v3.0
1.82k stars 214 forks source link

Python Black: turn warnings into errors (e.g. `--strict`) #871

Closed bittner closed 2 years ago

bittner commented 2 years ago

I'm using Black to lint Python code, and when it finds code it would reformat Mega-Linter identifies them as warnings. Hence the pipeline job succeeds.

✅ Linted [PYTHON] files with [black]: Found 1 non blocking error(s) - (0.35s)

According to Black's usage screen black --diff --check exits with a non-zero status code, hence I assume the command should fail.

There doesn't seem to be any configuration switch to turn such warnings into errors (something like a --strict argument).

Is this by design? Is there no way treat all warnings as errors, to avoid overlooking linter complaints?

nvuillam commented 2 years ago

You can add arguments in .mega-linter.yml file :)

In your case, it would be PYTHON_BLACK_ARGUMENTS: ["--strict"]

nvuillam commented 2 years ago

Please reopen if my solution does not work :)

llaville commented 2 years ago

@nvuillam the --strict option does not exists in black linter. It was just a suggestion by @bittner

bittner commented 2 years ago

@nvuillam Can you reopen the issue? It seems like I can't do that myself.

My point is: Black would actually error out, but the internal logic of Mega-Linter seems to qualify these (or some?) errors as "simply warnings". Is there is any switch to make Mega-Linter error out? If not, should we add one?

bittner commented 2 years ago

It seems like isort has a similar behavior. Likely because its interface, the one Mega-Linter uses, is very similar to Black. I would need this linter to fail our pipeline, too, on warnings.

nvuillam commented 2 years ago

MegaLinter fails when the return code is > 0, is there a way to make black return 1 or more in case of warnings ?

bittner commented 2 years ago

Black does exit with a status code != 0. That's what the --check option does. The question is, how does Mega-Linter decide to identify this as a "warning"?

nvuillam commented 2 years ago

Linters defined as formatters in descriptors trigger a warning by default

You can change this behavior by defining FORMATTERS_DISABLE_ERRORS: false :)

bittner commented 2 years ago

Nice, thank you! :1st_place_medal: