-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Update sources for mypy-1.11.1 #6707
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
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. |
cirq-core/cirq/value/linear_dict.py
Outdated
@@ -34,7 +34,7 @@ | |||
) | |||
from typing_extensions import Self | |||
|
|||
Scalar = Union[complex, float, numbers.Complex] | |||
Scalar = complex |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this still cover floats and numpy complex numbers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack. I feel the current version makes it a bit more clear why is the attribute stored in a local var.
cirq-core/cirq/ops/projector.py
Outdated
@@ -61,7 +61,7 @@ def matrix(self, projector_qids: Optional[Iterable[raw_types.Qid]] = None) -> cs | |||
for qid in projector_qids | |||
] | |||
|
|||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! Done in b32f04e
@@ -175,7 +175,7 @@ def test_sample_no_indices_repetitions(): | |||
|
|||
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -36,6 +38,9 @@ | |||
from cirq.devices import grid_qubit | |||
from cirq.vis import vis_utils | |||
|
|||
if TYPE_CHECKING: | |||
from numpy.typing import ArrayLike |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thank you.
|
||
# packages with stub types for various libraries | ||
types-backports==0.1.3 | ||
types-cachetools |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the cachetools types no longer needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. cachetools are not imported after #6596.
- 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