Skip to content

Commit

Permalink
[flake8-builtins] Add builtins-ignorelist Option (#2061)
Browse files Browse the repository at this point in the history
Closes #2053.
  • Loading branch information
saadmk11 committed Jan 21, 2023
1 parent 80295f3 commit 0c30768
Show file tree
Hide file tree
Showing 22 changed files with 555 additions and 58 deletions.
3 changes: 3 additions & 0 deletions resources/test/fixtures/flake8_builtins/A001.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import some as sum
from some import other as int
from directory import new as dir

print = 1
copyright: 'annotation' = 2
(complex := 3)
float = object = 4
min, max = 5, 6

id = 4

def bytes():
pass

Expand Down
2 changes: 2 additions & 0 deletions resources/test/fixtures/flake8_builtins/A002.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ def func1(str, /, type, *complex, Exception, **getattr):
async def func2(bytes):
pass

async def func3(id, dir):
pass

map([], lambda float: ...)
4 changes: 4 additions & 0 deletions resources/test/fixtures/flake8_builtins/A003.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class MyClass:
ImportError = 4
id = 5
dir = "/"

def __init__(self):
self.float = 5 # is fine
self.id = 10
self.dir = "."

def str(self):
pass
3 changes: 3 additions & 0 deletions resources/test/fixtures/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ per-file-ignores = { "__init__.py" = ["F401"] }
[tool.ruff.flake8-bugbear]
extend-immutable-calls = ["fastapi.Depends", "fastapi.Query"]

[tool.ruff.flake8-builtins]
builtins-ignorelist = ["id", "dir"]

[tool.ruff.flake8-quotes]
inline-quotes = "single"
multiline-quotes = "double"
Expand Down
27 changes: 27 additions & 0 deletions ruff.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@
}
]
},
"flake8-builtins": {
"description": "Options for the `flake8-builtins` plugin.",
"anyOf": [
{
"$ref": "#/definitions/Flake8BuiltinsOptions"
},
{
"type": "null"
}
]
},
"flake8-errmsg": {
"description": "Options for the `flake8-errmsg` plugin.",
"anyOf": [
Expand Down Expand Up @@ -584,6 +595,22 @@
},
"additionalProperties": false
},
"Flake8BuiltinsOptions": {
"type": "object",
"properties": {
"builtins-ignorelist": {
"description": "Ignore list of builtins.",
"type": [
"array",
"null"
],
"items": {
"type": "string"
}
}
},
"additionalProperties": false
},
"Flake8ErrMsgOptions": {
"type": "object",
"properties": {
Expand Down
3 changes: 3 additions & 0 deletions src/checkers/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4735,6 +4735,7 @@ impl<'a> Checker<'a> {
name,
located,
flake8_builtins::types::ShadowingType::Attribute,
&self.settings.flake8_builtins.builtins_ignorelist,
) {
self.diagnostics.push(diagnostic);
}
Expand All @@ -4745,6 +4746,7 @@ impl<'a> Checker<'a> {
name,
located,
flake8_builtins::types::ShadowingType::Variable,
&self.settings.flake8_builtins.builtins_ignorelist,
) {
self.diagnostics.push(diagnostic);
}
Expand All @@ -4758,6 +4760,7 @@ impl<'a> Checker<'a> {
name,
arg,
flake8_builtins::types::ShadowingType::Argument,
&self.settings.flake8_builtins.builtins_ignorelist,
) {
self.diagnostics.push(diagnostic);
}
Expand Down
20 changes: 18 additions & 2 deletions src/flake8_to_ruff/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::rules::flake8_quotes::settings::Quote;
use crate::rules::flake8_tidy_imports::relative_imports::Strictness;
use crate::rules::pydocstyle::settings::Convention;
use crate::rules::{
flake8_annotations, flake8_bugbear, flake8_errmsg, flake8_pytest_style, flake8_quotes,
flake8_tidy_imports, mccabe, pep8_naming, pydocstyle,
flake8_annotations, flake8_bugbear, flake8_builtins, flake8_errmsg, flake8_pytest_style,
flake8_quotes, flake8_tidy_imports, mccabe, pep8_naming, pydocstyle,
};
use crate::settings::options::Options;
use crate::settings::pyproject::Pyproject;
Expand Down Expand Up @@ -90,6 +90,7 @@ pub fn convert(
let mut options = Options::default();
let mut flake8_annotations = flake8_annotations::settings::Options::default();
let mut flake8_bugbear = flake8_bugbear::settings::Options::default();
let mut flake8_builtins = flake8_builtins::settings::Options::default();
let mut flake8_errmsg = flake8_errmsg::settings::Options::default();
let mut flake8_pytest_style = flake8_pytest_style::settings::Options::default();
let mut flake8_quotes = flake8_quotes::settings::Options::default();
Expand Down Expand Up @@ -147,6 +148,11 @@ pub fn convert(
flake8_bugbear.extend_immutable_calls =
Some(parser::parse_strings(value.as_ref()));
}
// flake8-builtins
"builtins-ignorelist" | "builtins_ignorelist" => {
flake8_builtins.builtins_ignorelist =
Some(parser::parse_strings(value.as_ref()));
}
// flake8-annotations
"suppress-none-returning" | "suppress_none_returning" => {
match parser::parse_bool(value.as_ref()) {
Expand Down Expand Up @@ -345,6 +351,9 @@ pub fn convert(
if flake8_bugbear != flake8_bugbear::settings::Options::default() {
options.flake8_bugbear = Some(flake8_bugbear);
}
if flake8_builtins != flake8_builtins::settings::Options::default() {
options.flake8_builtins = Some(flake8_builtins);
}
if flake8_errmsg != flake8_errmsg::settings::Options::default() {
options.flake8_errmsg = Some(flake8_errmsg);
}
Expand Down Expand Up @@ -439,6 +448,7 @@ mod tests {
flake8_annotations: None,
flake8_bandit: None,
flake8_bugbear: None,
flake8_builtins: None,
flake8_errmsg: None,
flake8_pytest_style: None,
flake8_quotes: None,
Expand Down Expand Up @@ -502,6 +512,7 @@ mod tests {
flake8_annotations: None,
flake8_bandit: None,
flake8_bugbear: None,
flake8_builtins: None,
flake8_errmsg: None,
flake8_pytest_style: None,
flake8_quotes: None,
Expand Down Expand Up @@ -565,6 +576,7 @@ mod tests {
flake8_annotations: None,
flake8_bandit: None,
flake8_bugbear: None,
flake8_builtins: None,
flake8_errmsg: None,
flake8_pytest_style: None,
flake8_quotes: None,
Expand Down Expand Up @@ -628,6 +640,7 @@ mod tests {
flake8_annotations: None,
flake8_bandit: None,
flake8_bugbear: None,
flake8_builtins: None,
flake8_errmsg: None,
flake8_pytest_style: None,
flake8_quotes: None,
Expand Down Expand Up @@ -691,6 +704,7 @@ mod tests {
flake8_annotations: None,
flake8_bandit: None,
flake8_bugbear: None,
flake8_builtins: None,
flake8_errmsg: None,
flake8_pytest_style: None,
flake8_quotes: Some(flake8_quotes::settings::Options {
Expand Down Expand Up @@ -767,6 +781,7 @@ mod tests {
flake8_annotations: None,
flake8_bandit: None,
flake8_bugbear: None,
flake8_builtins: None,
flake8_errmsg: None,
flake8_pytest_style: None,
flake8_quotes: None,
Expand Down Expand Up @@ -837,6 +852,7 @@ mod tests {
flake8_annotations: None,
flake8_bandit: None,
flake8_bugbear: None,
flake8_builtins: None,
flake8_errmsg: None,
flake8_pytest_style: None,
flake8_quotes: Some(flake8_quotes::settings::Options {
Expand Down
8 changes: 5 additions & 3 deletions src/lib_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ use crate::directives;
use crate::linter::check_path;
use crate::registry::Rule;
use crate::rules::{
flake8_annotations, flake8_bandit, flake8_bugbear, flake8_errmsg, flake8_import_conventions,
flake8_pytest_style, flake8_quotes, flake8_tidy_imports, flake8_unused_arguments, isort,
mccabe, pep8_naming, pycodestyle, pydocstyle, pylint, pyupgrade,
flake8_annotations, flake8_bandit, flake8_bugbear, flake8_builtins, flake8_errmsg,
flake8_import_conventions, flake8_pytest_style, flake8_quotes, flake8_tidy_imports,
flake8_unused_arguments, isort, mccabe, pep8_naming, pycodestyle, pydocstyle, pylint,
pyupgrade,
};
use crate::rustpython_helpers::tokenize;
use crate::settings::configuration::Configuration;
Expand Down Expand Up @@ -136,6 +137,7 @@ pub fn defaultSettings() -> Result<JsValue, JsValue> {
flake8_annotations: Some(flake8_annotations::settings::Settings::default().into()),
flake8_bandit: Some(flake8_bandit::settings::Settings::default().into()),
flake8_bugbear: Some(flake8_bugbear::settings::Settings::default().into()),
flake8_builtins: Some(flake8_builtins::settings::Settings::default().into()),
flake8_errmsg: Some(flake8_errmsg::settings::Settings::default().into()),
flake8_pytest_style: Some(flake8_pytest_style::settings::Settings::default().into()),
flake8_quotes: Some(flake8_quotes::settings::Settings::default().into()),
Expand Down
31 changes: 29 additions & 2 deletions src/rules/flake8_builtins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Rules from [flake8-builtins](https://pypi.org/project/flake8-builtins/2.0.1/).
pub(crate) mod rules;
pub mod settings;
pub(crate) mod types;

#[cfg(test)]
Expand All @@ -11,7 +12,7 @@ mod tests {

use crate::linter::test_path;
use crate::registry::Rule;
use crate::settings;
use crate::settings::Settings;

#[test_case(Rule::BuiltinVariableShadowing, Path::new("A001.py"); "A001")]
#[test_case(Rule::BuiltinArgumentShadowing, Path::new("A002.py"); "A002")]
Expand All @@ -22,9 +23,35 @@ mod tests {
Path::new("./resources/test/fixtures/flake8_builtins")
.join(path)
.as_path(),
&settings::Settings::for_rule(rule_code),
&Settings::for_rule(rule_code),
)?;
insta::assert_yaml_snapshot!(snapshot, diagnostics);
Ok(())
}

#[test_case(Rule::BuiltinVariableShadowing, Path::new("A001.py"); "A001")]
#[test_case(Rule::BuiltinArgumentShadowing, Path::new("A002.py"); "A002")]
#[test_case(Rule::BuiltinAttributeShadowing, Path::new("A003.py"); "A003")]
fn builtins_ignorelist(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"{}_{}_builtins_ignorelist",
rule_code.code(),
path.to_string_lossy()
);

let diagnostics = test_path(
Path::new("./resources/test/fixtures/flake8_builtins")
.join(path)
.as_path(),
&Settings {
flake8_builtins: super::settings::Settings {
builtins_ignorelist: vec!["id".to_string(), "dir".to_string()],
},
..Settings::for_rules(vec![rule_code])
},
)?;

insta::assert_yaml_snapshot!(snapshot, diagnostics);
Ok(())
}
}
3 changes: 2 additions & 1 deletion src/rules/flake8_builtins/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ pub fn builtin_shadowing<T>(
name: &str,
located: &Located<T>,
node_type: ShadowingType,
ignorelist: &[String],
) -> Option<Diagnostic> {
if BUILTINS.contains(&name) {
if BUILTINS.contains(&name) && !ignorelist.contains(&name.to_string()) {
Some(Diagnostic::new::<DiagnosticKind>(
match node_type {
ShadowingType::Variable => {
Expand Down
44 changes: 44 additions & 0 deletions src/rules/flake8_builtins/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//! Settings for the `flake8-builtins` plugin.

use ruff_macros::ConfigurationOptions;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(
Debug, PartialEq, Eq, Default, Serialize, Deserialize, ConfigurationOptions, JsonSchema,
)]
#[serde(
deny_unknown_fields,
rename_all = "kebab-case",
rename = "Flake8BuiltinsOptions"
)]
pub struct Options {
#[option(
default = r#"[]"#,
value_type = "Vec<String>",
example = "builtins-ignorelist = [\"id\"]"
)]
/// Ignore list of builtins.
pub builtins_ignorelist: Option<Vec<String>>,
}

#[derive(Debug, Default, Hash)]
pub struct Settings {
pub builtins_ignorelist: Vec<String>,
}

impl From<Options> for Settings {
fn from(options: Options) -> Self {
Self {
builtins_ignorelist: options.builtins_ignorelist.unwrap_or_default(),
}
}
}

impl From<Settings> for Options {
fn from(settings: Settings) -> Self {
Self {
builtins_ignorelist: Some(settings.builtins_ignorelist),
}
}
}

0 comments on commit 0c30768

Please sign in to comment.