From e63c645d4d47d3a1577535d118924244a4b8c849 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 29 Apr 2024 12:22:34 +0100 Subject: [PATCH] [`pylint`] Also emit `PLR0206` for properties with variadic parameters --- .../fixtures/pylint/property_with_parameters.py | 10 ++++++++++ .../pylint/rules/property_with_parameters.rs | 15 ++++----------- ...ests__PLR0206_property_with_parameters.py.snap | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/property_with_parameters.py b/crates/ruff_linter/resources/test/fixtures/pylint/property_with_parameters.py index 580c319038b9f..dba24c4a1e388 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/property_with_parameters.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/property_with_parameters.py @@ -28,3 +28,13 @@ def example(self): @abstractmethod def example(self, value): """Setter.""" + + +class VariadicParameters: + @property + def attribute_var_args(self, *args): # [property-with-parameters] + return sum(args) + + @property + def attribute_var_kwargs(self, **kwargs): #[property-with-parameters] + return {key: value * 2 for key, value in kwargs.items()} diff --git a/crates/ruff_linter/src/rules/pylint/rules/property_with_parameters.rs b/crates/ruff_linter/src/rules/pylint/rules/property_with_parameters.rs index 01661edc6f7da..835f19c1a38b4 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/property_with_parameters.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/property_with_parameters.rs @@ -51,20 +51,13 @@ pub(crate) fn property_with_parameters( decorator_list: &[Decorator], parameters: &Parameters, ) { - let semantic = checker.semantic(); - if !decorator_list - .iter() - .any(|decorator| semantic.match_builtin_expr(&decorator.expression, "property")) - { + if parameters.len() <= 1 { return; } - if parameters - .posonlyargs + let semantic = checker.semantic(); + if decorator_list .iter() - .chain(¶meters.args) - .chain(¶meters.kwonlyargs) - .count() - > 1 + .any(|decorator| semantic.match_builtin_expr(&decorator.expression, "property")) { checker .diagnostics diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR0206_property_with_parameters.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR0206_property_with_parameters.py.snap index d3a8e325da421..50ffeaf9636b8 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR0206_property_with_parameters.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLR0206_property_with_parameters.py.snap @@ -26,4 +26,19 @@ property_with_parameters.py:15:9: PLR0206 Cannot have defined parameters for pro 16 | return param + param1 | +property_with_parameters.py:35:9: PLR0206 Cannot have defined parameters for properties + | +33 | class VariadicParameters: +34 | @property +35 | def attribute_var_args(self, *args): # [property-with-parameters] + | ^^^^^^^^^^^^^^^^^^ PLR0206 +36 | return sum(args) + | +property_with_parameters.py:39:9: PLR0206 Cannot have defined parameters for properties + | +38 | @property +39 | def attribute_var_kwargs(self, **kwargs): #[property-with-parameters] + | ^^^^^^^^^^^^^^^^^^^^ PLR0206 +40 | return {key: value * 2 for key, value in kwargs.items()} + |