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

[DO NOT MERGE] SONARPY-2295 Support relative imports in type inference V2 #2120

Closed
wants to merge 11 commits into from

Conversation

guillaume-dequenne-sonarsource
Copy link
Contributor

@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource commented Oct 31, 2024

@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource changed the base branch from master to MMF-3945 October 31, 2024 13:33
@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource force-pushed the SONARPY-2295 branch 7 times, most recently from 9b74c04 to 3387928 Compare October 31, 2024 16:12
@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource changed the title SONARPY-2295 SONARPY-2295 Support relative imports in type inference V2 Oct 31, 2024
@@ -96,13 +98,15 @@ public class TrivialTypeInferenceVisitor extends BaseTreeVisitor {

private final TypeTable projectLevelTypeTable;
private final String fileId;
private final String fullyQualifiedModuleName;

Choose a reason for hiding this comment

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

We could store it as a list of strings

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I actually use the string in my other PR to introduce FQNs for FunctionType, so I'd rather keep the string here and split it when needed in the import resolution.

.ifPresent(fqn -> setTypeToImportFromStatement(importFrom, fqn));
.orElse(new ArrayList<>());

List<String> moduleFqnElements = new ArrayList<>(Arrays.stream(fullyQualifiedModuleName.split("\\.")).toList());

Choose a reason for hiding this comment

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

then this operation makes no sense, but if you prefer to store module name as a single string then this expression could be simpler:

var moduleFqnElements =  List.of(fullyQualifiedModuleName.split("\\."))

Choose a reason for hiding this comment

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

also, does it make sense to define this variable outside of the if condition?

Choose a reason for hiding this comment

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

also if you'll get rid of dotPrefixTokens variable and simply assign the fromModuleFqn - it will allow to get rid of the else part

    var fromModuleFqn = Optional.ofNullable(importFrom.module())
      .map(TrivialTypeInferenceVisitor::dottedNameToPartFqn)
      .orElse(new ArrayList<>());
    var dotPrefixTokens = importFrom.dottedPrefixForModule();
    if (!dotPrefixTokens.isEmpty()) {
      var moduleFqnElements = List.of(fullyQualifiedModuleName.split("\\."));
      // Relative import: we start from the current module FQN and go up as many levels as there are dots in the import statement
      int sizeLimit = Math.max(0, moduleFqnElements.size() - dotPrefixTokens.size());
      fromModuleFqn = Stream.concat(moduleFqnElements.stream().limit(sizeLimit), fromModuleFqn.stream()).toList();
    }
    setTypeToImportFromStatement(importFrom, fromModuleFqn);

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, thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good, just a couple of small changes could be applied

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM

Copy link

sonarqube-next bot commented Nov 1, 2024

Quality Gate failed Quality Gate failed

Failed conditions
1 New issue

See analysis details on SonarQube

Catch issues before they fail your Quality Gate with our IDE extension SonarLint SonarLint

@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource changed the title SONARPY-2295 Support relative imports in type inference V2 [DO NOT MERGE] SONARPY-2295 Support relative imports in type inference V2 Nov 1, 2024
@guillaume-dequenne-sonarsource
Copy link
Contributor Author

Note: I re-created #2122 to target master instead of the feature branch.

@guillaume-dequenne-sonarsource
Copy link
Contributor Author

Duplicate.

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.

2 participants