Skip to content

Feature/issue 1399 switch to pyrefly - #1433

Open
rosspeili wants to merge 4 commits into
quantumlib:mainfrom
rosspeili:feature/issue-1399-switch-to-pyrefly
Open

Feature/issue 1399 switch to pyrefly#1433
rosspeili wants to merge 4 commits into
quantumlib:mainfrom
rosspeili:feature/issue-1399-switch-to-pyrefly

Conversation

@rosspeili

@rosspeili rosspeili commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

This switches mypy to pyrefly, matching the Cirq 8182 direction.

Migrated the old [tool.mypy] settings with pyrefly init, then tightened the config so we check src/, skip notebooks/docs, and ignore heavy third-party imports the way CI effectively was doing with mypy. The first pass flagged a lot of Cirq/sympy noise and missing optional deps, but local mypy was noisy too without the full CI env, so aimed at the same practical bar as CI rather than matching a bare local mypy run.

After, some annotation issues were left (Optional defaults, bool returns, Expr typing), which are fixed in a few source files, and a couple of Cirq/sympy-only error kinds stay disabled in config for now so the migration stays green without a large Cirq typing follow-up.

Wiring: added check/typecheck, kept check/mypy as a deprecated wrapper, updated check/all, CI, deps/env files, pre-commit, and CONTRIBUTING.

Test plan

  • pyrefly check → 0 errors locally
  • Black clean on touched Python files
  • CI green on this PR

Fixes #1399

@google-cla

google-cla Bot commented Jul 25, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

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.

Code Review

This pull request migrates the project's type-checking tool from mypy to Pyrefly, updating configuration files, dependencies, CI workflows, and documentation accordingly. It also resolves several type annotation issues across the codebase. Feedback on the changes highlights that the use of the | union operator in src/openfermion/chem/pubchem.py requires from __future__ import annotations to prevent runtime errors on Python versions older than 3.10. Additionally, it is recommended to use a trap in the new check/typecheck script to guarantee that terminal colors are reset even if the execution is interrupted.

Comment on lines 11 to +14
# limitations under the License.


def geometry_from_pubchem(name: str, structure: str = None):
def geometry_from_pubchem(name: str, structure: str | None = 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.

high

The use of the PEP 604 union operator | (e.g., str | None) in type annotations is only supported at runtime in Python 3.10 or newer. If this codebase supports Python 3.9 or older, this will raise a TypeError at import time unless from __future__ import annotations is imported at the top of the file. To prevent runtime errors on older Python versions, please add from __future__ import annotations at the beginning of the file.

#   limitations under the License.
from __future__ import annotations


def geometry_from_pubchem(name: str, structure: str | None = None):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

OpenFermion requires Python 3.10+, so str | None is fine here, will leave for now as-is, unless otherwise advised @mhucka

Comment thread check/typecheck
Replace mypy with Pyrefly via check/typecheck, migrate config, fix a few annotations, and update CI, deps, and docs. Keep check/mypy as a deprecated wrapper.
@rosspeili
rosspeili force-pushed the feature/issue-1399-switch-to-pyrefly branch from b641ff4 to 788ba63 Compare July 25, 2026 12:31
Use EXIT trap so Ctrl+C does not leave the shell stuck in red after pyrefly.
This PR is only the Pyrefly migration; the workflow is not included here.
python-checks installs pylint.env.txt which includes pyscf; Pyrefly then rejects scf.HF-style annotations as not-a-type. replace-imports-with-any keeps typecheck stable whether or not pyscf is installed.
@rosspeili

Copy link
Copy Markdown
Contributor Author

Treating pyscf as Any via replace-imports-with-any, hoping CI typecheck no longer fails on scf.HF-style annotations when pyscf is installed via pylint.env.txt.

@arettig arettig left a comment

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.

Thanks for doing this! I was able to run pyrefly and it seemed quicker than mypy!

I went ahead and did a partial review. It is worth waiting for @mhucka to take a look at this too though, as he's the expert here.

Some comments and questions below. Mostly I am worried we will have lowered coverage due to the 3 globally disabled settings.

Comment thread .github/workflows/ci.yaml
Comment on lines -100 to +101
echo '::add-matcher::.github/problem-matchers/mypy.json'
check/mypy
echo '::add-matcher::.github/problem-matchers/typecheck.json'
check/typecheck --output-format github

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.

I suspect we don't need the matcher here since pyrefly has a --output-format github flag. If so, you could delete the typecheck.json.

Comment thread check/mypy
echo -e -n "\033[0m"

exit ${result}
exec check/typecheck "$@"

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.

@mhucka Do we want to fully remove mypy support immediately? We could alternatively leave it in for now and just print the deprecation warning if anyone runs it. This would make it easy to run both and ensure that pyrefly isn't lowering our coverage for now.

--hash=sha256:fbccdc05410c9ee21bbf16a35f4c1d16123dcdeb8a1d38f33654fa21d0234f79 \
--hash=sha256:fea24543955a6a729c45a73fe90e08c743f0b3334bbf3201e6c4bc1b0c7fa464
# via requests
cirq-core==1.5.0 ; python_full_version < '3.11' \

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.

It looks like support for python 3.10 was removed, you probably need to rerun the ./dev_tools/requirements/create-env-files.sh script with the older python version to fix.

Comment thread pyproject.toml
Comment on lines +87 to +89
unsupported-operation = false
invalid-inheritance = false
bad-override = false

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.

I'm assuming these are turned off as pyrefly is more strict than mypy and we get a bunch of errors? Disabling them seems like it will result in less coverage than mypy.

Comment thread pyproject.toml
Comment on lines +58 to +75
"cirq",
"cirq.*",
"h5py",
"jax",
"jax.*",
"numpy.*",
"pandas",
"pandas.*",
"pubchempy",
"pybtas",
"pyscf",
"pyscf.*",
"pytest.*",
"scipy.*",
"sympy",
"sympy.*",
"tensorflow_docs",
"tensorflow_docs.*",

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.

Why are all of these modules added? A lot of these are mandatory dependencies so I assume they are never missing.

pandas-stubs
types-requests
types-setuptools
types-networkx

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.

I get a warning about missing networkx types. You probably need to add types-networkx back in to typecheck.txt.

@arettig arettig removed their assignment Jul 30, 2026
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.

Switch to Pyrefly for type checking instead of mypy

2 participants