File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed
main/java/org/sonar/python/semantic/v2/types
test/java/org/sonar/python/semantic/v2 Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change 3636import org .sonar .plugins .python .api .tree .AliasedName ;
3737import org .sonar .plugins .python .api .tree .AnnotatedAssignment ;
3838import org .sonar .plugins .python .api .tree .ArgList ;
39- import org .sonar .plugins .python .api .tree .AwaitExpression ;
4039import org .sonar .plugins .python .api .tree .AssignmentExpression ;
4140import org .sonar .plugins .python .api .tree .AssignmentStatement ;
41+ import org .sonar .plugins .python .api .tree .AwaitExpression ;
4242import org .sonar .plugins .python .api .tree .BaseTreeVisitor ;
4343import org .sonar .plugins .python .api .tree .BinaryExpression ;
4444import org .sonar .plugins .python .api .tree .CallExpression ;
@@ -730,8 +730,8 @@ private ClassType computeDirectClassOwner() {
730730 }
731731
732732 private PythonType resolveTypeAnnotationExpressionType (Expression expression , @ Nullable ClassType enclosingClassType ) {
733- if (expression instanceof Name name && name .typeV2 () != PythonType .UNKNOWN ) {
734- PythonType resolvedType = resolveSelfType (name .typeV2 (), enclosingClassType );
733+ if (( expression instanceof Name || expression instanceof QualifiedExpression ) && expression .typeV2 () != PythonType .UNKNOWN ) {
734+ PythonType resolvedType = resolveSelfType (expression .typeV2 (), enclosingClassType );
735735 return ObjectType .Builder .fromType (resolvedType ).withTypeSource (TypeSource .TYPE_HINT ).build ();
736736 } else if (expression instanceof SubscriptionExpression subscriptionExpression && subscriptionExpression .object ().typeV2 () != PythonType .UNKNOWN ) {
737737 var candidateTypes = subscriptionExpression .subscripts ()
Original file line number Diff line number Diff line change @@ -4316,6 +4316,27 @@ class B(datetime.datetime): pass
43164316 .isEqualTo (bType );
43174317 }
43184318
4319+ @ Test
4320+ void self_type_as_return_type_on_inherited_methods_with_qualified_expression () {
4321+ FileInput fileInput = inferTypes ("""
4322+ import typing
4323+ class A:
4324+ def foo() -> typing.Self: ...
4325+
4326+ class B(A):
4327+ ...
4328+ resultB = B().foo()
4329+ """ );
4330+
4331+ PythonType resultB = PythonTestUtils .<Name >getFirstChild (fileInput , t -> t instanceof Name name && "resultB" .equals (name .name ())).typeV2 ();
4332+ PythonType classTypeB = PythonTestUtils .<ClassDef >getFirstChild (fileInput , t -> t instanceof ClassDef cd && "B" .equals (cd .name ().name ())).name ().typeV2 ();
4333+
4334+ assertThat (resultB )
4335+ .isInstanceOf (ObjectType .class )
4336+ .extracting (PythonType ::unwrappedType )
4337+ .isEqualTo (classTypeB );
4338+ }
4339+
43194340 @ Test
43204341 void self_type_as_return_type_on_inherited_methods () {
43214342 FileInput fileInput = inferTypes ("""
You can’t perform that action at this time.
0 commit comments