Added pylint formatter (#1995)

Fixes: #1953

@charliermarsh thank you for the tips in the issue.

I'm not very familiar with Rust, so please excuse if my string formatting syntax is messy.

In terms of testing, I compared output of `flake8 --format=pylint ` and `cargo run --format=pylint` on the same code and the output syntax seems to check out.
This commit is contained in:
Damien Allen 2023-01-19 14:01:27 +01:00 committed by GitHub
parent 26901a78c9
commit 6ddfe50ac4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 10 deletions

View File

@ -381,7 +381,7 @@ Options:
--per-file-ignores <PER_FILE_IGNORES> --per-file-ignores <PER_FILE_IGNORES>
List of mappings from file pattern to code to exclude List of mappings from file pattern to code to exclude
--format <FORMAT> --format <FORMAT>
Output serialization format for violations [env: RUFF_FORMAT=] [possible values: text, json, junit, grouped, github, gitlab] Output serialization format for violations [env: RUFF_FORMAT=] [possible values: text, json, junit, grouped, github, gitlab, pylint]
--stdin-filename <STDIN_FILENAME> --stdin-filename <STDIN_FILENAME>
The name of the file when passing it through stdin The name of the file when passing it through stdin
--cache-dir <CACHE_DIR> --cache-dir <CACHE_DIR>
@ -2120,9 +2120,9 @@ force-exclude = true
The style in which violation messages should be formatted: `"text"` The style in which violation messages should be formatted: `"text"`
(default), `"grouped"` (group messages by file), `"json"` (default), `"grouped"` (group messages by file), `"json"`
(machine-readable), `"junit"` (machine-readable XML), `"github"` (machine-readable), `"junit"` (machine-readable XML), `"github"` (GitHub
(GitHub Actions annotations) or `"gitlab"` Actions annotations), `"gitlab"` (GitLab CI code quality report), or
(GitLab CI code quality report). `"pylint"` (Pylint text format).
**Default value**: `"text"` **Default value**: `"text"`

View File

@ -227,7 +227,7 @@
] ]
}, },
"format": { "format": {
"description": "The style in which violation messages should be formatted: `\"text\"` (default), `\"grouped\"` (group messages by file), `\"json\"` (machine-readable), `\"junit\"` (machine-readable XML), `\"github\"` (GitHub Actions annotations) or `\"gitlab\"` (GitLab CI code quality report).", "description": "The style in which violation messages should be formatted: `\"text\"` (default), `\"grouped\"` (group messages by file), `\"json\"` (machine-readable), `\"junit\"` (machine-readable XML), `\"github\"` (GitHub Actions annotations), `\"gitlab\"` (GitLab CI code quality report), or `\"pylint\"` (Pylint text format).",
"anyOf": [ "anyOf": [
{ {
"$ref": "#/definitions/SerializationFormat" "$ref": "#/definitions/SerializationFormat"
@ -1804,7 +1804,8 @@
"junit", "junit",
"grouped", "grouped",
"github", "github",
"gitlab" "gitlab",
"pylint"
] ]
}, },
"Strictness": { "Strictness": {

View File

@ -319,6 +319,9 @@ pub fn explain(rule: &Rule, format: SerializationFormat) -> Result<()> {
SerializationFormat::Gitlab => { SerializationFormat::Gitlab => {
bail!("`--explain` does not support GitLab format") bail!("`--explain` does not support GitLab format")
} }
SerializationFormat::Pylint => {
bail!("`--explain` does not support pylint format")
}
}; };
Ok(()) Ok(())
} }

View File

@ -285,7 +285,7 @@ impl<'a> Printer<'a> {
} }
} }
SerializationFormat::Gitlab => { SerializationFormat::Gitlab => {
// Generate JSON with errors in GitLab CI format // Generate JSON with violations in GitLab CI format
// https://docs.gitlab.com/ee/ci/testing/code_quality.html#implementing-a-custom-tool // https://docs.gitlab.com/ee/ci/testing/code_quality.html#implementing-a-custom-tool
writeln!(stdout, writeln!(stdout,
"{}", "{}",
@ -312,6 +312,20 @@ impl<'a> Printer<'a> {
)? )?
)?; )?;
} }
SerializationFormat::Pylint => {
// Generate violations in Pylint format.
// See: https://flake8.pycqa.org/en/latest/internal/formatters.html#pylint-formatter
for message in &diagnostics.messages {
let label = format!(
"{}:{}: [{}] {}",
relativize_path(Path::new(&message.filename)),
message.location.row(),
message.kind.rule().code(),
message.kind.body(),
);
writeln!(stdout, "{label}")?;
}
}
} }
stdout.flush()?; stdout.flush()?;

View File

@ -208,9 +208,9 @@ pub struct Options {
)] )]
/// The style in which violation messages should be formatted: `"text"` /// The style in which violation messages should be formatted: `"text"`
/// (default), `"grouped"` (group messages by file), `"json"` /// (default), `"grouped"` (group messages by file), `"json"`
/// (machine-readable), `"junit"` (machine-readable XML), `"github"` /// (machine-readable), `"junit"` (machine-readable XML), `"github"` (GitHub
/// (GitHub Actions annotations) or `"gitlab"` /// Actions annotations), `"gitlab"` (GitLab CI code quality report), or
/// (GitLab CI code quality report). /// `"pylint"` (Pylint text format).
pub format: Option<SerializationFormat>, pub format: Option<SerializationFormat>,
#[option( #[option(
default = r#"false"#, default = r#"false"#,

View File

@ -156,6 +156,7 @@ pub enum SerializationFormat {
Grouped, Grouped,
Github, Github,
Gitlab, Gitlab,
Pylint,
} }
impl Default for SerializationFormat { impl Default for SerializationFormat {