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-2229 Fix FP on S2201 when instantiating the result of a type call #2088

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

guillaume-dequenne-sonarsource
Copy link
Contributor

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

This is admittedly a very naive approach to fix this FP.

However, I believe that this deserves a broader discussion, considering SONARPY-2013 to allow for easier manipulation of type predicates.
I also considered adding a proper PythonType#kind API to avoid checking for instances. However, I feel this can still be done later if the need proves real.

@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource force-pushed the SONARPY-2229 branch 3 times, most recently from 8e4545c to cb92bfb Compare October 22, 2024 13:09
@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource changed the title SONARPY-2229 Fix FP on S2201 when instantiating the result of a type … SONARPY-2229 Fix FP on S2201 when instantiating the result of a type call Oct 22, 2024
Copy link
Contributor

@Seppli11 Seppli11 left a comment

Choose a reason for hiding this comment

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

According to our discussion, I think it would make sense to change the type checks in TypeCheckBuilder to either check if a type is an instance of a or is a.
Since this PR is using isTypeWithName introducing this change for this method would make sense.

Maybe it also would make sense to align the behaviour of isInstanceOf, so that checking with isTypeWithName checks for the type and isInstanceOf if it is an instance of a type. Having (somewhat) uniform behaviour for the type checkers when it comes to union type would make the API easier to use. But this should probably be its separate PR.

.filter(f -> typeChecker.typeCheckBuilder().isTypeWithName(f).check(pythonType).equals(TriBool.TRUE))
.findFirst()
.ifPresent(result -> ctx.addIssue(callExpression.callee(), String.format(MESSAGE_FORMAT, result)));
if (typeChecker.typeCheckBuilder().isObjectType().check(pythonType) == TriBool.FALSE) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think extracting this condition into its own method would help readability as it can be named

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 removed the condition altogether)

@guillaume-dequenne-sonarsource
Copy link
Contributor Author

According to our discussion, I think it would make sense to change the type checks in TypeCheckBuilder to either check if a type is an instance of a or is a. Since this PR is using isTypeWithName introducing this change for this method would make sense.

Maybe it also would make sense to align the behaviour of isInstanceOf, so that checking with isTypeWithName checks for the type and isInstanceOf if it is an instance of a type. Having (somewhat) uniform behaviour for the type checkers when it comes to union type would make the API easier to use. But this should probably be its separate PR.

Following our discussion, I instead added the isStrict option to the isSameTypeAsPredicate. I feel that we should indeed have a more global approach to dealing with "in an instance of" vs "is the exact type", but I feel it should probably be part of a more global refactoring (we'll have to clarify the public type checking API we want to expose eventually, I think it can be part of that).

Copy link
Contributor

@Seppli11 Seppli11 left a comment

Choose a reason for hiding this comment

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

lgtm

@Seppli11 Seppli11 merged commit e38877f into master Oct 23, 2024
11 checks passed
@Seppli11 Seppli11 deleted the SONARPY-2229 branch October 23, 2024 12:49
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