Skip to content

Update sources for mypy-1.11.1#6707

Merged
pavoljuhas merged 20 commits intoquantumlib:mainfrom
pavoljuhas:sync-with-mypy-1.11.1
Aug 23, 2024
Merged

Update sources for mypy-1.11.1#6707
pavoljuhas merged 20 commits intoquantumlib:mainfrom
pavoljuhas:sync-with-mypy-1.11.1

Conversation

@pavoljuhas
Copy link
Copy Markdown
Collaborator

  • 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
  • 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 #6705

@codecov
Copy link
Copy Markdown

codecov bot commented Aug 21, 2024

Codecov Report

Attention: Patch coverage is 98.96907% with 1 line in your changes missing coverage. Please review.

Project coverage is 97.83%. Comparing base (2f922cd) to head (963b558).
Report is 2 commits behind head on main.

Files Patch % Lines
cirq-core/cirq/vis/heatmap.py 75.00% 1 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

@pavoljuhas pavoljuhas marked this pull request as ready for review August 21, 2024 01:19
Comment thread cirq-core/cirq/value/linear_dict.py Outdated
from typing_extensions import Self

Scalar = Union[complex, float, numbers.Complex]
Scalar = complex
Copy link
Copy Markdown
Collaborator

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?

Copy link
Copy Markdown
Collaborator Author

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:
Copy link
Copy Markdown
Contributor

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:

Suggested change
elif token.unary_action is not None:
elif (action := token.unary_action) is not None:

Then lambda _, b: action(b) should work.

Copy link
Copy Markdown
Collaborator Author

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.

Comment thread cirq-core/cirq/ops/projector.py Outdated
]

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()
Copy link
Copy Markdown
Contributor

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

Suggested change
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)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Done in b32f04e

Comment thread cirq-core/cirq/sim/state_vector_test.py Outdated
@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
Copy link
Copy Markdown
Contributor

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?

Suggested change
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.

Copy link
Copy Markdown
Collaborator Author

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.

from cirq.vis import vis_utils

if TYPE_CHECKING:
from numpy.typing import ArrayLike
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Collaborator Author

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
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Collaborator Author

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.

Comment thread cirq-ionq/cirq_ionq/ionq_gateset.py Outdated
@pavoljuhas pavoljuhas requested a review from maffoo August 23, 2024 17:06
@pavoljuhas pavoljuhas merged commit 6037f18 into quantumlib:main Aug 23, 2024
@pavoljuhas pavoljuhas deleted the sync-with-mypy-1.11.1 branch August 23, 2024 20:24
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
- 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
BichengYing pushed a commit to BichengYing/Cirq that referenced this pull request Jun 20, 2025
- 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
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.

Update sources to pass typecheck with mypy==1.11.1

3 participants