|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 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.plugins.python; |
| 18 | + |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.concurrent.atomic.AtomicLong; |
| 22 | +import org.sonar.plugins.python.api.tree.AliasedName; |
| 23 | +import org.sonar.plugins.python.api.tree.BaseTreeVisitor; |
| 24 | +import org.sonar.plugins.python.api.tree.FileInput; |
| 25 | +import org.sonar.plugins.python.api.tree.ImportFrom; |
| 26 | +import org.sonar.plugins.python.api.tree.ImportName; |
| 27 | +import org.sonar.plugins.python.api.tree.Name; |
| 28 | +import org.sonar.plugins.python.api.types.v2.PythonType; |
| 29 | +import org.sonar.plugins.python.api.types.v2.UnknownType; |
| 30 | +import org.sonar.python.semantic.v2.SymbolV2; |
| 31 | +import org.sonar.python.semantic.v2.UsageV2; |
| 32 | + |
| 33 | +/** |
| 34 | + * Collects telemetry metrics about type inference quality. |
| 35 | + * Must be called after type inference has run on each file. |
| 36 | + */ |
| 37 | +public class TypeInferenceTelemetryCollector { |
| 38 | + |
| 39 | + private final AtomicLong totalNames = new AtomicLong(0); |
| 40 | + private final AtomicLong unknownTypeNames = new AtomicLong(0); |
| 41 | + private final AtomicLong unresolvedImportTypeNames = new AtomicLong(0); |
| 42 | + private final AtomicLong totalImports = new AtomicLong(0); |
| 43 | + private final AtomicLong importsWithUnknownType = new AtomicLong(0); |
| 44 | + private final AtomicLong uniqueSymbols = new AtomicLong(0); |
| 45 | + private final AtomicLong unknownSymbols = new AtomicLong(0); |
| 46 | + |
| 47 | + public void collect(FileInput rootTree) { |
| 48 | + var visitor = new CollectorVisitor(); |
| 49 | + rootTree.accept(visitor); |
| 50 | + aggregateMetrics(visitor); |
| 51 | + } |
| 52 | + |
| 53 | + private synchronized void aggregateMetrics(CollectorVisitor visitor) { |
| 54 | + totalNames.addAndGet(visitor.totalNames); |
| 55 | + unknownTypeNames.addAndGet(visitor.unknownTypeNames); |
| 56 | + unresolvedImportTypeNames.addAndGet(visitor.unresolvedImportTypeNames); |
| 57 | + totalImports.addAndGet(visitor.totalImports); |
| 58 | + importsWithUnknownType.addAndGet(visitor.importsWithUnknownType); |
| 59 | + uniqueSymbols.addAndGet(visitor.uniqueSymbols.size()); |
| 60 | + unknownSymbols.addAndGet(visitor.unknownSymbols.size()); |
| 61 | + } |
| 62 | + |
| 63 | + public TypeInferenceTelemetry getTelemetry() { |
| 64 | + return new TypeInferenceTelemetry( |
| 65 | + totalNames.get(), |
| 66 | + unknownTypeNames.get(), |
| 67 | + unresolvedImportTypeNames.get(), |
| 68 | + totalImports.get(), |
| 69 | + importsWithUnknownType.get(), |
| 70 | + uniqueSymbols.get(), |
| 71 | + unknownSymbols.get() |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + private static class CollectorVisitor extends BaseTreeVisitor { |
| 76 | + long totalNames = 0; |
| 77 | + long unknownTypeNames = 0; |
| 78 | + long unresolvedImportTypeNames = 0; |
| 79 | + long totalImports = 0; |
| 80 | + long importsWithUnknownType = 0; |
| 81 | + Set<SymbolV2> uniqueSymbols = new HashSet<>(); |
| 82 | + Set<SymbolV2> unknownSymbols = new HashSet<>(); |
| 83 | + |
| 84 | + @Override |
| 85 | + public void visitName(Name name) { |
| 86 | + totalNames++; |
| 87 | + |
| 88 | + PythonType type = name.typeV2(); |
| 89 | + if (type == PythonType.UNKNOWN) { |
| 90 | + unknownTypeNames++; |
| 91 | + } else if (type instanceof UnknownType.UnresolvedImportType) { |
| 92 | + unresolvedImportTypeNames++; |
| 93 | + } |
| 94 | + |
| 95 | + SymbolV2 symbol = name.symbolV2(); |
| 96 | + if (symbol != null) { |
| 97 | + uniqueSymbols.add(symbol); |
| 98 | + if (isUnknownSymbol(symbol)) { |
| 99 | + unknownSymbols.add(symbol); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + super.visitName(name); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void visitImportName(ImportName importName) { |
| 108 | + for (AliasedName aliasedName : importName.modules()) { |
| 109 | + totalImports++; |
| 110 | + if (hasUnknownImportType(aliasedName)) { |
| 111 | + importsWithUnknownType++; |
| 112 | + } |
| 113 | + } |
| 114 | + super.visitImportName(importName); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void visitImportFrom(ImportFrom importFrom) { |
| 119 | + for (AliasedName aliasedName : importFrom.importedNames()) { |
| 120 | + totalImports++; |
| 121 | + if (hasUnknownImportType(aliasedName)) { |
| 122 | + importsWithUnknownType++; |
| 123 | + } |
| 124 | + } |
| 125 | + super.visitImportFrom(importFrom); |
| 126 | + } |
| 127 | + |
| 128 | + private static boolean hasUnknownImportType(AliasedName aliasedName) { |
| 129 | + Name nameToCheck = aliasedName.alias(); |
| 130 | + if (nameToCheck == null) { |
| 131 | + var names = aliasedName.dottedName().names(); |
| 132 | + if (!names.isEmpty()) { |
| 133 | + nameToCheck = names.get(names.size() - 1); |
| 134 | + } |
| 135 | + } |
| 136 | + if (nameToCheck == null) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + return isUnknownType(nameToCheck.typeV2()); |
| 140 | + } |
| 141 | + |
| 142 | + private static boolean isUnknownType(PythonType type) { |
| 143 | + return type == PythonType.UNKNOWN || type instanceof UnknownType.UnresolvedImportType; |
| 144 | + } |
| 145 | + |
| 146 | + private static boolean isUnknownSymbol(SymbolV2 symbol) { |
| 147 | + // A symbol is considered unknown if all its binding usages have unknown types |
| 148 | + return symbol.usages().stream() |
| 149 | + .filter(UsageV2::isBindingUsage) |
| 150 | + .allMatch(usage -> { |
| 151 | + if (usage.tree() instanceof Name name) { |
| 152 | + return isUnknownType(name.typeV2()); |
| 153 | + } |
| 154 | + return true; |
| 155 | + }); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
0 commit comments