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-2245 Fix resolving type for a name if its type is UnresolvedImportType #2099

Merged
merged 3 commits into from
Oct 25, 2024

Conversation

maksim-grebeniuk-sonarsource
Copy link
Contributor

@maksim-grebeniuk-sonarsource maksim-grebeniuk-sonarsource commented Oct 23, 2024

Copy link
Contributor

Choose a reason for hiding this comment

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

Left some comments.
I would expect the // FAILS comment to be removed, and am tempted to say we should extract the custom logic in a dedicated method. I leave @ghislainpiot with the decision to approve as-is or not, though.

var arg = ((RegularArgument) ((CallExpression) funcCall).arguments().get(0));
var argType = arg.expression().typeV2();

assertThat(argType).isInstanceOfSatisfying(UnresolvedImportType.class, a -> assertThat(a.importPath()).isEqualTo("a.b")); // FAILS

Choose a reason for hiding this comment

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

You copy pasted the test case in the ticket, but forgot to remove this FAILS comment.

@@ -471,7 +471,7 @@ public void visitName(Name name) {
.map(Expression.class::cast)
.map(Expression::typeV2)
// TODO: classes (SONARPY-1829) and functions should be propagated like other types
.filter(t -> (t instanceof ClassType) || (t instanceof FunctionType) || (t instanceof ModuleType))
.filter(t -> (t instanceof ClassType) || (t instanceof FunctionType) || (t instanceof ModuleType) || (t instanceof UnknownType.UnresolvedImportType))

Choose a reason for hiding this comment

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

I'm wondering if we should extract this to a dedicated method that could be named something like shouldTypeBeEagerlyPropagated, to make this heuristic more intentional.

I have the feeling that we'll never fully remove this heuristic (even as we fix SONARPY-1829), because of the reasons mentioned in SONARPY-2245 (basically, we sometimes want to circumvent the actual Python propagations to ensure classes, functions and imports referenced in inner scopes are correctly resolved, even if they could technically be shadowed)

Copy link
Contributor

@ghislainpiot ghislainpiot left a comment

Choose a reason for hiding this comment

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

I think putting the filter condition in a method would make sense, it would help understanding the code in my opinion.

Copy link
Contributor

@ghislainpiot ghislainpiot left a comment

Choose a reason for hiding this comment

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

LGTM!

@ghislainpiot ghislainpiot merged commit 59c4664 into master Oct 25, 2024
12 checks passed
@ghislainpiot ghislainpiot deleted the SONARPY-2245 branch October 25, 2024 12:24
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