Feature/issue 1399 switch to pyrefly - #1433
Conversation
|
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. |
There was a problem hiding this comment.
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.
| # limitations under the License. | ||
|
|
||
|
|
||
| def geometry_from_pubchem(name: str, structure: str = None): | ||
| def geometry_from_pubchem(name: str, structure: str | None = None): |
There was a problem hiding this comment.
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):There was a problem hiding this comment.
OpenFermion requires Python 3.10+, so str | None is fine here, will leave for now as-is, unless otherwise advised @mhucka
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.
b641ff4 to
788ba63
Compare
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.
|
Treating |
arettig
left a comment
There was a problem hiding this comment.
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.
| echo '::add-matcher::.github/problem-matchers/mypy.json' | ||
| check/mypy | ||
| echo '::add-matcher::.github/problem-matchers/typecheck.json' | ||
| check/typecheck --output-format github |
There was a problem hiding this comment.
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.
| echo -e -n "\033[0m" | ||
|
|
||
| exit ${result} | ||
| exec check/typecheck "$@" |
There was a problem hiding this comment.
@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' \ |
There was a problem hiding this comment.
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.
| unsupported-operation = false | ||
| invalid-inheritance = false | ||
| bad-override = false |
There was a problem hiding this comment.
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.
| "cirq", | ||
| "cirq.*", | ||
| "h5py", | ||
| "jax", | ||
| "jax.*", | ||
| "numpy.*", | ||
| "pandas", | ||
| "pandas.*", | ||
| "pubchempy", | ||
| "pybtas", | ||
| "pyscf", | ||
| "pyscf.*", | ||
| "pytest.*", | ||
| "scipy.*", | ||
| "sympy", | ||
| "sympy.*", | ||
| "tensorflow_docs", | ||
| "tensorflow_docs.*", |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
I get a warning about missing networkx types. You probably need to add types-networkx back in to typecheck.txt.
This switches mypy to pyrefly, matching the Cirq 8182 direction.
Migrated the old
[tool.mypy]settings withpyrefly init, then tightened the config so we checksrc/, 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, keptcheck/mypyas a deprecated wrapper, updatedcheck/all, CI, deps/env files, pre-commit, and CONTRIBUTING.Test plan
pyrefly check→ 0 errors locallyFixes #1399