Skip to content

Commit d3504b5

Browse files
SONARPY-960 Typeshed serializer: resolve type of alias variables to overloaded symbols
1 parent be9649f commit d3504b5

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

python-frontend/src/test/java/org/sonar/python/types/TypeShedTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,15 @@ public void typeshed_private_modules_should_not_affect_fqn() {
527527
assertThat(socket.superClasses()).extracting(Symbol::fullyQualifiedName).containsExactly("object");
528528
}
529529

530+
@Test
531+
public void overloaded_function_alias_has_function_annotated_type() {
532+
Map<String, Symbol> gettextModule = symbolsForModule("gettext");
533+
Symbol translation = gettextModule.get("translation");
534+
Symbol catalog = gettextModule.get("Catalog");
535+
assertThat(translation.kind()).isEqualTo(Kind.AMBIGUOUS);
536+
assertThat(catalog.annotatedTypeName()).isEqualTo("function");
537+
}
538+
530539
@Test
531540
public void stubFilesSymbols_third_party_symbols_should_not_be_null() {
532541
// six modules contain ambiguous symbols that only contain class symbols

python-frontend/typeshed_serializer/serializer/symbols.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ def __init__(self, _type: mpt.Type):
135135
self.kind = TypeKind.TYPED_DICT
136136
# TODO: check in items for key/type mapping
137137
self.pretty_printed_name = "TypedDict"
138+
elif isinstance(_type, mpt.Overloaded):
139+
self.kind = TypeKind.CALLABLE
140+
fallback = TypeDescriptor(_type.fallback)
141+
self.fully_qualified_name = fallback.fully_qualified_name
142+
self.args.append(fallback)
143+
self.pretty_printed_name = f"CallableType[{fallback.pretty_printed_name}]"
138144
else:
139145
# this can happen when there is a var symbol assigned to an overload symbol
140146
self.is_unknown = True

python-frontend/typeshed_serializer/tests/test_symbols_merger.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from unittest.mock import Mock
2222

2323
from serializer import symbols, symbols_merger
24-
from serializer.symbols import MergedModuleSymbol
24+
from serializer.symbols import MergedModuleSymbol, TypeKind
2525

2626

2727
def test_build_multiple_python_version(typeshed_stdlib):
@@ -235,8 +235,9 @@ def test_actual_module_merge(fake_module_36_38):
235235
alias = all_vars['fakemodule.alias']
236236
assert len(alias) == 1
237237
alias_symbol = alias[0].var_symbol
238-
assert alias_symbol.type.fully_qualified_name is None
239-
assert alias_symbol.type.is_unknown is True
238+
assert alias_symbol.type.fully_qualified_name == "builtins.function"
239+
assert alias_symbol.type.kind == TypeKind.CALLABLE
240+
assert alias_symbol.type.pretty_printed_name == "CallableType[builtins.function]"
240241

241242
imported_sys = all_vars['sys']
242243
assert len(imported_sys) == 1

0 commit comments

Comments
 (0)