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-2209 Avoid duplicating re-exported stub symbols when deserializing stubs #2078

Merged
merged 4 commits into from
Oct 22, 2024

Conversation

guillaume-dequenne-sonarsource
Copy link
Contributor

No description provided.

@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource force-pushed the SONARPY-2209 branch 2 times, most recently from 7933a5b to 6a01a09 Compare October 16, 2024 12:54
@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource changed the title SONARPY-2209 tmp SONARPY-2209 Avoid duplicating re-exported stub symbols when deserializing stubs Oct 16, 2024
Base automatically changed from SONARPY-2203 to master October 17, 2024 08:32
@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource force-pushed the SONARPY-2209 branch 8 times, most recently from 56f9eab to 56b5ace Compare October 17, 2024 15:02
var descriptor = entry.getValue();
var name = entry.getKey();
PythonType result;
String fullyQualifiedName = descriptor.fullyQualifiedName();
Copy link
Contributor

Choose a reason for hiding this comment

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

If I understand correctly, this is the fullname from Mypy

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, which represents the actual fully qualified name of the symbol (where it's defined, rather than where it's re-exposed).
Do you think it could be better named?

Copy link
Contributor

Choose a reason for hiding this comment

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

It was just to be sure, I think it is strange that we didn't keep the name of the filed from Mypy

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 this is a good start towards improving resolution, there are still quite a few edge cases from what is in the tests

if (parent instanceof ObjectType) {
return PythonType.UNKNOWN;
}
if (parent instanceof ModuleType moduleType) {
TypeWrapper typeWrapper = moduleType.members().get(part);
if (typeWrapper instanceof LazyTypeWrapper lazyTypeWrapper && !lazyTypeWrapper.isResolved()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Manually checking if a LazyType is not resolved makes it feel like a bandaid to me. I think properly representing submodules would be better, but perhaps outside the scope of the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. I've added 13d2386 which adds a bit more intentionality, although the "LazyType is not resolved" heuristics is still used.

Ultimately, I think SONARPY-2176 should model a bit better the cases where a submodule or a member should be looked at in priority. I do think that the "LazyType is not resolved" is a case where resolving the submodule first is necessary (because the LazyType is apparently pointing to it - not resolving it would lead to a stack overflow), and we only tackle this one due to the crashes this avoids.

Comment on lines +85 to +67
public Optional<PythonType> resolveSubmodule(String submoduleName) {
return Optional.ofNullable(subModules.get(submoduleName)).map(TypeWrapper::type);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it expected that subModules is only used in test code? Not counting adding and retrieving from the parents, it never leaves the module types.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good point. I've added 13d2386 to instead consistently register submodules in the subModule field.
I've updated the strategy of resolveMember to first resolve actual members, then sub modules if no member is present. The behavior is not entirely accurate depending on the context, but I believe it is a decent enough approximation before we tackle SONARPY-2176.

@@ -39,12 +40,43 @@ void resolveMemberTest() {
@Test
void replaceMemberIfUnknown() {
var a = new ModuleType("a");
a.members().put("b", PythonType.UNKNOWN);
a.members().put("b", TypeWrapper.of(PythonType.UNKNOWN));
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: We have TypeWrapper.UNKNOWN_TYPE_WRAPPER

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.

Looks good to me now.

@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource marked this pull request as ready for review October 21, 2024 14:07
@@ -30,23 +30,23 @@
public final class ModuleType implements PythonType {
private final String name;
private final ModuleType parent;
private final Map<String, PythonType> members;
private final Map<String, TypeWrapper> members;
private final Map<String, TypeWrapper> subModules = new HashMap<>();

Choose a reason for hiding this comment

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

I'd suggest to assign the field value in the same place where other fields are initialized, to have single place to look at

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM

@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource merged commit 97b164c into master Oct 22, 2024
8 of 10 checks passed
@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource deleted the SONARPY-2209 branch October 22, 2024 10:32
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