@@ -614,14 +614,19 @@ recommended that you make an attempt to support type checking through your
614614public API in the best way that you can (based on your supported Python
615615versions). Stub files can be used instead for out-of-line typing.
616616[MyPy](https://mypy.readthedocs.io/en/stable/) is suggested for type checking,
617- though there are several other good options to try, as well. If you have
617+ though there are several other good options to try, as well;
618+ [Pyrefly](https://pyrefly.org) is a fast Rust-based checker, and is shown
619+ alongside MyPy in the tabs below. If you have
618620built-in support for type checking, you need to add empty `py.typed` files to
619621all packages/subpackages to indicate that you support it.
620622
621623Read more about type checking on the [dedicated page][mypy page].
622624
623- The MyPy addition for pre-commit :
625+ The pre-commit addition for type checking :
624626
627+ ::::{tab-set}
628+ :::{tab-item} MyPy
629+ :sync : mypy
625630` ` ` yaml
626631- repo: https://github.com/pre-commit/mirrors-mypy
627632 rev: "v2.1.0"
@@ -638,7 +643,27 @@ add items to the virtual environment setup for MyPy by pre-commit, for example:
638643` ` ` yaml
639644additional_dependencies: [attrs==23.1.0]
640645` ` `
646+ :: :
647+ :::{tab-item} Pyrefly
648+ :sync : pyrefly
649+ ` ` ` yaml
650+ - repo: https://github.com/facebook/pyrefly-pre-commit
651+ rev: "1.1.1"
652+ hooks:
653+ - id: pyrefly-check
654+ ` ` `
641655
656+ This hook runs `pyrefly check`, reading its configuration from `pyproject.toml`
657+ (shown below). Like MyPy, you can add packages to the type-checking environment
658+ with `additional_dependencies`, and pass extra flags via `args`.
659+ :: :
660+ ::: :
661+
662+ Both checkers read their configuration from `pyproject.toml` :
663+
664+ ::::{tab-set}
665+ :::{tab-item} MyPy
666+ :sync : mypy
642667{rr}`MY100` MyPy has a config section in `pyproject.toml` that looks like
643668this :
644669
@@ -673,6 +698,40 @@ and `ignore-without-code` {rr}`MY104`, `redundant-expr` {rr}`MY105`, and
673698` truthy-bool` {rr}`MY106` can trigger too often (like on `sys.platform`
674699checks) and have to be ignored occasionally, but can find some significant logic
675700errors in your typing.
701+ :: :
702+ :::{tab-item} Pyrefly
703+ :sync : pyrefly
704+ Pyrefly's config section in `pyproject.toml` looks like this :
705+
706+ ` ` ` toml
707+ [tool.pyrefly]
708+ project-includes = ["src"]
709+ python-version = "3.10"
710+ project-excludes = ["**/tests"]
711+
712+ # Use ` Any` for imports that can't be resolved, one entry per module
713+ ignore-missing-imports = ["numpy"]
714+ ```
715+
716+ Pyrefly checks all your code by default, rather than only annotated functions,
717+ so you can start from a looser ` preset ` and tighten as you go. Set
718+ ` preset = "strict" ` (the equivalent of MyPy's ` strict = true ` ) once you are
719+ ready, and toggle individual error codes in a ` [tool.pyrefly.errors] ` table, for
720+ example:
721+
722+ ``` toml
723+ [tool .pyrefly ]
724+ preset = " strict"
725+
726+ # Turn a specific check off
727+ errors.implicit-import = false
728+ ```
729+
730+ You can disable Pyrefly on a line with ` # pyrefly: ignore ` (or
731+ ` # pyrefly: ignore[error-name] ` for a specific code); the standard
732+ ` # type: ignore ` is honored as well.
733+ :::
734+ ::::
676735
677736[ mypy page ] : guides/mypy
678737
0 commit comments