|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * You can redistribute and/or modify this program under the terms of |
| 7 | + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.python.checks; |
| 18 | + |
| 19 | +import java.util.Set; |
| 20 | +import java.util.stream.Stream; |
| 21 | +import org.sonar.check.Rule; |
| 22 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 23 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 24 | +import org.sonar.plugins.python.api.tree.AnnotatedAssignment; |
| 25 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 26 | +import org.sonar.plugins.python.api.tree.ClassDef; |
| 27 | +import org.sonar.plugins.python.api.tree.Decorator; |
| 28 | +import org.sonar.plugins.python.api.tree.Expression; |
| 29 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 30 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 31 | +import org.sonar.plugins.python.api.tree.SubscriptionExpression; |
| 32 | +import org.sonar.plugins.python.api.tree.Tree; |
| 33 | +import org.sonar.plugins.python.api.tree.TypeAnnotation; |
| 34 | +import org.sonar.plugins.python.api.types.v2.matchers.TypeMatcher; |
| 35 | +import org.sonar.plugins.python.api.types.v2.matchers.TypeMatchers; |
| 36 | +import org.sonar.python.tree.TreeUtils; |
| 37 | + |
| 38 | +@Rule(key = "S8685") |
| 39 | +public class DataClassFunctionCallDefaultCheck extends PythonSubscriptionCheck { |
| 40 | + |
| 41 | + private static final String MESSAGE = "Use \"field(default_factory=...)\" instead of a function call as a default value."; |
| 42 | + private static final String DEFAULT_KEYWORD = "default"; |
| 43 | + |
| 44 | + private static final TypeMatcher IS_DATACLASS_DECORATOR = TypeMatchers.isType("dataclasses.dataclass"); |
| 45 | + private static final TypeMatcher IS_DATACLASSES_FIELD = TypeMatchers.isType("dataclasses.field"); |
| 46 | + private static final TypeMatcher IS_CLASS_VAR = TypeMatchers.isType("typing.ClassVar"); |
| 47 | + |
| 48 | + // Allowlist of callables whose result should be re-evaluated per dataclass instance. |
| 49 | + // Calls to anything outside this list are assumed safe (e.g. user-defined helpers, frozen-dataclass |
| 50 | + // constructors, wrappers around dataclasses.field(default_factory=...)) — we prefer FNs over FPs here. |
| 51 | + // |
| 52 | + // NOTE: random.* top-level functions are intentionally absent. In typeshed they are aliased from a |
| 53 | + // module-level Random() instance (`randint = _inst.randint`, ...) and the V2 typeshed serializer |
| 54 | + // flattens the bound-method type to CallableType[builtins.function], so isType("random.randint") |
| 55 | + // resolves through Unknown. They are matched separately below via the random module qualifier. |
| 56 | + private static final TypeMatcher IS_PROBLEMATIC_FACTORY = TypeMatchers.any(Stream.of( |
| 57 | + // Current time / clock readings |
| 58 | + "datetime.datetime.now", |
| 59 | + "datetime.datetime.utcnow", |
| 60 | + "datetime.datetime.today", |
| 61 | + "datetime.datetime.fromtimestamp", |
| 62 | + "datetime.datetime.utcfromtimestamp", |
| 63 | + "datetime.date.today", |
| 64 | + "datetime.date.fromtimestamp", |
| 65 | + "time.time", |
| 66 | + "time.time_ns", |
| 67 | + "time.monotonic", |
| 68 | + "time.monotonic_ns", |
| 69 | + "time.perf_counter", |
| 70 | + "time.perf_counter_ns", |
| 71 | + "time.process_time", |
| 72 | + "time.process_time_ns", |
| 73 | + "time.localtime", |
| 74 | + "time.gmtime", |
| 75 | + // UUIDs |
| 76 | + "uuid.uuid1", |
| 77 | + "uuid.uuid3", |
| 78 | + "uuid.uuid4", |
| 79 | + "uuid.uuid5", |
| 80 | + // Secrets |
| 81 | + "secrets.token_hex", |
| 82 | + "secrets.token_bytes", |
| 83 | + "secrets.token_urlsafe", |
| 84 | + "secrets.choice", |
| 85 | + "secrets.randbelow", |
| 86 | + "secrets.randbits", |
| 87 | + // OS randomness |
| 88 | + "os.urandom", |
| 89 | + // Mutable container constructors — shared across all instances otherwise |
| 90 | + "builtins.list", |
| 91 | + "builtins.dict", |
| 92 | + "builtins.set", |
| 93 | + "builtins.bytearray", |
| 94 | + "collections.defaultdict", |
| 95 | + "collections.OrderedDict", |
| 96 | + "collections.Counter", |
| 97 | + "collections.deque").map(TypeMatchers::isType)); |
| 98 | + |
| 99 | + // The random module type itself is known, even though its top-level function members all resolve |
| 100 | + // to Unknown (see NOTE on IS_PROBLEMATIC_FACTORY). We match `<random>.<name>` syntactically. |
| 101 | + private static final TypeMatcher IS_RANDOM_MODULE = TypeMatchers.isType("random"); |
| 102 | + |
| 103 | + private static final Set<String> RANDOM_PROBLEMATIC_FUNCTION_NAMES = Set.of( |
| 104 | + "random", |
| 105 | + "randint", |
| 106 | + "randrange", |
| 107 | + "choice", |
| 108 | + "choices", |
| 109 | + "sample", |
| 110 | + "uniform", |
| 111 | + "gauss", |
| 112 | + "normalvariate", |
| 113 | + "triangular", |
| 114 | + "betavariate", |
| 115 | + "expovariate", |
| 116 | + "gammavariate", |
| 117 | + "lognormvariate", |
| 118 | + "paretovariate", |
| 119 | + "vonmisesvariate", |
| 120 | + "weibullvariate", |
| 121 | + "getrandbits", |
| 122 | + "randbytes"); |
| 123 | + |
| 124 | + @Override |
| 125 | + public void initialize(Context context) { |
| 126 | + context.registerSyntaxNodeConsumer(Tree.Kind.CLASSDEF, DataClassFunctionCallDefaultCheck::checkClassDef); |
| 127 | + } |
| 128 | + |
| 129 | + private static void checkClassDef(SubscriptionContext ctx) { |
| 130 | + ClassDef classDef = (ClassDef) ctx.syntaxNode(); |
| 131 | + if (!isDataclass(classDef, ctx)) { |
| 132 | + return; |
| 133 | + } |
| 134 | + classDef.body().statements().stream() |
| 135 | + .flatMap(TreeUtils.toStreamInstanceOfMapper(AnnotatedAssignment.class)) |
| 136 | + .forEach(annotatedAssignment -> checkField(ctx, annotatedAssignment)); |
| 137 | + } |
| 138 | + |
| 139 | + private static boolean isDataclass(ClassDef classDef, SubscriptionContext ctx) { |
| 140 | + for (Decorator decorator : classDef.decorators()) { |
| 141 | + Expression decoratorExpr = getDecoratorFunctionExpression(decorator); |
| 142 | + if (IS_DATACLASS_DECORATOR.isTrueFor(decoratorExpr, ctx)) { |
| 143 | + return true; |
| 144 | + } |
| 145 | + } |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + private static Expression getDecoratorFunctionExpression(Decorator decorator) { |
| 150 | + Expression expr = decorator.expression(); |
| 151 | + if (expr instanceof CallExpression callExpr) { |
| 152 | + return callExpr.callee(); |
| 153 | + } |
| 154 | + return expr; |
| 155 | + } |
| 156 | + |
| 157 | + private static void checkField(SubscriptionContext ctx, AnnotatedAssignment annotatedAssignment) { |
| 158 | + Expression assignedValue = annotatedAssignment.assignedValue(); |
| 159 | + if (assignedValue == null || isClassVar(annotatedAssignment.annotation(), ctx)) { |
| 160 | + return; |
| 161 | + } |
| 162 | + if (isMutableLiteral(assignedValue)) { |
| 163 | + ctx.addIssue(assignedValue, MESSAGE); |
| 164 | + return; |
| 165 | + } |
| 166 | + if (assignedValue instanceof CallExpression callExpression) { |
| 167 | + Tree problematic = problematicCall(callExpression, ctx); |
| 168 | + if (problematic != null) { |
| 169 | + ctx.addIssue(problematic, MESSAGE); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private static boolean isMutableLiteral(Expression expression) { |
| 175 | + return expression.is(Tree.Kind.LIST_LITERAL, Tree.Kind.DICTIONARY_LITERAL, Tree.Kind.SET_LITERAL); |
| 176 | + } |
| 177 | + |
| 178 | + // Returns the expression to flag, or null. Handles all of: |
| 179 | + // x: T = datetime.now() -> the outer call |
| 180 | + // x: T = field(default=datetime.now()) -> the inner default argument |
| 181 | + // x: T = field(default=[]) -> the inner mutable literal |
| 182 | + private static Tree problematicCall(CallExpression callExpression, SubscriptionContext ctx) { |
| 183 | + if (isProblematicFactoryCall(callExpression, ctx)) { |
| 184 | + return callExpression; |
| 185 | + } |
| 186 | + if (IS_DATACLASSES_FIELD.isTrueFor(callExpression.callee(), ctx)) { |
| 187 | + RegularArgument defaultArg = TreeUtils.argumentByKeyword(DEFAULT_KEYWORD, callExpression.arguments()); |
| 188 | + if (defaultArg != null) { |
| 189 | + Expression defaultExpr = defaultArg.expression(); |
| 190 | + if (isMutableLiteral(defaultExpr)) { |
| 191 | + return defaultExpr; |
| 192 | + } |
| 193 | + if (defaultExpr instanceof CallExpression innerCall && isProblematicFactoryCall(innerCall, ctx)) { |
| 194 | + return innerCall; |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + return null; |
| 199 | + } |
| 200 | + |
| 201 | + private static boolean isProblematicFactoryCall(CallExpression callExpression, SubscriptionContext ctx) { |
| 202 | + Expression callee = callExpression.callee(); |
| 203 | + return IS_PROBLEMATIC_FACTORY.isTrueFor(callee, ctx) || isRandomModuleFunctionCall(callee, ctx); |
| 204 | + } |
| 205 | + |
| 206 | + // Workaround for the random.* alias gap in the V2 typeshed serializer (see NOTE on |
| 207 | + // IS_PROBLEMATIC_FACTORY): the random module type itself is known, so we recognise |
| 208 | + // a problematic call by checking the qualifier's type and the syntactic name. |
| 209 | + private static boolean isRandomModuleFunctionCall(Expression callee, SubscriptionContext ctx) { |
| 210 | + if (!(callee instanceof QualifiedExpression qualifiedExpression)) { |
| 211 | + return false; |
| 212 | + } |
| 213 | + return RANDOM_PROBLEMATIC_FUNCTION_NAMES.contains(qualifiedExpression.name().name()) |
| 214 | + && IS_RANDOM_MODULE.isTrueFor(qualifiedExpression.qualifier(), ctx); |
| 215 | + } |
| 216 | + |
| 217 | + private static boolean isClassVar(TypeAnnotation annotation, SubscriptionContext ctx) { |
| 218 | + Expression annotationExpr = annotation.expression(); |
| 219 | + if (annotationExpr instanceof SubscriptionExpression subscriptionExpr) { |
| 220 | + return IS_CLASS_VAR.isTrueFor(subscriptionExpr.object(), ctx); |
| 221 | + } |
| 222 | + return IS_CLASS_VAR.isTrueFor(annotationExpr, ctx); |
| 223 | + } |
| 224 | +} |
0 commit comments