Numpy 1.20 includes typing information, breaks mypy checks locally #3767
Closed
Description
Description of the issue
mypy encounters errors.
How to reproduce the issue
In a fresh virtual environment, install Cirq and the dev tools (mypy version 0.782). Then execute
./check/mypy
This gives the following errors:
cirq/linalg/tolerance.py:31: error: Incompatible return value type (got "bool_", expected "bool")
cirq/linalg/tolerance.py:31: error: Argument 1 to "__call__" of "ufunc" has incompatible type "Union[float, complex, Iterable[float], ndarray]"; expected "Union[Union[int, float, complex, str, bytes, generic], Sequence[Union[int, float, complex, str, bytes, generic]], Sequence[Sequence[Any]], _SupportsArray]"
cirq/linalg/tolerance.py:45: error: Incompatible return value type (got "bool_", expected "bool")
cirq/linalg/combinators.py:157: error: Argument 1 to "reduce" has incompatible type "Callable[[Type[number[Any]], Type[number[Any]]], Type[number[Any]]]"; expected "Callable[[dtype[Any], dtype[Any]], dtype[Any]]"
cirq/interop/quirk/cells/parse.py:79: error: Incompatible return value type (got "float", expected "str")
cirq/google/optimizers/two_qubit_gates/math_utils.py:62: error: Item Module of "Union[Module, Any]" has no attribute "rand"
cirq/google/optimizers/two_qubit_gates/math_utils.py:63: error: Item Module of "Union[Module, Any]" has no attribute "rand"
cirq/google/optimizers/two_qubit_gates/math_utils.py:64: error: Item Module of "Union[Module, Any]" has no attribute "rand"
cirq/google/optimizers/two_qubit_gates/math_utils.py:70: error: Item Module of "Union[Module, Any]" has no attribute "rand"
cirq/experiments/purity_estimation.py:62: error: Incompatible return value type (got "number[Any]", expected "float")
cirq/linalg/states.py:53: error: Function "numpy.array" is not valid as a type
cirq/linalg/states.py:53: note: Perhaps you need "Callable[...]" or a callback protocol?
cirq/work/pauli_sum_collector.py:81: error: Unsupported operand types for % ("number[Any]" and "int")
cirq/work/pauli_sum_collector.py:81: note: Left operand is of type "Union[number[Any], ndarray]"
cirq/vis/heatmap.py:267: error: No overload variant of "round" matches argument type "number[Any]"
cirq/vis/heatmap.py:267: note: Possible overload variants:
cirq/vis/heatmap.py:267: note: def round(number: float) -> int
cirq/vis/heatmap.py:267: note: def [_T] round(number: SupportsRound[_T]) -> int
cirq/vis/heatmap.py:267: note: <4 more non-matching overloads not shown>
cirq/vis/heatmap.py:268: error: No overload variant of "round" matches argument type "number[Any]"
cirq/vis/heatmap.py:268: note: Possible overload variants:
cirq/vis/heatmap.py:268: note: def round(number: float) -> int
cirq/vis/heatmap.py:268: note: def [_T] round(number: SupportsRound[_T]) -> int
cirq/vis/heatmap.py:268: note: <4 more non-matching overloads not shown>
cirq/value/product_state.py:140: error: Incompatible return value type (got "complex", expected "ndarray")
cirq/value/product_state.py:159: error: Incompatible return value type (got "complex", expected "ndarray")
cirq/testing/consistent_qasm.py:89: error: Incompatible types in assignment (expression has type "None", variable has type "ndarray")
cirq/testing/consistent_qasm.py:90: error: Incompatible types in assignment (expression has type "None", variable has type "ndarray")
cirq/testing/circuit_compare.py:280: error: Unsupported left operand type for * (np.array?)
cirq/testing/circuit_compare.py:295: error: Argument 1 to "reshape" of "_ArrayOrScalarCommon" has incompatible type "Tuple[number[Any], ...]"; expected "Sequence[int]"
cirq/study/result.py:356: error: Module has no attribute "bool"; maybe "bool_" or "bool8"?
cirq/study/result.py:357: error: Module has no attribute "bool"; maybe "bool_" or "bool8"?
cirq/study/result.py:404: error: Slice index must be an integer or None
cirq/sim/density_matrix_utils.py:197: error: Incompatible return value type (got "Tuple[List[int], Optional[ndarray]]", expected "Tuple[List[int], ndarray]")
cirq/sim/density_matrix_utils.py:205: error: Argument 2 to "reshape" has incompatible type "Tuple[number[Any], ...]"; expected "Union[int, Sequence[int]]"
cirq/sim/density_matrix_utils.py:225: error: No overload variant of "range" matches argument type "number[Any]"
cirq/sim/density_matrix_utils.py:225: note: Possible overload variant:
cirq/sim/density_matrix_utils.py:225: note: def range(self, stop: int) -> range
cirq/sim/density_matrix_utils.py:225: note: <1 more non-matching overload not shown>
cirq/sim/density_matrix_utils.py:236: error: Function "numpy.array" is not valid as a type
cirq/sim/density_matrix_utils.py:236: note: Perhaps you need "Callable[...]" or a callback protocol?
cirq/sim/density_matrix_utils.py:241: error: np.array? has no attribute "shape"
cirq/sim/density_matrix_utils.py:270: error: Unsupported operand types for & ("object" and "int")
cirq/sim/density_matrix_utils.py:270: error: Unsupported operand types for - ("object" and "int")
cirq/sim/density_matrix_utils.py:280: error: No overload variant of "int" matches argument type "object"
cirq/sim/density_matrix_utils.py:280: note: Possible overload variant:
cirq/sim/density_matrix_utils.py:280: note: def int(self, x: Union[str, bytes, SupportsInt, _SupportsIndex] = ...) -> int
cirq/sim/density_matrix_utils.py:280: note: <1 more non-matching overload not shown>
cirq/qis/states.py:428: error: Argument "shape" to "one_hot" has incompatible type "number[Any]"; expected "Union[int, Sequence[int]]"
cirq/qis/states.py:658: error: Function "numpy.array" is not valid as a type
cirq/qis/states.py:658: note: Perhaps you need "Callable[...]" or a callback protocol?
cirq/protocols/commutes_protocol.py:81: error: Incompatible default for argument "default" (default has type "ndarray", argument has type "TDefault")
cirq/optimizers/two_qubit_to_fsim.py:85: error: Argument 1 to "kak_decomposition" has incompatible type "Union[Operation, Gate, ndarray, Any]"; expected "Union[ndarray, SupportsUnitary]"
cirq/optimizers/two_qubit_to_fsim.py:185: error: Argument 1 to "kak_decomposition" has incompatible type "Union[Operation, Gate, ndarray, Any]"; expected "Union[ndarray, SupportsUnitary]"
cirq/optimizers/two_qubit_to_fsim.py:201: error: Argument 1 to "kak_decomposition" has incompatible type "_BGate"; expected "Union[ndarray, SupportsUnitary]"
cirq/optimizers/three_qubit_decomposition.py:185: error: Incompatible types in assignment (expression has type "None", variable has type "ndarray")
cirq/optimizers/merge_single_qubit_gates.py:71: error: Argument 2 has incompatible type "Union[float, complex, ndarray]"; expected "ndarray"
cirq/optimizers/merge_single_qubit_gates.py:74: error: Argument 1 to "MatrixGate" has incompatible type "Union[float, complex, ndarray]"; expected "ndarray"
cirq/optimizers/decompositions.py:140: error: Value of type np.array? is not indexable
cirq/optimizers/decompositions.py:141: error: Value of type np.array? is not indexable
cirq/ops/two_qubit_diagonal_gate.py:70: error: Incompatible return value type (got "None", expected "ndarray")
cirq/ops/raw_types.py:535: error: Argument 2 to "allclose" has incompatible type "Optional[ndarray]"; expected "Union[Union[int, float, complex, str, bytes, generic], Sequence[Union[int, float, complex, str, bytes, generic]], Sequence[Sequence[Any]], _SupportsArray]"
cirq/ops/phased_x_z_gate.py:111: error: Function "numpy.array" is not valid as a type
cirq/ops/phased_x_z_gate.py:111: note: Perhaps you need "Callable[...]" or a callback protocol?
cirq/ops/phased_iswap_gate.py:156: error: Argument 1 to "isclose" has incompatible type "Union[complex, float, Complex]"; expected "Union[Union[int, float, complex, str, bytes, generic], Sequence[Union[int, float, complex, str, bytes, generic]], Sequence[Sequence[Any]], _SupportsArray]"
cirq/ops/phased_iswap_gate.py:156: error: Argument 2 to "isclose" has incompatible type "Union[complex, float, Complex]"; expected "Union[Union[int, float, complex, str, bytes, generic], Sequence[Union[int, float, complex, str, bytes, generic]], Sequence[Sequence[Any]], _SupportsArray]"
cirq/ops/pauli_string.py:549: error: Argument "dtype" to "validate_normalized_state_vector" has incompatible type "dtype[Any]"; expected "Optional[Type[number[Any]]]"
cirq/ops/pauli_string.py:653: error: Argument "dtype" to "to_valid_density_matrix" has incompatible type "dtype[Any]"; expected "Optional[Type[number[Any]]]"
cirq/ops/identity.py:76: error: Argument 1 to "identity" has incompatible type "number[Any]"; expected "int"
cirq/ops/eigen_gate.py:188: error: Incompatible types in assignment (expression has type "number[Any]", variable has type "float")
cirq/ops/dense_pauli_string.py:374: error: Argument 2 of "copy" is incompatible with supertype "BaseDensePauliString"; supertype defines the argument type as "Union[str, Iterable[int], ndarray, None]"
cirq/ops/dense_pauli_string.py:374: note: This violates the Liskov substitution principle
cirq/ops/dense_pauli_string.py:374: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
cirq/ops/dense_pauli_string.py:446: error: Argument 2 of "copy" is incompatible with supertype "BaseDensePauliString"; supertype defines the argument type as "Union[str, Iterable[int], ndarray, None]"
cirq/ops/dense_pauli_string.py:446: note: This violates the Liskov substitution principle
cirq/ops/dense_pauli_string.py:446: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#incompatible-overrides
cirq/ops/dense_pauli_string.py:552: error: Unsupported operand types for & ("number[Any]" and "int")
cirq/ops/dense_pauli_string.py:552: note: Left operand is of type "Union[number[Any], ndarray]"
cirq/ops/controlled_operation.py:137: error: Argument "dtype" to "eye_tensor" has incompatible type "dtype[Any]"; expected "Type[number[Any]]"
cirq/ops/controlled_operation.py:141: error: Unsupported target for indexed assignment (np.array?)
cirq/ops/controlled_operation.py:142: error: np.array? has no attribute "reshape"
cirq/linalg/transformations.py:411: error: Incompatible default for argument "default" (default has type "ndarray", argument has type "TDefault")
cirq/linalg/transformations.py:498: error: Incompatible return value type (got "TDefault", expected "ndarray")
cirq/linalg/predicates.py:74: error: Incompatible return value type (got "Union[bool, bool_]", expected "bool")
cirq/linalg/operator_spaces.py:42: error: Incompatible return value type (got "Dict[str, int]", expected "Dict[str, ndarray]")
cirq/linalg/operator_spaces.py:108: error: Argument 1 to "__call__" of "ufunc" has incompatible type "Union[complex, float, Complex]"; expected "Union[Union[int, float, complex, str, bytes, generic], Sequence[Union[int, float, complex, str, bytes, generic]], Sequence[Sequence[Any]], _SupportsArray]"
cirq/linalg/diagonalize.py:186: error: Argument 1 to "is_hermitian" has incompatible type "Union[number[Any], ndarray]"; expected "ndarray"
cirq/linalg/diagonalize.py:188: error: Argument 1 to "is_hermitian" has incompatible type "Union[number[Any], ndarray]"; expected "ndarray"
cirq/linalg/diagonalize.py:224: error: Incompatible return value type (got "Tuple[Union[number[Any], ndarray], Any]", expected "Tuple[ndarray, ndarray]")
cirq/linalg/diagonalize.py:229: error: Function "numpy.array" is not valid as a type
cirq/linalg/diagonalize.py:229: note: Perhaps you need "Callable[...]" or a callback protocol?
cirq/linalg/decompositions.py:121: error: Function "numpy.array" is not valid as a type
cirq/linalg/decompositions.py:121: note: Perhaps you need "Callable[...]" or a callback protocol?
cirq/linalg/decompositions.py:166: error: np.array? has no attribute "astype"
cirq/linalg/decompositions.py:250: error: Argument 1 to "kron_factor_4x4_to_2x2s" has incompatible type "Union[float, complex, ndarray]"; expected "ndarray"
cirq/linalg/decompositions.py:512: error: Incompatible return value type (got "Union[complex, Any]", expected "ndarray")
cirq/linalg/decompositions.py:652: error: Argument 1 to "kak_vector" has incompatible type "Iterable[Union[ndarray, SupportsUnitary, KakDecomposition]]"; expected "Union[Iterable[ndarray], ndarray]"
cirq/google/optimizers/two_qubit_gates/gate_compilation.py:96: error: No overload variant of "__getitem__" of "Sequence" matches argument type "signedinteger[Any]"
cirq/google/optimizers/two_qubit_gates/gate_compilation.py:96: note: Possible overload variants:
cirq/google/optimizers/two_qubit_gates/gate_compilation.py:96: note: def __getitem__(self, int) -> Sequence[Tuple[ndarray, ndarray]]
cirq/google/optimizers/two_qubit_gates/gate_compilation.py:96: note: def __getitem__(self, slice) -> Sequence[Sequence[Tuple[ndarray, ndarray]]]
cirq/google/optimizers/two_qubit_gates/gate_compilation.py:110: error: No overload variant of "__getitem__" of "Sequence" matches argument type "signedinteger[Any]"
cirq/google/optimizers/two_qubit_gates/gate_compilation.py:110: note: Possible overload variants:
cirq/google/optimizers/two_qubit_gates/gate_compilation.py:110: note: def __getitem__(self, int) -> Sequence[Tuple[ndarray, ndarray]]
cirq/google/optimizers/two_qubit_gates/gate_compilation.py:110: note: def __getitem__(self, slice) -> Sequence[Sequence[Tuple[ndarray, ndarray]]]
cirq/google/optimizers/two_qubit_gates/gate_compilation.py:470: error: Incompatible types in assignment (expression has type "ndarray", variable has type "List[ndarray]")
cirq/google/optimizers/two_qubit_gates/gate_compilation.py:478: error: Argument 2 to "GateTabulation" has incompatible type "List[ndarray]"; expected "ndarray"
cirq/experiments/single_qubit_readout_calibration.py:114: error: Argument "zero_state_errors" to "SingleQubitReadoutCalibrationResult" has incompatible type "Dict[Qid, number[Any]]"; expected "Dict[Qid, float]"
cirq/experiments/single_qubit_readout_calibration.py:115: error: Argument "one_state_errors" to "SingleQubitReadoutCalibrationResult" has incompatible type "Dict[Qid, number[Any]]"; expected "Dict[Qid, float]"
cirq/experiments/random_quantum_circuit_generation.py:345: error: Name 'np.RandomState' is not defined
cirq/experiments/qubit_characterizations.py:248: error: Argument 1 to "RabiResult" has incompatible type "ndarray"; expected "Sequence[float]"
cirq/experiments/qubit_characterizations.py:248: error: Argument 2 to "RabiResult" has incompatible type "ndarray"; expected "Sequence[float]"
cirq/experiments/qubit_characterizations.py:308: error: Argument 2 to "RandomizedBenchMarkResult" has incompatible type "List[number[Any]]"; expected "Sequence[float]"
cirq/experiments/cross_entropy_benchmarking.py:229: error: Module has no attribute "polynomial"; maybe "Polynomial"?
cirq/circuits/qasm_output.py:46: error: Function "numpy.array" is not valid as a type
cirq/circuits/qasm_output.py:46: note: Perhaps you need "Callable[...]" or a callback protocol?
cirq/circuits/qasm_output.py:104: error: Function "numpy.array" is not valid as a type
cirq/circuits/qasm_output.py:104: note: Perhaps you need "Callable[...]" or a callback protocol?
cirq/sim/density_matrix_simulator.py:198: error: Incompatible types in assignment (expression has type "ndarray", target has type "List[ndarray]")
cirq/sim/density_matrix_simulator.py:307: error: Argument "measurements" to "DensityMatrixStepResult" has incompatible type "Dict[str, List[int]]"; expected "Dict[str, ndarray]"
cirq/sim/density_matrix_simulator.py:351: error: Argument 1 to "__init__" of "StepResult" has incompatible type "Dict[str, ndarray]"; expected "Optional[Dict[str, List[int]]]"
cirq/sim/density_matrix_simulator.py:518: error: Argument 2 to "reshape" has incompatible type "Tuple[number[Any], number[Any]]"; expected "Union[int, Sequence[int]]"
cirq/protocols/apply_unitary_protocol.py:261: error: Incompatible default for argument "default" (default has type "ndarray", argument has type "TDefault")
cirq/ops/linear_combinations.py:508: error: Argument "dtype" to "validate_normalized_state_vector" has incompatible type "dtype[Any]"; expected "Optional[Type[number[Any]]]"
cirq/ops/linear_combinations.py:565: error: Argument "dtype" to "to_valid_density_matrix" has incompatible type "dtype[Any]"; expected "Optional[Type[number[Any]]]"
cirq/circuits/circuit.py:2252: error: Incompatible return value type (got "Optional[ndarray]", expected "ndarray")
cirq/sim/state_vector.py:361: error: Incompatible return value type (got "Tuple[List[int], Optional[ndarray]]", expected "Tuple[List[int], ndarray]")
cirq/sim/state_vector.py:384: error: No overload variant of "range" matches argument type "number[Any]"
cirq/sim/state_vector.py:384: note: Possible overload variant:
cirq/sim/state_vector.py:384: note: def range(self, stop: int) -> range
cirq/sim/state_vector.py:384: note: <1 more non-matching overload not shown>
cirq/protocols/unitary_protocol.py:90: error: Incompatible default for argument "default" (default has type "ndarray", argument has type "TDefault")
cirq/protocols/unitary_protocol.py:203: error: Argument 1 to "reshape" of "_ArrayOrScalarCommon" has incompatible type "Tuple[number[Any], number[Any]]"; expected "Sequence[int]"
cirq/ops/pauli_interaction_gate.py:122: error: Incompatible types in assignment (expression has type "Iterable[Any]", variable has type "ndarray")
cirq/ops/pauli_interaction_gate.py:124: error: Argument "wire_symbols" to "CircuitDiagramInfo" has incompatible type "Tuple[ndarray, ndarray]"; expected "Iterable[str]"
cirq/google/optimizers/convert_to_sycamore_gates.py:109: error: Argument 2 to "unitary" has incompatible type "None"; expected "ndarray"
cirq/google/optimizers/convert_to_sycamore_gates.py:340: error: Argument 3 to "two_qubit_matrix_to_operations" has incompatible type "Operation"; expected "ndarray"
cirq/google/optimizers/convert_to_sycamore_gates.py:532: error: Argument 1 to "create_corrected_circuit" has incompatible type "Circuit"; expected "ndarray"
cirq/protocols/apply_mixture_protocol.py:161: error: Incompatible default for argument "default" (default has type "ndarray", argument has type "TDefault")
cirq/protocols/apply_mixture_protocol.py:266: error: Incompatible return value type (got "Optional[ndarray]", expected "Union[ndarray, TDefault]")
cirq/protocols/apply_channel_protocol.py:168: error: Incompatible default for argument "default" (default has type "ndarray", argument has type "TDefault")
cirq/sim/clifford/clifford_simulator.py:125: error: Return type "Dict[str, List[ndarray]]" of "_run" incompatible with return type "Dict[str, ndarray]" in supertype "SimulatesSamples"
cirq/sim/clifford/clifford_simulator.py:136: error: Incompatible types in assignment (expression has type "ndarray", target has type "List[ndarray]")
cirq/sim/clifford/clifford_simulator.py:149: error: Value expression in dictionary comprehension has incompatible type "ndarray"; expected type "List[ndarray]"
cirq/google/calibration/engine_simulator.py:317: error: Function "numpy.array" is not valid as a type
cirq/google/calibration/engine_simulator.py:317: note: Perhaps you need "Callable[...]" or a callback protocol?
cirq/experiments/fidelity_estimation.py:94: error: Incompatible return value type (got "number[Any]", expected "float")
cirq/experiments/fidelity_estimation.py:394: error: Incompatible types in assignment (expression has type "ndarray", variable has type "Sequence[float]")
cirq/experiments/fidelity_estimation.py:395: error: Incompatible types in assignment (expression has type "ndarray", variable has type "Sequence[float]")
cirq/experiments/fidelity_estimation.py:403: error: Argument 1 to "least_squares_xeb_fidelity_from_expectations" has incompatible type "List[number[Any]]"; expected "Sequence[float]"
cirq/experiments/fidelity_estimation.py:551: error: Incompatible types in assignment (expression has type "ndarray", variable has type "StateVectorTrialResult")
cirq/experiments/fidelity_estimation.py:552: error: Argument 1 to "__call__" of "ufunc" has incompatible type "StateVectorTrialResult"; expected "Union[Union[int, float, complex, str, bytes, generic], Sequence[Union[int, float, complex, str, bytes, generic]], Sequence[Sequence[Any]], _SupportsArray]"
cirq/experiments/fidelity_estimation.py:723: error: Incompatible types in assignment (expression has type "ndarray", variable has type "List[float]")
cirq/experiments/fidelity_estimation.py:729: error: Incompatible types in assignment (expression has type "ndarray", variable has type "List[List[float]]")
cirq/experiments/fidelity_estimation.py:731: error: Incompatible return value type (got "Tuple[List[List[float]], List[Any]]", expected "Tuple[ndarray, List[str]]")
cirq/experiments/grid_parallel_two_qubit_xeb.py:518: error: Argument "observed_probabilities" to "least_squares_xeb_fidelity_from_probabilities" has incompatible type "Tuple[ndarray, ...]"; expected "Sequence[Sequence[float]]"
cirq/experiments/grid_parallel_two_qubit_xeb.py:519: error: Argument "all_probabilities" to "least_squares_xeb_fidelity_from_probabilities" has incompatible type "Tuple[ndarray, ...]"; expected "Sequence[Sequence[float]]"
cirq/pasqal/pasqal_device.py:126: error: Incompatible types in assignment (expression has type "Union[bool_, ndarray]", variable has type "bool")
cirq/study/resolver_test.py:31: error: Module has no attribute "int"; maybe "uint", "rint", or "intp"?
cirq/study/resolver_test.py:37: error: Module has no attribute "complex"; maybe "complex_", "iscomplex", or "complex64"?
cirq/optimizers/decompositions_test.py:29: error: Argument 1 to "assert_allclose_up_to_global_phase" has incompatible type "Union[float, complex, ndarray]"; expected "ndarray"
cirq/linalg/diagonalize_test.py:59: error: Incompatible return value type (got "Tuple[Union[float, complex, ndarray], Union[float, complex, ndarray]]", expected "Tuple[ndarray, ndarray]")
cirq/ion/ion_decomposition_test.py:119: error: Function "numpy.array" is not valid as a type
cirq/ion/ion_decomposition_test.py:119: note: Perhaps you need "Callable[...]" or a callback protocol?
cirq/google/op_serializer_test.py:75: error: Module has no attribute "bool"; maybe "bool_" or "bool8"?
cirq/google/arg_func_langs_test.py:124: error: Module has no attribute "bool"; maybe "bool_" or "bool8"?
cirq/google/optimizers/two_qubit_gates/example.py:81: error: Incompatible types in assignment (expression has type "ndarray", variable has type "List[Any]")
cirq/google/optimizers/two_qubit_gates/example.py:82: error: Incompatible types in assignment (expression has type "ndarray", variable has type "List[Any]")
cirq/contrib/quimb/mps_simulator.py:160: error: Return type "Dict[str, List[ndarray]]" of "_run" incompatible with return type "Dict[str, ndarray]" in supertype "SimulatesSamples"
cirq/contrib/quimb/mps_simulator.py:183: error: Incompatible types in assignment (expression has type "ndarray", target has type "List[ndarray]")
cirq/contrib/quimb/mps_simulator.py:196: error: Value expression in dictionary comprehension has incompatible type "ndarray"; expected type "List[ndarray]"
cirq/contrib/quimb/grid_circuits.py:122: error: Argument 1 to "MatrixGate" has incompatible type "Union[float, complex, ndarray]"; expected "ndarray"
Found 133 errors in 63 files (checked 769 source files)
Cirq version
f08b344