@@ -363,6 +363,73 @@ def foo(param: int):
363
363
Assertions .assertThat (lastExpressionStatement .expressions ().get (0 ).typeV2 ().typeSource ()).isEqualTo (TypeSource .TYPE_HINT );
364
364
}
365
365
366
+ @ Test
367
+ void typeSourceOfCallExpressionResultDependsOnTypeSourceOfQualifier () {
368
+ FileInput root = inferTypes ("""
369
+ def foo(x: int):
370
+ y = x.conjugate()
371
+ y
372
+ z = x.conjugate().conjugate()
373
+ z
374
+ """ );
375
+ var functionDef = (FunctionDef ) root .statements ().statements ().get (0 );
376
+ var yStatement = (ExpressionStatement ) functionDef .body ().statements ().get (1 );
377
+ PythonType yType = yStatement .expressions ().get (0 ).typeV2 ();
378
+ assertThat (yType ).isInstanceOf (ObjectType .class );
379
+ assertThat (yType .unwrappedType ()).isEqualTo (INT_TYPE );
380
+ assertThat (yType .typeSource ()).isEqualTo (TypeSource .TYPE_HINT );
381
+
382
+ var zStatement = (ExpressionStatement ) functionDef .body ().statements ().get (3 );
383
+ PythonType zType = zStatement .expressions ().get (0 ).typeV2 ();
384
+ assertThat (zType ).isInstanceOf (ObjectType .class );
385
+ assertThat (zType .unwrappedType ()).isEqualTo (INT_TYPE );
386
+ assertThat (zType .typeSource ()).isEqualTo (TypeSource .TYPE_HINT );
387
+ }
388
+
389
+ @ Test
390
+ void typeSourceOfCallExpressionResultDependsOnTypeSourceOfName () {
391
+ FileInput fileInput = inferTypes ("""
392
+ from pyasn1.debug import Printer
393
+ def foo(p: Printer):
394
+ a = p()
395
+ a
396
+ b = p.__call__()
397
+ b
398
+ """ );
399
+
400
+ var functionDef = (FunctionDef ) fileInput .statements ().statements ().get (1 );
401
+ var aStatement = (ExpressionStatement ) functionDef .body ().statements ().get (1 );
402
+ PythonType aType = aStatement .expressions ().get (0 ).typeV2 ();
403
+ assertThat (aType ).isInstanceOf (ObjectType .class );
404
+ assertThat (aType .unwrappedType ()).isEqualTo (NONE_TYPE );
405
+ assertThat (aType .typeSource ()).isEqualTo (TypeSource .TYPE_HINT );
406
+
407
+ var bStatement = (ExpressionStatement ) functionDef .body ().statements ().get (3 );
408
+ PythonType bType = bStatement .expressions ().get (0 ).typeV2 ();
409
+ assertThat (bType ).isInstanceOf (ObjectType .class );
410
+ assertThat (bType .unwrappedType ()).isEqualTo (NONE_TYPE );
411
+ assertThat (bType .typeSource ()).isEqualTo (TypeSource .TYPE_HINT );
412
+ }
413
+
414
+ @ Test
415
+ void typeSourceIsExactByDefault () {
416
+ FileInput fileInput = inferTypes ("""
417
+ random[2]()
418
+ """ );
419
+ CallExpression callExpression = ((CallExpression ) ((ExpressionStatement ) fileInput .statements ().statements ().get (0 )).expressions ().get (0 ));
420
+
421
+ CallExpression callExpressionSpy = Mockito .spy (callExpression );
422
+ Expression calleeSpy = Mockito .spy (callExpression .callee ());
423
+ FunctionType functionType = new FunctionType ("foo" , List .of (), List .of (), INT_TYPE , false , false , false , false , null , null );
424
+ Mockito .when (calleeSpy .typeV2 ()).thenReturn (functionType );
425
+ Mockito .when (callExpressionSpy .callee ()).thenReturn (calleeSpy );
426
+
427
+ var resultType = callExpressionSpy .typeV2 ();
428
+ assertThat (resultType .typeSource ()).isEqualTo (TypeSource .EXACT );
429
+ assertThat (resultType ).isInstanceOf (ObjectType .class );
430
+ assertThat (resultType .unwrappedType ()).isEqualTo (INT_TYPE );
431
+ }
432
+
366
433
@ Test
367
434
void inferTypesInsideFunction6 () {
368
435
FileInput root = inferTypes ("""
@@ -1791,6 +1858,64 @@ def bar(self): ...
1791
1858
assertThat (unionType .candidates ()).containsExactlyInAnyOrder (classA , classB );
1792
1859
}
1793
1860
1861
+ @ Test
1862
+ void return_type_of_call_expression_inconsistent () {
1863
+ FileInput fileInput = inferTypes (
1864
+ """
1865
+ foo()
1866
+ """
1867
+ );
1868
+ CallExpression callExpression = ((CallExpression ) ((ExpressionStatement ) fileInput .statements ().statements ().get (0 )).expressions ().get (0 ));
1869
+ CallExpression callExpressionSpy = Mockito .spy (callExpression );
1870
+
1871
+ // Inconsistent union type, should not happen
1872
+ UnionType unionType = new UnionType (Set .of (PythonType .UNKNOWN ));
1873
+ Name mock = Mockito .mock (Name .class );
1874
+ Mockito .when (mock .typeV2 ()).thenReturn (unionType );
1875
+ Mockito .doReturn (mock ).when (callExpressionSpy ).callee ();
1876
+
1877
+ assertThat (callExpressionSpy .typeV2 ()).isEqualTo (PythonType .UNKNOWN );
1878
+ }
1879
+
1880
+ @ Test
1881
+ void return_type_of_call_expression_inconsistent_2 () {
1882
+ FileInput fileInput = inferTypes (
1883
+ """
1884
+ foo()
1885
+ """
1886
+ );
1887
+ CallExpression callExpression = ((CallExpression ) ((ExpressionStatement ) fileInput .statements ().statements ().get (0 )).expressions ().get (0 ));
1888
+ CallExpression callExpressionSpy = Mockito .spy (callExpression );
1889
+
1890
+ // Inconsistent union type, should not happen
1891
+ UnionType unionType = new UnionType (Set .of ());
1892
+ Name mock = Mockito .mock (Name .class );
1893
+ Mockito .when (mock .typeV2 ()).thenReturn (unionType );
1894
+ Mockito .doReturn (mock ).when (callExpressionSpy ).callee ();
1895
+
1896
+ assertThat (callExpressionSpy .typeV2 ()).isEqualTo (PythonType .UNKNOWN );
1897
+ }
1898
+
1899
+ @ Test
1900
+ void return_type_of_call_expression_inconsistent_3 () {
1901
+ FileInput fileInput = inferTypes (
1902
+ """
1903
+ foo()
1904
+ """
1905
+ );
1906
+ CallExpression callExpression = ((CallExpression ) ((ExpressionStatement ) fileInput .statements ().statements ().get (0 )).expressions ().get (0 ));
1907
+ CallExpression callExpressionSpy = Mockito .spy (callExpression );
1908
+
1909
+ // Inconsistent union type, should not happen
1910
+ UnionType unionType = new UnionType (Set .of (INT_TYPE ));
1911
+ Name mock = Mockito .mock (Name .class );
1912
+ Mockito .when (mock .typeV2 ()).thenReturn (unionType );
1913
+ Mockito .doReturn (mock ).when (callExpressionSpy ).callee ();
1914
+
1915
+ assertThat (callExpressionSpy .typeV2 ().unwrappedType ()).isEqualTo (INT_TYPE );
1916
+ }
1917
+
1918
+
1794
1919
@ Test
1795
1920
void imported_symbol_call_return_type () {
1796
1921
assertThat (lastExpression (
0 commit comments