quantumlib / Cirq

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.
Apache License 2.0
4.29k stars 1.02k forks source link

Numpy 1.20 includes typing information, breaks mypy checks locally #3767

Closed kevinsung closed 2 years ago

kevinsung commented 3 years ago

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 f08b3444004e44b6c0cebf0ad4136775f8decdf9

kevinsung commented 3 years ago

Probably a Python 3.9 issue.

balopat commented 3 years ago

I just started seeing this and was wondering whether it's only my machine. I'm on python 3.8 but I've seen it on python 3.7. I wonder why the CI is not catching it.

mpharrigan commented 3 years ago

This is because of the release of numpy 1.20, which added type annotations

mpharrigan commented 3 years ago

This isn't picked up by the CI since we don't install cirq or its requirements during the mypy check

mpharrigan commented 3 years ago
diff --git a/dev_tools/conf/mypy.ini b/dev_tools/conf/mypy.ini
index f931717b..6fbc7276 100644
--- a/dev_tools/conf/mypy.ini
+++ b/dev_tools/conf/mypy.ini
@@ -5,10 +5,15 @@ follow_imports = silent
 ignore_missing_imports = true

 # 3rd-party libs for which we don't have stubs
-[mypy-apiclient.*,freezegun.*,matplotlib.*,mpl_toolkits,multiprocessing.dummy,numpy.*,oauth2client.*,pandas.*,pytest.*,scipy.*,sortedcontainers.*,setuptools.*,pylatex.*,networkx.*,qiskit.*,pypandoc.*,ply.*,_pytest.*,google.api.*,google.api_core.*,grpc.*,google.oauth2.*,google.protobuf.text_format.*,quimb.*,pyquil.*,google.cloud.*,filelock.*,codeowners.*,tqdm.*]
+[mypy-apiclient.*,freezegun.*,matplotlib.*,mpl_toolkits,multiprocessing.dummy,oauth2client.*,pandas.*,pytest.*,scipy.*,sortedcontainers.*,setuptools.*,pylatex.*,networkx.*,qiskit.*,pypandoc.*,ply.*,_pytest.*,google.api.*,google.api_core.*,grpc.*,google.oauth2.*,google.protobuf.text_format.*,quimb.*,pyquil.*,google.cloud.*,filelock.*,codeowners.*,tqdm.*]
 follow_imports = silent
 ignore_missing_imports = true

+# There was no type information before numpy 1.20
+[mypy-numpy.*]
+follow_imports = skip
+follow_imports_for_stubs = true
+
 #Adding "sympy.* or mypy-sympy to the above list (3rd-party libs for which we don't have stubs) doesn't ignore "cannot find module 'sympy' error
 [mypy-sympy.*]
 ignore_missing_imports = True

silences it

balopat commented 3 years ago

Thanks for the research on this and silencing tip @mpharrigan! I believe that we should slowly chip away at this though - there is a reason why we have mypy - thus if we believe in the usefulness of type checking, we are behooved to follow through with fixing these issues and then turn it on on CI (by installing the correct requirements).

mpharrigan commented 3 years ago

What's the plan for this? I'm getting agita from having my uncomitted mypy.ini change hanging out on all my branches so I can actually run mypy locally without being drowned by numpy errors.

maffoo commented 3 years ago

FWIW we've internally used the follow_imports_for_stubs = true config that you mentioned above to allow us to update to numpy 1.20 and then incrementally fix type annotations. We have a "mypy-next" CI job that runs mypy with a different configuration where we do follow stub imports; errors in that job do not fail the build but we can look at them in the log so we can see what's left to do. We've also used this mypy-next scheme to incrementally fix the code base as we change various mypy flags.

balopat commented 3 years ago

+1 to that approach - @mpharrigan do you want to implement it?

mpharrigan commented 3 years ago

Do you have a failing check and have github ignore the result or do you put || true in the check, i.e. always have it report success and rely on manually inspecting the log to see what's really going on?

mpharrigan commented 3 years ago

3995 updates the CI. Of course: there is still the issue of fixing all the numpy problems

mpharrigan commented 3 years ago

Changing to kind/health instead of kind/bug-report since nothing is currently broken

dabacon commented 3 years ago

Run check/mypy --next Success: no issues found in 966 source files

Looks like @mpharrigan squashed all the bugs. Closing.

mpharrigan commented 3 years ago

I'm pretty sure there are a ton of (num+my)py bugs left. check/mypy --next assumes you have numpy >= 1.20 or it won't do the new numpy checks.

In any event, if it is fixed, we'd need to modify the CI to only do the check/mypy --next behavior (and remove the flag, probably)

mpharrigan commented 3 years ago

Having said that, I see that check/mypy --next doesn't find anything on my machine.

mpharrigan commented 3 years ago

broken here: https://github.com/quantumlib/Cirq/commit/fd257e6c228a0b4c8839ad6ac3d30467476f195e#diff-8cceb31ef3d58deea4a1c9df3e3177ce3aa6a18be536905e60d4b14e230e3150

maffoo commented 2 years ago

What is the goal for before-1.0 here? I made progress on fixing the numpy errors in check/mypy --next in https://github.com/quantumlib/Cirq/pull/5227, but if we want to fix all the remaining ~100 errors it's going to be quite a bit of work. This seems like something that could reasonably be postponed until after 1.0, though I do think it'd be good to continue to push on this so we can enable mypy checking of numpy calls eventually.

dstrain115 commented 2 years ago

Discussion from cirq cync: this is a nice to have for cirq 1.0, since typing changes could theoretically change the API interface, but these would likely be small changes and may not materialize. We will keep it on before 1.0 for now, but if we run out of time, this is a good candidate to cut.