Skip to content

Commit 85af5de

Browse files
Fix bug when copying ambiguous aliased symbol (#703)
1 parent 86dd550 commit 85af5de

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

python-frontend/src/main/java/org/sonar/python/semantic/Scope.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private static Symbol copySymbol(String symbolName, Symbol symbol, Map<String, S
149149
return classSymbol;
150150
} else if (symbol.is(Symbol.Kind.AMBIGUOUS)) {
151151
Set<Symbol> alternativeSymbols = ((AmbiguousSymbol) symbol).alternatives().stream()
152-
.map(s -> copySymbol(s.name(), s, globalSymbolsByFQN))
152+
.map(s -> copySymbol(symbolName, s, globalSymbolsByFQN))
153153
.collect(Collectors.toSet());
154154
return AmbiguousSymbolImpl.create(alternativeSymbols);
155155
} else if (symbol.is(Symbol.Kind.OTHER)) {

python-frontend/src/test/java/org/sonar/python/semantic/AmbiguousSymbolTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,19 @@ public void ambiguous_symbol_creation_different_fqn() {
149149
assertThat(ambiguousSymbol.alternatives()).containsExactlyInAnyOrder(foo, otherFoo);
150150
}
151151

152+
@Test
153+
public void aliased_import() {
154+
Symbol symbol = symbols(
155+
"try:",
156+
" from gettext import gettext as _",
157+
"except ImportError:",
158+
" def _(s): return s"
159+
).get("_");
160+
assertThat(symbol.name()).isEqualTo("_");
161+
assertThat(symbol.is(Symbol.Kind.AMBIGUOUS)).isTrue();
162+
assertThat(((AmbiguousSymbol) symbol).alternatives()).extracting(Symbol::kind).containsExactlyInAnyOrder(Symbol.Kind.OTHER, Symbol.Kind.FUNCTION);
163+
}
164+
152165
@Test
153166
public void copy_without_usages() {
154167
SymbolImpl foo = new SymbolImpl("foo", "mod1.foo");

0 commit comments

Comments
 (0)