Skip to content

Added support for bidirectional type inference when assigning an expr… #10585

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

Merged
merged 1 commit into from
Jun 10, 2025

Conversation

erictraut
Copy link
Collaborator

…ession to an unpacked tuple literal and all of the items in the tuple have a declared type. This addresses #10481.

…ession to an unpacked tuple literal and all of the items in the tuple have a declared type. This addresses #10481.
Copy link
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

sympy (https://github.com/sympy/sympy)
-   .../projects/sympy/sympy/tensor/array/expressions/array_expressions.py:1030:70 - error: Argument of type "Unknown | Basic" cannot be assigned to parameter "obj" of type "Sized" in function "len"
+   .../projects/sympy/sympy/tensor/array/expressions/array_expressions.py:1030:70 - error: Argument of type "Unknown | Tuple | Basic" cannot be assigned to parameter "obj" of type "Sized" in function "len"
-     Type "Unknown | Basic" is not assignable to type "Sized"
+     Type "Unknown | Tuple | Basic" is not assignable to type "Sized"
+   .../projects/sympy/sympy/tensor/array/expressions/array_expressions.py:1030:80 - error: No overloads for "__getitem__" match the provided arguments (reportCallIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/array_expressions.py:1030:80 - error: Argument of type "Unknown | Tuple" cannot be assigned to parameter "key" of type "slice[Any, Any, Any]" in function "__getitem__"
+     Type "Unknown | Tuple" is not assignable to type "slice[Any, Any, Any]"
+       "Tuple" is not assignable to "slice[Any, Any, Any]" (reportArgumentType)
-   .../projects/sympy/sympy/tensor/array/expressions/arrayexpr_derivatives.py:64:2 - error: Argument of type "(expr: ArraySymbol, x: _ArrayExpr) -> (PermuteDims | Zero | ZeroArray)" cannot be assigned to parameter of type "(...) -> NoReturn"
+   .../projects/sympy/sympy/tensor/array/expressions/arrayexpr_derivatives.py:64:2 - error: Argument of type "(expr: ArraySymbol, x: _ArrayExpr) -> (Zero | ZeroArray | Unknown | Basic | PermuteDims)" cannot be assigned to parameter of type "(...) -> NoReturn"
-     Type "(expr: ArraySymbol, x: _ArrayExpr) -> (PermuteDims | Zero | ZeroArray)" is not assignable to type "(...) -> NoReturn"
+     Type "(expr: ArraySymbol, x: _ArrayExpr) -> (Zero | ZeroArray | Unknown | Basic | PermuteDims)" is not assignable to type "(...) -> NoReturn"
-       Function return type "PermuteDims | Zero | ZeroArray" is incompatible with type "NoReturn"
+       Function return type "Zero | ZeroArray | Unknown | Basic | PermuteDims" is incompatible with type "NoReturn"
-         Type "PermuteDims | Zero | ZeroArray" is not assignable to type "NoReturn"
+         Type "Zero | ZeroArray | Unknown | Basic | PermuteDims" is not assignable to type "NoReturn"
-           Type "PermuteDims" is not assignable to type "NoReturn" (reportArgumentType)
+           Type "Basic" is not assignable to type "NoReturn" (reportArgumentType)
-   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:81:36 - error: Argument of type "Basic | Unknown | Zero | ZeroArray | PermuteDims | ArrayContraction" cannot be assigned to parameter "base_array" of type "ArrayContraction | ArrayDiagonal | ArrayTensorProduct" in function "__init__"
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:81:36 - error: Argument of type "Basic | Unknown | Zero | ZeroArray | ArrayTensorProduct | ArrayContraction" cannot be assigned to parameter "base_array" of type "ArrayContraction | ArrayDiagonal | ArrayTensorProduct" in function "__init__"
-     Type "Basic | Unknown | Zero | ZeroArray | PermuteDims | ArrayContraction" is not assignable to type "ArrayContraction | ArrayDiagonal | ArrayTensorProduct"
+     Type "Basic | Unknown | Zero | ZeroArray | ArrayTensorProduct | ArrayContraction" is not assignable to type "ArrayContraction | ArrayDiagonal | ArrayTensorProduct"
-   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:214:12 - error: Type "ArrayContraction | Basic | Unknown | Zero | ZeroArray | PermuteDims" is not assignable to declared type "ArrayContraction"
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:214:12 - error: Type "ArrayContraction | Basic | Unknown | Zero | ZeroArray | ArrayTensorProduct" is not assignable to declared type "ArrayContraction"
-     Type "ArrayContraction | Basic | Unknown | Zero | ZeroArray | PermuteDims" is not assignable to type "ArrayContraction"
+     Type "ArrayContraction | Basic | Unknown | Zero | ZeroArray | ArrayTensorProduct" is not assignable to type "ArrayContraction"
-   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:215:12 - error: Type "PermuteDims" is not assignable to declared type "ArrayContraction"
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:215:12 - error: Type "Zero | ZeroArray | Unknown | Basic | PermuteDims" is not assignable to declared type "ArrayContraction"
+     Type "Zero | ZeroArray | Unknown | Basic | PermuteDims" is not assignable to type "ArrayContraction"
-     "PermuteDims" is not assignable to "ArrayContraction" (reportAssignmentType)
+       "Basic" is not assignable to "ArrayContraction" (reportAssignmentType)
-   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:216:12 - error: Type "PermuteDims" is not assignable to declared type "ArrayContraction"
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:216:12 - error: Type "Zero | ZeroArray | Unknown | Basic | PermuteDims" is not assignable to declared type "ArrayContraction"
+     Type "Zero | ZeroArray | Unknown | Basic | PermuteDims" is not assignable to type "ArrayContraction"
-     "PermuteDims" is not assignable to "ArrayContraction" (reportAssignmentType)
+       "Basic" is not assignable to "ArrayContraction" (reportAssignmentType)
-   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:217:12 - error: Type "PermuteDims" is not assignable to declared type "ArrayContraction"
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:217:12 - error: Type "Zero | ZeroArray | Unknown | Basic | PermuteDims" is not assignable to declared type "ArrayContraction"
+     Type "Zero | ZeroArray | Unknown | Basic | PermuteDims" is not assignable to type "ArrayContraction"
-     "PermuteDims" is not assignable to "ArrayContraction" (reportAssignmentType)
+       "Basic" is not assignable to "ArrayContraction" (reportAssignmentType)
-   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:233:39 - error: Cannot access attribute "contraction_indices" for class "PermuteDims"
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:233:39 - error: Cannot access attribute "contraction_indices" for class "ArrayTensorProduct"
-   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:307:17 - error: Argument of type "PermuteDims | Any | Transpose" cannot be assigned to parameter "value" of type "None" in function "__setitem__"
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:307:17 - error: Argument of type "Zero | ZeroArray | Unknown | Basic | PermuteDims | Any | Transpose" cannot be assigned to parameter "value" of type "None" in function "__setitem__"
-     Type "PermuteDims | Any | Transpose" is not assignable to type "None"
+     Type "Zero | ZeroArray | Unknown | Basic | PermuteDims | Any | Transpose" is not assignable to type "None"
-       "PermuteDims" is not assignable to "None" (reportArgumentType)
+       "Basic" is not assignable to "None" (reportArgumentType)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:383:32 - error: Cannot access attribute "shape" for class "One"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:383:32 - error: Cannot access attribute "shape" for class "NegativeOne"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:383:32 - error: Cannot access attribute "shape" for class "Zero"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:383:32 - error: Cannot access attribute "shape" for class "Integer"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:383:32 - error: Cannot access attribute "shape" for class "NaN"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:383:32 - error: Cannot access attribute "shape" for class "ComplexInfinity"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:383:32 - error: Cannot access attribute "shape" for class "Rational"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:383:32 - error: Cannot access attribute "shape" for class "Infinity"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:383:32 - error: Cannot access attribute "shape" for class "NegativeInfinity"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:383:32 - error: Cannot access attribute "shape" for class "Float"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:398:25 - error: Cannot access attribute "shape" for class "One"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:398:25 - error: Cannot access attribute "shape" for class "NegativeOne"
+     Attribute "shape" is unknown (reportAttributeAccessIssue)
+   .../projects/sympy/sympy/tensor/array/expressions/from_array_to_matrix.py:398:25 - error: Cannot access attribute "shape" for class "Zero"

... (truncated 432 lines) ...

@erictraut erictraut merged commit 859cc50 into main Jun 10, 2025
17 checks passed
@erictraut erictraut deleted the issue-10481 branch June 10, 2025 17:07
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.

1 participant