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-2202: Migrate S5727 ComparisonToNoneCheck to the new type model #2085

Closed
wants to merge 5 commits into from

Conversation

Seppli11
Copy link
Contributor

No description provided.


@Override
public TriBool test(PythonType otherMaybeWrappedType) {
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'm not entirely sure that the behaviour of this method and InferredType.isIdentityComparableWith is the same.

My understanding of this check is that it checks if two types are the same, or, if one is a union, one of its candidates is the same as the other type. Additionally, if one of the two types is unknown, the result is always TriBool.UNKNOWN.

@Seppli11 Seppli11 force-pushed the SONARPY-2202 branch 3 times, most recently from 4e9ea82 to 49c3d6c Compare October 18, 2024 14:18
@Seppli11 Seppli11 marked this pull request as ready for review October 18, 2024 14:20
@Seppli11 Seppli11 force-pushed the SONARPY-2202 branch 4 times, most recently from 1d7a71b to 601d05d Compare October 21, 2024 12:35
@Seppli11 Seppli11 marked this pull request as draft October 22, 2024 07:12
@Seppli11 Seppli11 force-pushed the SONARPY-2202 branch 5 times, most recently from f2e4d76 to 8bcf3f3 Compare October 23, 2024 07:57
Copy link
Contributor Author

Choose a reason for hiding this comment

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

was moved to types/v2

@Seppli11 Seppli11 force-pushed the SONARPY-2202 branch 5 times, most recently from bc18ff4 to 494711d Compare October 23, 2024 09:31
return Set.of(type);
}

public static PythonType unwrapType(PythonType 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.

I think a decent argument can be made that UnionType#unwrappedType() should have this behavior. I'm not sure if there are unwanted side-effects from this and aired on the side of caution,

@Seppli11 Seppli11 marked this pull request as ready for review October 23, 2024 09:40
@Seppli11 Seppli11 requested a review from ghislainpiot October 23, 2024 09:46
@Seppli11 Seppli11 force-pushed the SONARPY-2202 branch 2 times, most recently from c77f94e to 35eb809 Compare October 23, 2024 12:36
The disabled test dependent on SONARPY-2241
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.

The utility methods seem to be handling a lot of edge cases that should never happen in the first place.
I think it would be good to create tickets to investigate (and eliminate) the creation of those impossible types.

I didn't review the TypeChecker changes as discussed, I don't think handling the identify case is worth the added complexity on the main predicate.

* @return a set of all possible effective types. However, {@link ObjectType}s are not unwrapped, as they are used to represent an
* instance of a type, rather than a type itself, and this should be done explicitly.
*/
public static Set<PythonType> getNestedEffectiveTypes(PythonType type) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure about the naming of the method, maybe something like flattenedTypes would be clearer ?

Comment on lines +70 to +82
var nestedUnions = UnionType.or(
intObjType,
UnionType.or(
floatObjType,
NONE_TYPE
)
);

assertThat(TypeUtils.unwrapType(nestedUnions))
.isEqualTo(
UnionType.or(INT_TYPE, UnionType.or(FLOAT_TYPE, NONE_TYPE))
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

UnionTypes cannot be nested, and the variable does not have any nesting.

assertThat(TypeUtils.getNestedEffectiveTypes(INT_TYPE)).containsExactlyInAnyOrder(INT_TYPE);

assertThat(TypeUtils.getNestedEffectiveTypes(UnionType.or(INT_TYPE, FLOAT_TYPE))).containsExactlyInAnyOrder(INT_TYPE, FLOAT_TYPE);
assertThat(TypeUtils.getNestedEffectiveTypes(new UnionType(Set.of(INT_TYPE, UnionType.or(FLOAT_TYPE, BOOL_TYPE))))).containsExactlyInAnyOrder(INT_TYPE, FLOAT_TYPE, BOOL_TYPE);
Copy link
Contributor

Choose a reason for hiding this comment

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

This is also an UnionType that should not be able to exist

assertThat(TypeUtils.getNestedEffectiveTypes(
new UnionType(Set.of(
PROJECT_LEVEL_TYPE_TABLE.lazyTypesContext().getOrCreateLazyType("int"),
new LazyUnionType(Set.of(FLOAT_TYPE, BOOL_TYPE))
Copy link
Contributor

Choose a reason for hiding this comment

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

This also probably shouldn't exist, the creation of the UnionType should resolve its candidates

var floatObjType = new ObjectType(FLOAT_TYPE);
assertThat(TypeUtils.unwrapType(INT_TYPE)).isEqualTo(INT_TYPE);
assertThat(TypeUtils.unwrapType(intObjType)).isEqualTo(INT_TYPE);
assertThat(TypeUtils.unwrapType(new ObjectType(new ObjectType(INT_TYPE)))).isEqualTo(INT_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 there an example of this happening ? If yes, we should investigate if that is correct

if (type2 instanceof ResolvableType resolvableType) {
type2 = resolvableType.resolve();
}
if (type2 instanceof ObjectType objectType && typeStrictness == TypeStrictness.NON_STRICT) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this the same as SONARPY-2229 ?

}

private boolean isNone(PythonType type) {
return isNoneTypeCheck.check(TypeUtils.unwrapType(type)) == TriBool.TRUE;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the unwrapType needed ?

@Seppli11 Seppli11 closed this Nov 14, 2024
@Seppli11 Seppli11 deleted the SONARPY-2202 branch December 10, 2024 09:50
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