-
Notifications
You must be signed in to change notification settings - Fork 93
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
Conversation
4d37b04
to
3d389b0
Compare
|
||
@Override | ||
public TriBool test(PythonType otherMaybeWrappedType) { |
There was a problem hiding this comment.
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
.
4e9ea82
to
49c3d6c
Compare
1d7a71b
to
601d05d
Compare
f2e4d76
to
8bcf3f3
Compare
There was a problem hiding this comment.
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
bc18ff4
to
494711d
Compare
return Set.of(type); | ||
} | ||
|
||
public static PythonType unwrapType(PythonType type) { |
There was a problem hiding this comment.
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,
c77f94e
to
35eb809
Compare
The disabled test dependent on SONARPY-2241
35eb809
to
cea811f
Compare
Quality Gate passedIssues Measures |
There was a problem hiding this 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) { |
There was a problem hiding this comment.
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 ?
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)) | ||
); | ||
} |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the unwrapType needed ?
No description provided.