Update sources for mypy-1.11.1#6707
Conversation
Use iterator to annotate generator functions.
The argument names in the replacement callable must match the original.
Address complaints on tuple-vs-ndarray and int vs np.int64.
Fix (wrong-import-position) and (unused-import) with a temporarily enabled pylint check.
Needed in modules importing pkg_resources.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6707 +/- ##
==========================================
- Coverage 97.83% 97.83% -0.01%
==========================================
Files 1077 1077
Lines 92502 92482 -20
==========================================
- Hits 90501 90480 -21
- Misses 2001 2002 +1 ☔ View full report in Codecov by Sentry. |
| from typing_extensions import Self | ||
|
|
||
| Scalar = Union[complex, float, numbers.Complex] | ||
| Scalar = complex |
There was a problem hiding this comment.
Does this still cover floats and numpy complex numbers?
There was a problem hiding this comment.
Good catch. int and float are OK for complex annotation per PEP-0484, but numpy types did not match. Turns out they also did not match in the main branch (2f922cd) with mypy-1.2.0, but it looks all is good after 00765f0 here.
Here is a test to check if numeric types pass -
# linear_dict_typing.py -- check with
# mypy --config-file=dev_tools/conf/mypy.ini linear_dict_typing.py
import numpy as np
import cirq
cirq.LinearDict({'a': 1})
cirq.LinearDict({'a': np.int32(32)})
cirq.LinearDict({'a': np.int64(64)})
cirq.LinearDict({'a': 2.0})
cirq.LinearDict({'a': np.float32(32.0)})
cirq.LinearDict({'a': np.float64(64.0)})
cirq.LinearDict({'a': complex(3j)})
cirq.LinearDict({'a': np.complex64(3j)})
cirq.LinearDict({'a': np.complex128(3j)})| @@ -191,7 +191,9 @@ def feed_op(could_be_binary: bool, token: Any) -> None: | |||
| elif token.unary_action is not None: | |||
There was a problem hiding this comment.
consider using a walrus operator here:
| elif token.unary_action is not None: | |
| elif (action := token.unary_action) is not None: |
Then lambda _, b: action(b) should work.
There was a problem hiding this comment.
Ack. I feel the current version makes it a bit more clear why is the attribute stored in a local var.
| ] | ||
|
|
||
| total_d = np.prod([qid.dimension for qid in projector_qids], dtype=np.int64) | ||
| total_d = np.prod([qid.dimension for qid in projector_qids], dtype=np.int64).item() |
There was a problem hiding this comment.
consider using math.prod instead
| total_d = np.prod([qid.dimension for qid in projector_qids], dtype=np.int64).item() | |
| total_d = math.prod(qid.dimension for qid in projector_qids) |
| @pytest.mark.parametrize('use_np_transpose', [False, True]) | ||
| def test_measure_state_computational_basis(use_np_transpose: bool): | ||
| linalg.can_numpy_support_shape = lambda s: use_np_transpose | ||
| linalg.can_numpy_support_shape = lambda shape: use_np_transpose |
There was a problem hiding this comment.
nit: use _shape instead since the arg is unused?
| linalg.can_numpy_support_shape = lambda shape: use_np_transpose | |
| linalg.can_numpy_support_shape = lambda _shape: use_np_transpose |
Same thing for several other places below.
There was a problem hiding this comment.
The actual mypy complaint was about the replacement function not matching the previous signature as the can_numpy_support_shape has shape argument. Looking at it again, this test overwrites cirq.linalg. can_numpy_support_shape without restoring it back.
This is fixed in f7da161.
| from cirq.vis import vis_utils | ||
|
|
||
| if TYPE_CHECKING: | ||
| from numpy.typing import ArrayLike |
There was a problem hiding this comment.
Can't do this in an if TYPE_CHECKING block unless you also make annotations lazy by doing from __future__ import annotations.
There was a problem hiding this comment.
Done, thank you.
|
|
||
| # packages with stub types for various libraries | ||
| types-backports==0.1.3 | ||
| types-cachetools |
There was a problem hiding this comment.
Are the cachetools types no longer needed?
There was a problem hiding this comment.
Indeed. cachetools are not imported after #6596.
Co-authored-by: Matthew Neeley <mneeley@gmail.com>
- typecheck - purge unused ignore comments - typecheck - address complaints about _ComplexLike expressions - typecheck - fix errors on missing return statement - typecheck - tell mypy it has a callable in inner function - typecheck - fix redundant cast to "str" - typecheck - fix type annotation to kwargs to dataclasses.replace - typecheck - fix signature of the replacement function and restore the original function after the test - typecheck - tell mypy about type when extracted from a zip tuple - typecheck - fix incompatible types in assignment - typecheck - fix type complaint due to unnecessary use of functools.partial - Pick up easy lint from the cirq_google/cloud file - Excise unused stub dependencies in mypy.txt Fixes quantumlib#6705
- typecheck - purge unused ignore comments - typecheck - address complaints about _ComplexLike expressions - typecheck - fix errors on missing return statement - typecheck - tell mypy it has a callable in inner function - typecheck - fix redundant cast to "str" - typecheck - fix type annotation to kwargs to dataclasses.replace - typecheck - fix signature of the replacement function and restore the original function after the test - typecheck - tell mypy about type when extracted from a zip tuple - typecheck - fix incompatible types in assignment - typecheck - fix type complaint due to unnecessary use of functools.partial - Pick up easy lint from the cirq_google/cloud file - Excise unused stub dependencies in mypy.txt Fixes quantumlib#6705
Fixes #6705