Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pylint] Add allow-dunder-method-names setting for bad-dunder-method-name (PLW3201) #8812

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Rename setting
  • Loading branch information
charliermarsh committed Nov 21, 2023
commit 1e36734eb9a04805aaf6a496400a198b55ba30dc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def _missing_(cls, value):
def _(self):
pass

# Allow custom dunder names (via setting)
# Allow custom dunder names (via setting).
def __special_custom_magic__(self):
pass

Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/rules/pylint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ mod tests {
Path::new("pylint").join(path).as_path(),
&LinterSettings {
pylint: pylint::settings::Settings {
custom_dunder_method_names: FxHashSet::from_iter([
allow_dunder_method_names: FxHashSet::from_iter([
"__special_custom_magic__".to_string()
]),
..pylint::settings::Settings::default()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ use crate::checkers::ast::Checker;
/// that diverges from standard Python dunder methods could potentially
/// confuse someone reading the code.
///
/// Non-standard dunder methods that are required by some library you're
/// using can be defined using the [`pylint.custom-dunder-method-names`]
/// configuration option.
///
/// This rule will detect all methods starting and ending with at least
/// one underscore (e.g., `_str_`), but ignores known dunder methods (like
/// `__init__`), as well as methods that are marked with `@override`.
///
/// Additional dunder methods names can be allowed via the
/// [`pylint.allow-dunder-method-names`] setting.
///
/// ## Example
/// ```python
/// class Foo:
Expand All @@ -41,7 +40,7 @@ use crate::checkers::ast::Checker;
/// ```
///
/// ## Options
/// - `pylint.custom-dunder-method-names`
/// - `pylint.allow-dunder-method-names`
#[violation]
pub struct BadDunderMethodName {
name: String,
Expand All @@ -65,7 +64,7 @@ pub(crate) fn bad_dunder_method_name(checker: &mut Checker, class_body: &[Stmt])
|| checker
.settings
.pylint
.custom_dunder_method_names
.allow_dunder_method_names
.contains(method.name.as_str())
|| matches!(method.name.as_str(), "_")
{
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/rules/pylint/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl ConstantType {
#[derive(Debug, CacheKey)]
pub struct Settings {
pub allow_magic_value_types: Vec<ConstantType>,
pub custom_dunder_method_names: FxHashSet<String>,
pub allow_dunder_method_names: FxHashSet<String>,
pub max_args: usize,
pub max_returns: usize,
pub max_bool_expr: usize,
Expand All @@ -50,7 +50,7 @@ impl Default for Settings {
fn default() -> Self {
Self {
allow_magic_value_types: vec![ConstantType::Str, ConstantType::Bytes],
custom_dunder_method_names: FxHashSet::default(),
allow_dunder_method_names: FxHashSet::default(),
max_args: 5,
max_returns: 6,
max_bool_expr: 5,
Expand Down
9 changes: 5 additions & 4 deletions crates/ruff_workspace/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2545,15 +2545,16 @@ pub struct PylintOptions {
)]
pub allow_magic_value_types: Option<Vec<ConstantType>>,

/// Custom names to allow for dunder methods (see: `PLW3201`).
/// Dunder methods name to allow, in addition to the default set from the
/// Python standard library (see: `PLW3201`).
#[option(
default = r#"[]"#,
value_type = r#"list[str]"#,
example = r#"
custom-dunder-method-names = ["__tablename__", "__table_args__"]
allow-dunder-method-names = ["__tablename__", "__table_args__"]
"#
)]
pub custom_dunder_method_names: Option<FxHashSet<String>>,
pub allow_dunder_method_names: Option<FxHashSet<String>>,

/// Maximum number of branches allowed for a function or method body (see:
/// `PLR0912`).
Expand Down Expand Up @@ -2596,7 +2597,7 @@ impl PylintOptions {
allow_magic_value_types: self
.allow_magic_value_types
.unwrap_or(defaults.allow_magic_value_types),
custom_dunder_method_names: self.custom_dunder_method_names.unwrap_or_default(),
allow_dunder_method_names: self.allow_dunder_method_names.unwrap_or_default(),
max_args: self.max_args.unwrap_or(defaults.max_args),
max_bool_expr: self.max_bool_expr.unwrap_or(defaults.max_bool_expr),
max_returns: self.max_returns.unwrap_or(defaults.max_returns),
Expand Down
18 changes: 9 additions & 9 deletions ruff.schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading