Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ jobs:
with:
python-version: '3.11'
architecture: 'x64'
- name: Install mypy
run: pip install -r dev_tools/requirements/mypy.env.txt
- name: Install type checker
run: pip install -r dev_tools/requirements/typecheck.env.txt
- name: Type check
run: FORCE_COLOR=1 check/typecheck
changed_files:
Expand Down
2 changes: 1 addition & 1 deletion check/all
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ for arg in "$@"; do
--apply-format-changes)
echo "Warning: option '--apply-format-changes' is deprecated." \
"Please use '--fix' instead." >&2
echo "Warning: The '--apply-format-changes' will be removed in cirq v1.8." >&2
echo "Warning: The '--apply-format-changes' option will be removed in cirq v1.8." >&2
echo >&2
extra_format_args=( --apply )
;;
Expand Down
1 change: 1 addition & 0 deletions check/shellcheck
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ required_shell_scripts=(
check/all
check/doctest
check/format-incremental
check/mypy
check/nbformat
check/pylint
check/pytest
Expand Down
13 changes: 11 additions & 2 deletions check/typecheck
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env bash

################################################################################
# Runs type checking on the repository using the mypy tool.
# Runs type checking on the repository using mypy and Pyrefly.
#
# Usage:
# check/typecheck [flags-for-mypy]
# check/typecheck [--flags]
################################################################################

# Get the working directory to the repo root.
Expand All @@ -13,4 +13,13 @@ repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel) || exit $?
cd "${repo_dir}" || exit $?

source dev_tools/pypath || exit $?

mypy "$@" .

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.

Please update to run both mypy and typecheck, for example,

mypy "$@" . && pyrefly check

For now we can ignore the command-line arguments for pyrefly and add them there after dropping mypy.

mypy_result=$?
if (( mypy_result != 0 )); then
exit "${mypy_result}"
fi

pyrefly check
result=$?
exit "${result}"
2 changes: 1 addition & 1 deletion cirq-core/cirq/sim/density_matrix_simulation_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def create(
).reshape(qid_shape * 2)
else:
if qid_shape is not None:
if dtype and initial_state.dtype != dtype:
if dtype is not None and initial_state.dtype != dtype:
initial_state = initial_state.astype(dtype)
density_matrix = qis.to_valid_density_matrix(
initial_state, len(qid_shape), qid_shape=qid_shape, dtype=dtype
Expand Down
2 changes: 1 addition & 1 deletion dev_tools/requirements/deps/dev-tools.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-r mypy.txt
-r typecheck.txt
-r pytest.txt
-r format.txt
-r pylint.txt
Expand Down
6 changes: 0 additions & 6 deletions dev_tools/requirements/deps/mypy.txt

This file was deleted.

7 changes: 7 additions & 0 deletions dev_tools/requirements/deps/typecheck.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Type checking tools (mypy during transition; Pyrefly with baseline).
mypy==2.1.0
pyrefly==1.2.0

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.

Please add mypy==2.1.0 back here.


# Packages with stub types for various libraries
types-protobuf>=6.32.1, <8.0
types-requests~=2.32
6 changes: 0 additions & 6 deletions dev_tools/requirements/mypy.env.txt

This file was deleted.

5 changes: 5 additions & 0 deletions dev_tools/requirements/typecheck.env.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Full typecheck environment with Cirq modules installed.
# Used by CI Type check job.

-r deps/cirq-all.txt
-r deps/typecheck.txt
80 changes: 80 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ extend_skip = ["__init__.py"]
extend_skip_glob = ["*_pb2.py"]



[tool.mypy]
exclude = [
'/setup\.py',
Expand Down Expand Up @@ -342,3 +343,82 @@ select = [

[tool.ruff.lint.per-file-ignores]
"*.ipynb" = ["E402", "PLW0406"]

# Migrated from [tool.mypy] via `pyrefly init`. Cirq is a multi-package
# monorepo; search-path mirrors `source dev_tools/pypath` for imports.
[tool.pyrefly]
preset = "legacy"
# Match mypy defaults: do not aggressively check unannotated defs.
check-unannotated-defs = false
infer-return-types = "never"
# Respect leftover `# type: ignore` comments during migration.
permissive-ignores = true
disable-search-path-heuristics = true
search-path = [
".",
"cirq-aqt",
"cirq-core",
"cirq-google",
"cirq-ionq",
"cirq-pasqal",
"cirq-web",
]
project-excludes = [
"**/setup.py",
"**/cirq-google/cirq_google/cloud/**",
"**/*.ipynb",
"**/node_modules/**",
Comment on lines +367 to +370

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.

Are all of these needed? Please keep only the patterns which may match some files in the search-path above.

]
baseline = "pyrefly-baseline.json"
ignore-missing-imports = [
"__main__",
"google.api_core.*",
"google.auth.*",
"google.cloud.*",
"google.colab.*",
"google.oauth2.*",
"google.protobuf.*",
"qsimcirq",
"IPython.*",
"ipython.*",
"_pytest.*",
"absl.*",
"astroid.*",
"cachetools.*",
"filelock.*",
"ipywidgets.*",
"matplotlib.*",
"mpl_toolkits.*",
"networkx.*",
"pandas.*",
"ply.*",
"proto.*",
"pylatex.*",
"pylint.*",
"pytest.*",
"qiskit.*",
"quimb.*",
"ruamel.*",
"scipy.*",
"setuptools.*",
"stim",
"stimcirq.*",
"sympy.*",
"tensorflow_docs.*",
"tqdm.*",
]
# Protobuf / heavy stub-poor libs: mypy used follow_imports=silent/skip.
replace-imports-with-any = [
"google.protobuf",
"google.protobuf.*",
"sympy",
"sympy.*",
"networkx",
"networkx.*",
"pandas",
"pandas.*",
]

[tool.pyrefly.errors]

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.

This would essentially disable typechecking. For consistency with our internal config, please use

redundant-cast = "error"
unused-type-ignore = "error"

Instead of disabling most errors, we should generate and commit the "pyrefly-baseline.json" file following the advice at https://pyrefly.org/en/docs/migrating-from-mypy/#3-handle-the-new-errors-and-drop-mypy. When pyrefly checks against the baseline file, it will flag typing errors in any new code. We can then gradually work on fixing the errors in the baseline file so it is eventually not necessary and can be removed. At that point we can drop mypy and use pyrefly only.

redundant-cast = "error"
unused-type-ignore = "error"
Loading
Loading