Skip to content

Commit 772831e

Browse files
joke1196sonartech
authored andcommitted
SONARPY-3862 S6542 Removed check on variadic parameters (#916)
GitOrigin-RevId: b1d88e70a41c08684962983e79221c71ecb862d1
1 parent 5066ddf commit 772831e

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

python-checks/src/main/java/org/sonar/python/checks/UseOfAnyAsTypeHintCheck.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.sonar.plugins.python.api.tree.Decorator;
2727
import org.sonar.plugins.python.api.tree.FunctionDef;
2828
import org.sonar.plugins.python.api.tree.Name;
29+
import org.sonar.plugins.python.api.tree.Parameter;
2930
import org.sonar.plugins.python.api.tree.Tree;
3031
import org.sonar.plugins.python.api.tree.TypeAnnotation;
3132
import org.sonar.python.semantic.SymbolUtils;
@@ -55,12 +56,20 @@ private static void checkForAnyInReturnTypeAndParameters(SubscriptionContext ctx
5556
TypeAnnotation typeAnnotation = (TypeAnnotation) ctx.syntaxNode();
5657
Optional.of(typeAnnotation)
5758
.filter(UseOfAnyAsTypeHintCheck::isTypeAny)
59+
.filter(Predicate.not(UseOfAnyAsTypeHintCheck::isVariadicParameter))
5860
.map(annotation -> (FunctionDef) TreeUtils.firstAncestorOfKind(annotation, Tree.Kind.FUNCDEF))
5961
.filter(Predicate.not(UseOfAnyAsTypeHintCheck::hasFunctionOverrideOrOverloadDecorator))
6062
.filter(Predicate.not(UseOfAnyAsTypeHintCheck::canFunctionBeAnOverride))
6163
.ifPresent(functionDef -> ctx.addIssue(typeAnnotation.expression(), MESSAGE));
6264
}
6365

66+
private static boolean isVariadicParameter(TypeAnnotation typeAnnotation) {
67+
return Optional.ofNullable(typeAnnotation.parent())
68+
.flatMap(TreeUtils.toOptionalInstanceOfMapper(Parameter.class))
69+
.map(Parameter::starToken)
70+
.isPresent();
71+
}
72+
6473
private static boolean isTypeAny(@Nullable TypeAnnotation typeAnnotation) {
6574
return Optional.ofNullable(typeAnnotation)
6675
.map(TypeAnnotation::expression)

python-checks/src/test/resources/checks/useOfAnyAsTypeHintCheck/useOfAnyAsTypeHint.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,26 @@ class LocalClassWithAnnotatedMember:
126126
class LocalClassChild(LocalClassWithAnnotatedMember):
127127
def my_member(self, param: Any) -> Any: # OK, defined in parent
128128
...
129+
130+
# Variadic parameters (*args, **kwargs) should not raise issues
131+
def function_with_args(*args: Any) -> None: # Compliant
132+
pass
133+
134+
def function_with_kwargs(**kwargs: Any) -> None: # Compliant
135+
pass
136+
137+
def function_with_both(*some_args: Any, **kwargs: Any) -> None: # Compliant
138+
pass
139+
140+
def function_with_mixed(param: Any, *args: Any, **kwargs: Any) -> str: # Noncompliant
141+
pass
142+
143+
class ClassWithVariadicMethods:
144+
def method_with_args(self, *args: Any) -> None: # Compliant
145+
pass
146+
147+
def method_with_kwargs(self, **kwargs: Any) -> None: # Compliant
148+
pass
149+
150+
def method_with_both(self, param: Any, *args: Any, **kwargs: Any) -> Any: # Noncompliant 2
151+
pass

0 commit comments

Comments
 (0)