Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONARPY-2310: Partly support wildcard import #2167

Merged
merged 4 commits into from
Nov 21, 2024
Merged

SONARPY-2310: Partly support wildcard import #2167

merged 4 commits into from
Nov 21, 2024

Conversation

joke1196
Copy link
Contributor

@joke1196 joke1196 commented Nov 19, 2024

@@ -61,10 +62,15 @@ public TypeInferenceV2(TypeTable projectLevelTypeTable, PythonFile pythonFile, S
}

public Map<SymbolV2, Set<PythonType>> inferTypes(FileInput fileInput) {
TrivialTypeInferenceVisitor trivialTypeInferenceVisitor = new TrivialTypeInferenceVisitor(projectLevelTypeTable, pythonFile, fullyQualifiedModuleName);
WildcardImportVisitor wildcardImportVisitor = new WildcardImportVisitor(projectLevelTypeTable);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this need to be its dedicated visitor?
I know we're going for a naive implementation, but I think it's still sensible to assume that we don't want the following resolution:

foo()
from lib import * # contains foo

I'm under the impression that relying on the main visit of TrivialTypeInferenceVisitor should be enough here, no?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The TrivialTypeInferenceVisistor is enough (initially that's what we did). Our thought was that if it is moved to its own visitor, it is easier to rip it out when we eventually will do that. But we didn't think of the example you provided

@Seppli11 Seppli11 force-pushed the SONARPY-2310 branch 5 times, most recently from ecf4705 to 82abc6d Compare November 20, 2024 15:53
@Seppli11 Seppli11 changed the title SONARPY-2310: Support wildcard import SONARPY-2310: Partly support wildcard import Nov 20, 2024
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kudos on the clear tests (well documenting the limitations) and the clear separation of commits (even if I didn't notice it initially, sorry about that!).
LGTM.

Comment on lines +3117 to +3118
assertThat(fooExpr.typeV2()).isInstanceOf(FunctionType.class);
assertThat(importedFooExpr.typeV2()).isInstanceOf(FunctionType.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add an assertion checking whether these are the same objects?

import mod2
(foo, mod1.foo, mod2.foo)
"""),
// foo should be mod2.foo, not mod1.foo. Since wildcard imports are only assigned to names without a symbol, foo will be equal to mod1.foo. See SONARPY-2357

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good! I think this is a reasonable behavior for now.
As a nitpick, I'd rename the Jira ticket to something a bit more explicit/neutral (Something like: Support edge cases of wildcard imports resolution?)

@@ -3124,4 +3262,25 @@ private static Statement lastStatement(StatementList statementList) {
List<Statement> statements = statementList.statements();
return statements.get(statements.size() - 1);
}

private static class TestProject {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a pretty neat construct!
I'm wondering if it wouldn't make more sense for this to be in a different test category though (either ProjectLevelSymbolTableTest or ProjectLevelTypeTableTest, or even a new CrossFileTypeInferenceTest dedicated to tests focused on import resolution.
We probably can keep this here for now though.

@Seppli11 Seppli11 merged commit 20697b0 into master Nov 21, 2024
12 checks passed
@Seppli11 Seppli11 deleted the SONARPY-2310 branch November 21, 2024 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants