-
Notifications
You must be signed in to change notification settings - Fork 93
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-2242 TypeInferenceV2 should resolve stubs with variable descriptors pointing to submodules #2094
Conversation
7a4d942
to
a3760b4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good! I like the fact that it's a simple solution.
I left some questions and suggestions, but this is just for the sake of intentionality and clarity.
@@ -253,4 +253,14 @@ class lib: ... | |||
// SONARPY-2176 lib should be resolved as the renamed class "A" here | |||
assertThat(aType.name()).isEqualTo("A"); | |||
} | |||
|
|||
@Test | |||
void resolveCustomStub() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would make sense to reflect that it's about importedModule
and the conflict between module members and submodules. The fact it happens in a custom stub is not really what's relevant here.
I'd suggest naming to resolveCustomStubWithImportedSubmodule
or something similar to express that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Note: I took the liberty to rename the ticket/PR title consistently with this, however feel free to further update it)
if (from.isImportedModule()) { | ||
var fqn = from.fullyQualifiedName(); | ||
if (fqn != null) { | ||
return ctx.lazyTypesContext().getOrCreateLazyType(fqn); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Genuine question: what did you decide to do about our discussion for LazyModuleType
? Handle it in another ticket? Scrap it?
Not challenging to choice to keep things simple here, though. I think it makes sense (even though it's a shame we lose some information in the process).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried the implementation with LazyModuleType
, and it opened up some problematic discussions.
One example is if there is a LazyType
and a ModuleLazyType
with the same imported path, what happens ?
It also didn't help in the code, and the change was bigger for no real benefit (I didn't do profiling)
@@ -67,10 +67,11 @@ public PythonType getType(List<String> typeFqnParts) { | |||
if (parent instanceof ModuleType moduleType) { | |||
TypeWrapper typeWrapper = moduleType.members().get(part); | |||
if (typeWrapper instanceof LazyTypeWrapper lazyTypeWrapper && !lazyTypeWrapper.isResolved()) { | |||
if (i == typeFqnParts.size() - 1) { | |||
if (i == typeFqnParts.size() - 1 && !(lazyTypeWrapper.hasImportPath(String.join(".", typeFqnParts)))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Acknowledging the confusion when looked at this together, I'm thinking maybe we should extract this to another method, or at least add a tiny bit more info on the purpose of this in a comment (maybe updating the one on line 71).
@@ -29,6 +29,12 @@ | |||
public class VariableDescriptorToPythonTypeConverter implements DescriptorToPythonTypeConverter { | |||
|
|||
public PythonType convert(ConversionContext ctx, VariableDescriptor from) { | |||
if (from.isImportedModule()) { | |||
var fqn = from.fullyQualifiedName(); | |||
if (fqn != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can do a mock to cover it, but I am not sure of the value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
I had in mind something a bit more detailed for the explanation and the method extraction, but I would be okay to merge as-is for the sake of not blocking this and other tickets depending on this.
@@ -67,8 +67,8 @@ public PythonType getType(List<String> typeFqnParts) { | |||
if (parent instanceof ModuleType moduleType) { | |||
TypeWrapper typeWrapper = moduleType.members().get(part); | |||
if (typeWrapper instanceof LazyTypeWrapper lazyTypeWrapper && !lazyTypeWrapper.isResolved()) { | |||
if (i == typeFqnParts.size() - 1 && !(lazyTypeWrapper.hasImportPath(String.join(".", typeFqnParts)))) { | |||
// this is the name we are looking for, resolve it | |||
if (i == typeFqnParts.size() - 1 && !fqnSameAsImportedPath(typeFqnParts, lazyTypeWrapper)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must admit, this is not really what I had in mind, fqnSameAsImportedPath
and lazyTypeWrapper.hasImportPath(String.join(".", typeFqnParts))
read very similar and don't provide the extra clarity I expected.
What I had in mind was extracting the full condition (both parts of the &&) and expanding a bit more on the cases that can lead us to this condition. Basically explaining that we try to resolve a lazy type only if it's pointing us to a different module, and if it's going to point us on the same module again, we instead try to resolve a submodule of the same name instead of the member.
(Sorry, I realize my suggestion is a bit unclear, and this is probably why we need one in the first place)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for the updated comment! I think it makes things clearer.
6b04ff3
to
c5abe65
Compare
Quality Gate passedIssues Measures |
SONARPY-2242