|
25 | 25 | import java.util.Set; |
26 | 26 | import java.util.function.Function; |
27 | 27 | import java.util.stream.Stream; |
| 28 | +import org.assertj.core.api.Condition; |
28 | 29 | import org.assertj.core.api.InstanceOfAssertFactories; |
29 | 30 | import org.assertj.core.data.Index; |
30 | 31 | import org.assertj.core.groups.Tuple; |
@@ -4906,4 +4907,69 @@ class AClass(SuperClass): |
4906 | 4907 | .isInstanceOf(LazyTypeWrapper.class), Index.atIndex(0)); |
4907 | 4908 | }); |
4908 | 4909 | } |
| 4910 | + |
| 4911 | + @Test |
| 4912 | + void testParametersWithAttributes() { |
| 4913 | + FileInput root = inferTypes(""" |
| 4914 | + from typing import Annotated |
| 4915 | + def test(a: Annotated[int, "foo"]) -> Annotated[int, "bar"]: |
| 4916 | + pass |
| 4917 | + """); |
| 4918 | + |
| 4919 | + FunctionDef functionDef = PythonTestUtils.getFirstChild(root, FunctionDef.class::isInstance); |
| 4920 | + FunctionType functionType = (FunctionType) functionDef.name().typeV2(); |
| 4921 | + |
| 4922 | + assertThat(functionType.parameters()).hasSize(1); |
| 4923 | + |
| 4924 | + ParameterV2 parameter = functionType.parameters().get(0); |
| 4925 | + assertThat(parameter.declaredType().type()) |
| 4926 | + .isInstanceOfSatisfying(ObjectType.class, objType -> assertThat(objType.attributes()) |
| 4927 | + .satisfies(intAttribute -> assertThat(intAttribute).is(objectTypeOf(INT_TYPE)), Index.atIndex(0)) |
| 4928 | + .satisfies(strAttribute -> assertThat(strAttribute).is(objectTypeOf(STR_TYPE)), Index.atIndex(1))); |
| 4929 | + |
| 4930 | + PythonType returnType = functionType.returnType(); |
| 4931 | + assertThat(returnType) |
| 4932 | + .isInstanceOfSatisfying(ObjectType.class, objType -> assertThat(objType.attributes()) |
| 4933 | + .satisfies(intAttribute -> assertThat(intAttribute).is(objectTypeOf(INT_TYPE)), Index.atIndex(0)) |
| 4934 | + .satisfies(strAttribute -> assertThat(strAttribute).is(objectTypeOf(STR_TYPE)), Index.atIndex(1))); |
| 4935 | + } |
| 4936 | + |
| 4937 | + @Test |
| 4938 | + void testParametersWithAttributesWithLocalClassTypes() { |
| 4939 | + FileInput root = inferTypes(""" |
| 4940 | + from typing import Annotated |
| 4941 | + class MyClass: ... |
| 4942 | + def test(a: Annotated[int, MyClass()]) -> Annotated[int, MyClass()]: |
| 4943 | + pass |
| 4944 | + """); |
| 4945 | + |
| 4946 | + FunctionDef functionDef = PythonTestUtils.getFirstChild(root, FunctionDef.class::isInstance); |
| 4947 | + FunctionType functionType = (FunctionType) functionDef.name().typeV2(); |
| 4948 | + |
| 4949 | + ClassType myClass = (ClassType) PythonTestUtils.<ClassDef>getFirstChild(root, ClassDef.class::isInstance).name().typeV2(); |
| 4950 | + |
| 4951 | + assertThat(functionType.parameters()).hasSize(1); |
| 4952 | + |
| 4953 | + ParameterV2 parameter = functionType.parameters().get(0); |
| 4954 | + assertThat(parameter.declaredType().type()) |
| 4955 | + .isInstanceOfSatisfying(ObjectType.class, objType -> assertThat(objType.attributes()) |
| 4956 | + .satisfies(intAttribute -> assertThat(intAttribute).is(objectTypeOf(INT_TYPE)), Index.atIndex(0)) |
| 4957 | + .satisfies(myClassAttribute -> assertThat(myClassAttribute).is(objectTypeOf(myClass)), Index.atIndex(1))); |
| 4958 | + |
| 4959 | + PythonType returnType = functionType.returnType(); |
| 4960 | + assertThat(returnType) |
| 4961 | + .isInstanceOfSatisfying(ObjectType.class, objType -> assertThat(objType.attributes()) |
| 4962 | + .satisfies(intAttribute -> assertThat(intAttribute).is(objectTypeOf(INT_TYPE)), Index.atIndex(0)) |
| 4963 | + .satisfies(myClassAttribute -> assertThat(myClassAttribute).is(objectTypeOf(myClass)), Index.atIndex(1))); |
| 4964 | + } |
| 4965 | + |
| 4966 | + private static Condition<PythonType> objectTypeOf(PythonType type) { |
| 4967 | + return new Condition<PythonType>("is object type of " + type) { |
| 4968 | + @Override |
| 4969 | + public boolean matches(PythonType value) { |
| 4970 | + return value instanceof ObjectType objectType && objectType.unwrappedType().equals(type); |
| 4971 | + } |
| 4972 | + }; |
| 4973 | + } |
| 4974 | + |
4909 | 4975 | } |
0 commit comments