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-2288 Fix FP on S5795 when checking if type is unsuitable #2116

Merged
merged 3 commits into from
Oct 30, 2024

Conversation

Seppli11
Copy link
Contributor

@Seppli11 Seppli11 commented Oct 30, 2024

SONARPY-2288

Part of

Copy link
Contributor

Choose a reason for hiding this comment

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

The change makes sense, and the underlying problem highlights still some weaknesses in our APIs.

I think it would be nice to move the custom logic to the type checker so that it can be later rethought in context, without needing to consider custom logic implemented in specific rules.

@@ -147,7 +148,7 @@ private boolean isUnsuitableOperand(Expression expr) {
private boolean isUnsuitableType(PythonType type) {
for (String builtinName : NAMES_OF_TYPES_UNSUITABLE_FOR_COMPARISON) {
TypeCheckBuilder builtinWithNameChecker = typeChecker.typeCheckBuilder().isBuiltinWithName(builtinName);
if (builtinWithNameChecker.check(type) == TriBool.TRUE) {
if (type instanceof ObjectType && builtinWithNameChecker.check(type) == TriBool.TRUE) {

Choose a reason for hiding this comment

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

I feel that we shouldn't mix checks done by the type checker and by the rule itself.
In this case, you want to ensure the object is actually an object of a specific type.

The proper fix (that you can consider in the type checking API sprint), is in my opinion the possibility to check something like isObjectOfOf(PythonType expected) that checks that we have an object of a certain type, rather than the type itself. I feel it's something that will come up enough that it will deserve its own method to create the relevant predicates.

As for the immediate fix, I think you could introduce a isObject method in the type checker that perform the same check. Then, thanks to the natural ANDing of predicates, you'll be able to write:

TypeCheckBuilder isObjectOfBuiltinCheck = typeChecker.typeCheckBuilder().isObject().isBuiltinWithName(builtinName);

And simply check that for TRUE.

@@ -156,3 +156,7 @@ def comparison_to_none():
def potential_null_symbols():
type(arr) is tuple
some_thing is other_thing

def unknown_is_not_type(arg):

Choose a reason for hiding this comment

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

Does that method name make sense?
I guess the underlying problem is to not confuse a type object with an object of a given type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The name does not make sense. You're absolutely right with the reason why the FP is happening.

Copy link
Contributor

Choose a reason for hiding this comment

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

We need an extra unit test.

record IsInstancePredicate() implements TypePredicate {

@Override
public TriBool test(PythonType pythonType) {

Choose a reason for hiding this comment

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

Because this is now in the frontend (and not checks), we need a dedicated unit test for this method.

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 01f6048 into master Oct 30, 2024
11 checks passed
@guillaume-dequenne-sonarsource guillaume-dequenne-sonarsource deleted the SONARPY-2288 branch October 30, 2024 16:35
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