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-2382 fix unary propagation problem #2198

Merged
merged 2 commits into from
Dec 3, 2024
Merged

SONARPY-2382 fix unary propagation problem #2198

merged 2 commits into from
Dec 3, 2024

Conversation

Seppli11
Copy link
Contributor

@Seppli11 Seppli11 commented Nov 29, 2024

@Seppli11 Seppli11 force-pushed the SONARPY-2382 branch 4 times, most recently from 6d38e69 to 370b97e Compare November 29, 2024 14:04
Copy link
Contributor

@thomas-serre-sonarsource thomas-serre-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM, well done 💪

I left a minor comment on the test

Comment on lines 5 to 6
(~s)(X) # Ok
Copy link
Contributor

Choose a reason for hiding this comment

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

minor I find it not explicit what you want to test here. What matters is not really the fact that s is unknown but that there is a unary ~ operator applied on s right?
I suggest adding a test in nonCallableCalled called def callable_with_unary_operator and checking that not issue are raised when +,-,~... are applied

Copy link
Contributor

Choose a reason for hiding this comment

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

Now that I read the all PR I understand why you called the test function like this. Still, I think it will be clearer for someone who hasn't the context to rewrite this test to be more explicit

Comment on lines +60 to +79
private PythonType calculateUnaryExprType(UnaryExpression unaryExpr) {
String operator = unaryExpr.operator().value();
return TypeUtils.map(unaryExpr.expression().typeV2(), type -> mapUnaryExprType(operator, type));
}

private PythonType mapUnaryExprType(String operator, PythonType type) {
return switch (operator) {
case "~" -> mapInvertExprType(type);
// not cannot be overloaded and always returns a boolean
case "not" -> boolType;
case "+", "-" -> mapUnaryPlusMinusType(type);
default -> PythonType.UNKNOWN;
};
}

private PythonType mapInvertExprType(PythonType type) {
if(isIntTypeCheck.check(type) == TriBool.TRUE || isBooleanTypeCheck.check(type) == TriBool.TRUE) {
return intType;
}
return 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.

clean implementation, well done 💪

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thx 😊


expr.accept(trivialTypePropagationVisitor);
assertThat(expr.typeV2()).isEqualTo(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.

nit add the extra line there

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Personally, I'm impartial whether there is a line there or not. However, I've definitely removed lines between the last function and the closing curly bracket of the class. Is this style documented?

@Seppli11 Seppli11 merged commit 1adc874 into master Dec 3, 2024
10 checks passed
@Seppli11 Seppli11 deleted the SONARPY-2382 branch December 3, 2024 12:54
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