diff --git a/.dagger/modules/e2e/dagger-module.toml b/.dagger/modules/e2e/dagger-module.toml index 61c2aa2..aa356b3 100644 --- a/.dagger/modules/e2e/dagger-module.toml +++ b/.dagger/modules/e2e/dagger-module.toml @@ -12,6 +12,10 @@ engineVersion = "v1.0.0-beta.15" name = "pyproject" source = "../../../pyproject" +[[dependencies]] + name = "pyrefly" + source = "../../../pyrefly" + [[dependencies]] name = "pytest" source = "../../../pytest" diff --git a/.dagger/modules/e2e/main.dang b/.dagger/modules/e2e/main.dang index 2ec565e..2c41fc5 100644 --- a/.dagger/modules/e2e/main.dang +++ b/.dagger/modules/e2e/main.dang @@ -483,6 +483,7 @@ type E2e { let nativeFixture: String! = "testdata/project-native" let ruffRangeFixture: String! = "testdata/project-ruff-range" let ruffRangeOldFixture: String! = "testdata/project-ruff-range-old" + let pyreflyExcludesFixture: String! = "testdata/project-pyrefly-excludes" """ Fixtures that are broken on purpose, left out so the batch checks @@ -500,6 +501,8 @@ type E2e { "!" + excludedFixture, # A path source in it holds a type error, checked with the project. "!" + uvWorkspaceFixture, + # Its own Pyrefly config keeps out a type error the other checkers see. + "!" + pyreflyExcludesFixture, ] let mypyFailure(ws: Workspace!, path: String!): String! { @@ -510,6 +513,19 @@ type E2e { failureOf(ty(scope: ["**"]).project(ws, path).typeCheck(ws)) } + """ + Pyrefly under its default preset. A project with no Pyrefly config gets the + basic preset, which reports no type mismatch, so a check that must see one + asks for the default preset. + """ + let pyreflyDefault: Pyrefly! { + pyrefly(scope: ["**"], args: ["--preset", "default"]) + } + + let pyreflyFailure(ws: Workspace!, path: String!): String! { + failureOf(pyreflyDefault.project(ws, path).typeCheck(ws)) + } + """ mypy checks a healthy workspace, really fails on a type error, and skips a project with no Python source of its own. @@ -559,6 +575,7 @@ type E2e { # The child's module is mistyped; the parent must still pass. run(mypy.project(ws, nestedFixture).typeCheck(ws)) run(ty.project(ws, nestedFixture).typeCheck(ws)) + run(pyreflyDefault.project(ws, nestedFixture).typeCheck(ws)) let childFailure = mypyFailure(ws, nestedChildFixture) assert( @@ -578,6 +595,12 @@ type E2e { "ty over-matched a dotted nested project exclusion: " + tyMetachar, ) + let pyreflyMetachar = pyreflyFailure(ws, metacharFixture) + assert( + pyreflyMetachar.contains("subXv2/mistyped.py"), + "pyrefly over-matched a dotted nested project exclusion: " + pyreflyMetachar, + ) + let nestedFailure = mypyFailure(ws, metacharNestedFixture) assert( nestedFailure.contains("exit code"), @@ -600,6 +623,53 @@ type E2e { null } + """ + Pyrefly reaches the same verdicts as mypy and ty under its default preset, + and otherwise checks a project the way the project's own config says. + """ + pyreflyCheck(ws: Workspace!): Void @check { + run(pyrefly(scope: healthy).projects(ws).batch.typeCheck(ws)) + + let failure = pyreflyFailure(ws, typeFailFixture) + assert( + failure.contains("exit code"), + "pyrefly did not fail on a type error: " + failure, + ) + # With no Pyrefly config, the basic preset applies, as for `pyrefly check`. + run(pyrefly.project(ws, typeFailFixture).typeCheck(ws)) + + let subset = [passFixture, typeFailFixture, nestedChildFixture] + assertReports( + failureOf(pyreflyDefault.projects(ws).subset(keys: subset).batch.typeCheck(ws)), + [typeFailFixture, nestedChildFixture], + [passFixture], + ) + + # The basic preset still reports an unresolved import, so passing under it + # means the imports of path sources resolve in the project's environment. + run(pyrefly.project(ws, uvWorkspaceFixture).typeCheck(ws)) + let workspace = pyreflyFailure(ws, uvWorkspaceFixture) + assert( + workspace.contains("broken.py"), + "a path source's type error was not the project's to report: " + workspace, + ) + + # The project has a nested project, so a --project-excludes for it would + # replace the configured list and check generated/, which is mistyped. + let configured = pyrefly.project(ws, pyreflyExcludesFixture) + assert(configured.configuresExcludes(ws), "pyrefly missed project-excludes in [tool.pyrefly]") + assert( + pyrefly.project(ws, pyreflyExcludesFixture + "/nested").configuresExcludes(ws), + "pyrefly missed project_excludes in pyrefly.toml", + ) + assert( + pyrefly.project(ws, passFixture).configuresExcludes(ws) == false, + "pyrefly saw project-excludes in a project that sets none", + ) + run(configured.typeCheck(ws)) + null + } + """ A tool runs the version the project pins, and its own default otherwise. """ @@ -621,6 +691,10 @@ type E2e { ty.versionFor(ws, passFixture) == ty.defaultVersion, "ty did not fall back to its own default", ) + assert( + pyrefly.versionFor(ws, passFixture) == pyrefly.defaultVersion, + "pyrefly did not fall back to its own default", + ) assert( mypy.project(ws, pinnedFixture).version(ws) == "1.11.2", "a mypy project did not report the version pinned in uv.lock", diff --git a/AGENTS.md b/AGENTS.md index 1c58fff..55f1410 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -3,10 +3,11 @@ One repository, one module per directory: - `pyproject/` — the shared library. It never gets `@check` functions. -- `ruff/`, `pytest/`, `uv/`, `mypy/`, `ty/` — tool modules, each with its own - checks. `ruff/` never runs through uv; it is a standalone binary. Each one's - `projects` is a Dagger collection keyed by project root: checks live on the - item type, and the collection's batch functions run them over a selection. +- `ruff/`, `pytest/`, `uv/`, `mypy/`, `ty/`, `pyrefly/` — tool modules, each + with its own checks. `ruff/` never runs through uv; it is a standalone + binary. Each one's `projects` is a Dagger collection keyed by project root: + checks live on the item type, and the collection's batch functions run them + over a selection. - `.dagger/modules/e2e` — one end-to-end suite covering every module. - `testdata/` — fixture projects shared by the suite. Several fail on purpose. diff --git a/README.md b/README.md index caff0ad..97b9eb0 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ github.com/dagger/python ├── uv/ lock, build, audit ├── mypy/ type check ├── ty/ type check +├── pyrefly/ type check └── .dagger/ one end-to-end suite for all of them ``` @@ -33,6 +34,7 @@ dagger install github.com/dagger/python/pytest # tests dagger install github.com/dagger/python/uv # lock, build, audit dagger install github.com/dagger/python/mypy # type check dagger install github.com/dagger/python/ty # type check, Astral's checker +dagger install github.com/dagger/python/pyrefly # type check, Meta's checker ``` `pyproject` is a library, not a tool. You depend on it when you write a module; @@ -46,26 +48,27 @@ library; see "Relationship to the standalone modules" below. Each tool module's `projects` is a Dagger collection keyed by project root, so it adds a dimension — `ruff-project`, `pytest-project`, `mypy-project`, -`ty-project`, `uv-project` — that checks and generators select on. pytest adds a -second one under it, `pytest-test-file`, keyed by test file. For a repository -with projects `app` and `libs/core`: +`ty-project`, `pyrefly-project`, `uv-project` — that checks and generators +select on. pytest adds a second one under it, `pytest-test-file`, keyed by test +file. For a repository with projects `app` and `libs/core`: ```console $ dagger list ruff-projects -a $ dagger list pytest-test-files --pytest-project=app -a $ dagger check --ruff --check lint --ruff-project=app -$ dagger check --check type-check # mypy and ty together +$ dagger check --check type-check # mypy, ty and pyrefly together $ dagger check --pytest --pytest-project=app --pytest-test-file=tests/test_api.py $ dagger generate --ruff --ruff-project=app # ruff format $ dagger check -l --all --ruff -f=cli # one line per project, as flags ``` `dagger check --help` lists the flags in effect: `--ruff-project`, -`--pytest-project`, `--pytest-test-file`, `--mypy-project`, `--ty-project` and -`--uv-project` select keys (`--ruff-projects`, `--pytest-tests` and so on select -a whole dimension); `--ruff`, `--pytest`, `--mypy`, `--ty` and `--uv` select a -module; and `--check ` selects a check by name across modules: `lint`, -`test`, `type-check`, `audit`, or `stale` for the generators' staleness checks. +`--pytest-project`, `--pytest-test-file`, `--mypy-project`, `--ty-project`, +`--pyrefly-project` and `--uv-project` select keys (`--ruff-projects`, +`--pytest-tests` and so on select a whole dimension); `--ruff`, `--pytest`, +`--mypy`, `--ty`, `--pyrefly` and `--uv` select a module; and `--check ` +selects a check by name across modules: `lint`, `test`, `type-check`, `audit`, +or `stale` for the generators' staleness checks. `dagger call` cannot step into a collection yet. To call a function on one project, use the shell form: @@ -106,6 +109,7 @@ container or a Python tool. It reads the workspace directly: | `ruff` | holds a `pyproject.toml`, `ruff.toml` or `.ruff.toml` | one `findRoots` | | `mypy` | holds a non-empty `*.py` file of its own | one `findRoots`, one ripgrep search | | `ty` | holds a non-empty `*.py` file of its own | one `findRoots`, one ripgrep search | +| `pyrefly` | holds a non-empty `*.py` file of its own | one `findRoots`, one ripgrep search | | `pytest` | holds a test file of its own (see below) | one `findRoots`, one ripgrep search | | `uv` | holds a `uv.lock` | two `findRoots` | @@ -127,8 +131,8 @@ whole-project run still collects those, because it runs pytest the way the project configures it. On a tree with 40 projects and 16,000 files in their virtualenvs, discovery -for `dagger check -l --all` with all five modules installed takes 4–8 seconds, -down from 12–25 seconds when each project walked its own tree. +for `dagger check -l --all` with ruff, pytest, uv, mypy and ty installed takes +4–8 seconds, down from 12–25 seconds when each project walked its own tree. A uv workspace member, or a `path` source in `[tool.uv.sources]`, that sits inside the project declaring it is part of that project: it is not a key of @@ -139,21 +143,21 @@ source outside the declaring project stays a project of its own. ### The Python environment -mypy, ty and pytest run in the project's own environment, built with uv in -steps: +mypy, ty, pyrefly and pytest run in the project's own environment, built with +uv in steps: 1. `uv sync --locked --no-install-workspace --no-install-local`, with only the files uv reads mounted: the project's `pyproject.toml`, `uv.lock`, `.python-version` and `uv.toml`, and the `pyproject.toml` of each uv - workspace member or path source it owns. The three tools share this layer. -2. The tool is resolved against those dependencies: `mypy==…`, `ty==…`, or - `pytest==…` with the bundled `dagger-pytest-otel` plugin. + workspace member or path source it owns. The four tools share this layer. +2. The tool is resolved against those dependencies: `mypy==…`, `ty==…`, + `pyrefly==…`, or `pytest==…` with the bundled `dagger-pytest-otel` plugin. 3. The source is mounted. For pytest, `uv sync --locked` then installs the - project and its local path dependencies. mypy and ty skip the project - itself — they read first-party code from the source — and install only its - uv workspace members and path sources, when it has any. + project and its local path dependencies. mypy, ty and pyrefly skip the + project itself — they read first-party code from the source — and install + only its uv workspace members and path sources, when it has any. -A source edit leaves steps 1 and 2 cached. For mypy and ty nothing is +A source edit leaves steps 1 and 2 cached. For the type checkers nothing is installed after it, so only the checker runs again, and a project whose build needs a compiler or a slow build backend is type checked without one. An edit to an unrelated project's `pyproject.toml`, or to the workspace's @@ -406,7 +410,7 @@ Locking is a `@generate` rather than a pass/fail check, so drift shows up in Packaging is neither: an application-only project has no build backend, and failing it would be wrong. -### `mypy` and `ty` +### `mypy`, `ty` and `pyrefly` | Function | Description | | ------------- | ----------------------------------------------------------- | @@ -414,18 +418,18 @@ failing it would be wrong. | `project` | The project containing a workspace path. | | `version-for` | The tool version used for a project. | -| Setting | Default | Meaning | -| ---------------- | ------------------------- | ------------------------------------------ | -| `scope` | `["**"]` | Project roots to type check. | -| `defaultVersion` | mypy `2.3.1`, ty `0.0.78` | The checker for a project that pins none. | -| `args` | `[]` | Extra arguments for every run, e.g. `["--strict"]`. | -| `version` | `3.14` | Python version of the default base image. | -| `base` | none | Base image with Python and uv. | +| Setting | Default | Meaning | +| ---------------- | ------------------------------------------ | ------------------------------------------ | +| `scope` | `["**"]` | Project roots to type check. | +| `defaultVersion` | mypy `2.3.1`, ty `0.0.78`, pyrefly `1.4.0` | The checker for a project that pins none. | +| `args` | `[]` | Extra arguments for every run, e.g. mypy's `["--strict"]` or pyrefly's `["--preset", "strict"]`. | +| `version` | `3.14` | Python version of the default base image. | +| `base` | none | Base image with Python and uv. | -On a project: `type-check` (a `@check`), `has-sources`, `version`, `skip`, and -for mypy `configures-files`. Both modules name the check `type-check`, so -`dagger check --check type-check` runs them together and `--mypy --check -type-check` runs one. +On a project: `type-check` (a `@check`), `has-sources`, `version`, `skip`, for +mypy `configures-files`, and for pyrefly `configures-excludes`. All three +modules name the check `type-check`, so `dagger check --check type-check` runs +them together and `--mypy --check type-check` runs one. mypy runs as the project configures it. When the project's mypy config names its files — `files` in `mypy.ini`, `.mypy.ini`, `[tool.mypy]` in @@ -434,13 +438,37 @@ runs bare `mypy`, so the config decides what is checked; otherwise it runs `mypy .`. ty always runs bare `ty check` and honours `[tool.ty.src]`. Nested projects are excluded either way. -Both run inside the project's environment. A checker that cannot see the +pyrefly always runs bare `pyrefly check`, so the project's own +`project-includes` and `project-excludes` decide what is checked. The module +adds no preset, so a project gets the one `pyrefly check` would give it +locally. A project with no Pyrefly config — no `pyrefly.toml`, `.pyrefly.toml` +or `[tool.pyrefly]` — gets its mypy or Pyright config migrated when it has +one, and Pyrefly's `basic` preset otherwise. `basic` reports only +high-confidence errors such as an unresolved import, and not a mismatched +type. To check more, set `preset` in the project's Pyrefly config, or pass +`args: ["--preset", "default"]`, which overrides every project's own preset +and turns the migration off. + +Nested projects are excluded with `--project-excludes`, except in a project +whose Pyrefly config sets `project-excludes`: the flag would replace that list +rather than add to it, so such a project lists its nested projects itself. +`configures-excludes` reads only a `project-excludes` set directly in the +Pyrefly config. It does not see one inherited through `extends`, read from a +file that `args` names with `--config`, or migrated from a mypy or Pyright +config; in a project with nested projects, the flag replaces those. Such a +project sets `project-excludes` in its own Pyrefly config, or passes +`--project-excludes` in `args`, which adds to the module's flags. The pyrefly +version a project pins with `required-version` is read from `[tool.pyrefly]` +before `pyrefly.toml`, the reverse of Pyrefly's own order, so a project that +sets it in both keeps the two equal. + +All three run inside the project's environment. A checker that cannot see the installed packages cannot resolve third-party imports and reports errors that are not real, and a false error is worse than no check. -They are two modules rather than one with a setting because they spell the same -flag in different languages: `mypy --exclude` takes a regular expression and -`ty --exclude` takes a glob, so a directory named `sdk-v1.2` needs two +They are separate modules rather than one with a setting because they spell the +same flag in different languages: `mypy --exclude` takes a regular expression +and `ty --exclude` takes a glob, so a directory named `sdk-v1.2` needs two different escapes. ### `pyproject` @@ -521,14 +549,15 @@ The defaults, for a project that pins nothing: | ruff | 0.16.5 | `ruff/images/ruff/Dockerfile` | | mypy | 2.3.1 | `mypy`, `defaultVersion` | | ty | 0.0.78 | `ty`, `defaultVersion` | +| pyrefly | 1.4.0 | `pyrefly`, `defaultVersion` | | pytest | 9.1.1 | `pytest`, `defaultVersion` | The two in Dockerfiles are there so Dependabot can raise a pull request for them. The rest are plain arguments, so a project that wants a different version either pins it in its own metadata or sets `defaultVersion`. -**One environment, not three.** pytest, mypy and ty all ask the library for the -same dependency layer, so one `uv sync` serves three checks. +**One environment, not four.** pytest, mypy, ty and pyrefly all ask the library +for the same dependency layer, so one `uv sync` serves four checks. ## Relationship to the standalone modules diff --git a/dagger.toml b/dagger.toml index 2d70ee8..bdce766 100644 --- a/dagger.toml +++ b/dagger.toml @@ -29,6 +29,11 @@ is-module = true name = "ty" clients = ["./pyproject"] +[sdks.dang.scopes."pyrefly"] +is-module = true +name = "pyrefly" +clients = ["./pyproject"] + [sdks.dang.scopes."ruff"] is-module = true name = "ruff" @@ -42,7 +47,7 @@ clients = ["./pyproject"] [sdks.dang.scopes.".dagger/modules/e2e"] is-module = true name = "e2e" -clients = ["./pyproject", "./uv", "./mypy", "./ty", "./ruff", "./pytest"] +clients = ["./pyproject", "./uv", "./mypy", "./ty", "./pyrefly", "./ruff", "./pytest"] [sdks.dang.scopes.".dagger/modules/dagger-pytest-otel"] is-module = true diff --git a/pyproject/main.dang b/pyproject/main.dang index d79877d..79894b6 100644 --- a/pyproject/main.dang +++ b/pyproject/main.dang @@ -907,11 +907,12 @@ type PythonProject { Container with this project's dependencies and the project installed. This is the one environment the tools that need a Python runtime share. Ask - for it with the same extra packages and pytest, mypy and ty all get the same - container, so a single `uv sync` serves three checks instead of three. + for it with the same extra packages and pytest, mypy, ty and pyrefly all get + the same container, so a single `uv sync` serves four checks. - mypy and ty need it because a checker that cannot see the installed packages - cannot resolve third-party imports, and reports errors that are not real. + The type checkers need it because a checker that cannot see the installed + packages cannot resolve third-party imports, and reports errors that are not + real. Dependencies are installed before any source is mounted (see `dependencies`), so editing the source reinstalls only the project itself. @@ -1173,13 +1174,17 @@ type PythonProject { """ setsKey(ws: Workspace!, relativePath: String!, section: String!, key: String!): Boolean! { let contents = fileContents(ws, relativePath) + let header = "[" + section + "]" if (contents == "") { false } else { contents .split("\n") .map { line => line.trimSpace } - .dropWhile { line => line != "[" + section + "]" } + .dropWhile { line => + let rest = line.trimPrefix(header).trimSpace + line.hasPrefix(header) == false or (rest != "" and rest.hasPrefix("#") == false) + } .dropFirst(1) .takeWhile { line => line.hasPrefix("[") == false } .any { line => line.hasPrefix(key) and line.trimPrefix(key).trimSpace.hasPrefix("=") } diff --git a/pyrefly/dagger-module.toml b/pyrefly/dagger-module.toml new file mode 100644 index 0000000..d6676e1 --- /dev/null +++ b/pyrefly/dagger-module.toml @@ -0,0 +1,9 @@ +name = "pyrefly" +engineVersion = "v1.0.0-beta.15" + +[runtime] + source = "dang" + +[[dependencies]] + name = "pyproject" + source = "../pyproject" diff --git a/pyrefly/main.dang b/pyrefly/main.dang new file mode 100644 index 0000000..6719968 --- /dev/null +++ b/pyrefly/main.dang @@ -0,0 +1,269 @@ +pub description = "Static type checking for Python projects, powered by Pyrefly" + +""" +Pyrefly over every Python project in a workspace. + +Pyrefly is Meta's checker. It runs inside the project's environment on +purpose. Without the installed packages it cannot resolve third-party imports, +and an unresolved import is reported as an error that is not real. + +The module adds no preset, so a project gets the one `pyrefly check` would +give it locally. One with no Pyrefly config of its own gets its mypy or +Pyright config migrated, or else Pyrefly's `basic` preset, which reports only +high-confidence errors such as unresolved imports. Set a preset in the +project's config, or pass one in `args`. + +https://dagger.io +https://pyrefly.org +""" +type Pyrefly { + """ + Files whose presence makes a directory a project Pyrefly should check. + """ + let markers: [String!]! = ["pyproject.toml"] + + """ + Python version used for the default base image. Null when a custom base is set. + """ + version: String { + lib.version + } + + """ + Project roots to check ("!" excludes; see the `scope` constructor argument). + """ + scope: [String!]! = ["**"] + + """ + Pyrefly version used for a project that pins none of its own. + + Pinned rather than floating: `uv run --with pyrefly` resolves to whichever + Pyrefly is newest on the day it runs, so a check starts failing on a day + nobody touched any code. + """ + defaultVersion: String! = "1.4.0" + + """ + Extra arguments forwarded to every Pyrefly invocation, e.g. ["--preset", "strict"]. + """ + args: [String!]! = [] + + let pythonVersion: String + let base: Container + + new( + """ + Python version used for the default base image. Mutually exclusive with base. + """ + version: String = null, + """ + Base image for every Pyrefly container. Expected to provide a Python + interpreter and uv on PATH. Mutually exclusive with version. + """ + base: Container = null, + """ + Project roots to check. A bare pattern selects, a "!"-prefixed pattern + excludes, and an exclude always wins whatever the order, so ["**", "!docs"] + checks everything but docs. + """ + scope: [String!]! = ["**"], + """ + Pyrefly version used for a project that pins none of its own. + """ + defaultVersion: String! = "1.4.0", + """ + Extra arguments forwarded to every Pyrefly invocation. + """ + args: [String!]! = [], + ) { + self.pythonVersion = version + self.base = base + self.scope = scope + self.defaultVersion = defaultVersion + self.args = args + self + } + + let lib: Pyproject! { + pyproject(markers: markers, version: pythonVersion, base: base) + } + + """ + Python projects at or below the working directory, plus the one enclosing + it, that hold Python source of their own, keyed by project root. + """ + projects(ws: Workspace!, includeSkipped: Boolean! = true): PyreflyProjects! { + PyreflyProjects( + paths: lib + .rootsOwning(ws, ["*.py"]) + .map { root => pyreflyProject(root) }.{{path, skip}} + .filter { project => includeSkipped or project.skip == false } + .map { project => project.path }, + tool: self, + ) + } + + """ + The Pyrefly project containing a workspace path. + """ + project(ws: Workspace!, path: String!, findUp: Boolean! = true): PyreflyProject! { + pyreflyProject(lib.project(ws, path, findUp: findUp).path) + } + + let pyreflyProject(root: String!): PyreflyProject! { + PyreflyProject(path: root, tool: self) + } + + """ + The Pyrefly version used for a project: what it pins, else the default. + """ + versionFor(ws: Workspace!, path: String!): String! { + lib.project(ws, path).toolVersion(ws, "pyrefly") ?? defaultVersion + } +} + +# Keep all Dang types in one source file until Dagger can convert Workspace +# arguments for installed multi-file Dang modules. See dagger/dagger#13476. + +""" +Python projects in a workspace, keyed by project root. +""" +type PyreflyProjects @collection { + """ + Project root paths. + """ + paths: [String!]! @keys + + let tool: Pyrefly! + + """ + The Pyrefly project rooted at path. + """ + project(path: String!): PyreflyProject! @get { + tool.pyreflyProject(path) + } + + """ + Type check the selected projects with Pyrefly. + """ + typeCheck(ws: Workspace!): Void @check { + # A selection over a list evaluates its elements concurrently, so every + # project is checked in parallel and every failure is reported. + let failures = paths.map { p => project(p) }.{{path, failure: typeCheckFailure(ws)}} + .filter { result => result.failure != null } + if (failures.length > 0) { + raise "pyrefly type check failed:\n" + failures.map { result => + "- " + result.path + ": " + (result.failure ?? "") + }.join("\n") + } + null + } +} + +""" +A Python project rooted at a workspace-relative path. +""" +type PyreflyProject { + """ + Project root, relative to the workspace. + """ + path: String! + + let tool: Pyrefly! + + """ + Whether this project is outside the configured scope. + """ + skip: Boolean! { + tool.lib.at(path).selected(tool.scope) == false + } + + """ + Whether this project holds Python source of its own, rather than only in a + project nested inside it. + """ + hasSources(ws: Workspace!): Boolean! { + tool.lib.at(path).hasOwnFiles(ws, ["*.py"]) + } + + """ + The Pyrefly version this project is checked with: what it pins, else the + module default. + """ + version(ws: Workspace!): String! { + tool.lib.at(path).toolVersion(ws, "pyrefly") ?? tool.defaultVersion + } + + """ + Type check this project with Pyrefly. + """ + typeCheck(ws: Workspace!): Void @check { + let failure = typeCheckFailure(ws) + if (failure != null) { + raise "pyrefly type check failed:\n- " + path + ": " + failure + } + null + } + + """ + Error message from type checking this project, or null when it passes, is + outside the configured scope, or holds no Python source of its own. + """ + let typeCheckFailure(ws: Workspace!): String { + if (skip or hasSources(ws) == false) { + null + } else { + { typeCheckExec(ws).sync; null } rescue { + err: Error => err.message.replace(old: "evaluating receiver: ", new: "").replace( + old: "reduce function: ", + new: "", + ) + } + } + } + + let typeCheckExec(ws: Workspace!): Container! { + let project = tool.lib.at(path) + let packages = ["pyrefly==" + version(ws)] + # A command-line --project-excludes replaces the configured list rather + # than adding to it, so it is passed only when the project configures none. + let excludes = if (configuresExcludes(ws)) { [] } else { + project.excludeNestedFlags(ws, "--project-excludes", regex: false) + } + # The environment comes from the library, so the dependency layer + # underneath is the same one pytest and the other checkers get. + # The project itself is not installed: the checker reads first-party code + # from the source, so it needs no build backend or compiler. + project.step( + project.env(ws, with: packages, installProject: false), + project.uvRun(packages) + ["pyrefly", "check"] + tool.args + excludes, + "pyrefly", + ) + } + + """ + Whether this project's Pyrefly config sets `project-excludes`. Pyrefly reads + the first of pyrefly.toml, .pyrefly.toml (both at the top level) and + pyproject.toml ([tool.pyrefly]) that it finds, and so does this. It also + accepts the key spelled `project_excludes`. + """ + configuresExcludes(ws: Workspace!): Boolean! { + let project = tool.lib.at(path) + let keys = ["project-excludes", "project_excludes"] + let pyreflyToml = if (project.hasFile(ws, "pyrefly.toml")) { "pyrefly.toml" } else { ".pyrefly.toml" } + if (project.hasFile(ws, pyreflyToml)) { + project + .fileContents(ws, pyreflyToml) + .split("\n") + .map { line => line.trimSpace } + .takeWhile { line => line.hasPrefix("[") == false } + .any { line => + keys.any { key => line.hasPrefix(key) and line.trimPrefix(key).trimSpace.hasPrefix("=") } + } + } else if (project.fileContents(ws, "pyproject.toml").contains("[tool.pyrefly]")) { + keys.any { key => project.setsKey(ws, "pyproject.toml", "tool.pyrefly", key) } + } else { + false + } + } +} diff --git a/testdata/project-pyrefly-excludes/generated/bad.py b/testdata/project-pyrefly-excludes/generated/bad.py new file mode 100644 index 0000000..6cd21fa --- /dev/null +++ b/testdata/project-pyrefly-excludes/generated/bad.py @@ -0,0 +1,2 @@ +# Checked only if the project's own project-excludes were replaced. +generated_count: int = "not an int" diff --git a/testdata/project-pyrefly-excludes/nested/ok.py b/testdata/project-pyrefly-excludes/nested/ok.py new file mode 100644 index 0000000..48580ff --- /dev/null +++ b/testdata/project-pyrefly-excludes/nested/ok.py @@ -0,0 +1,2 @@ +def nested_value() -> int: + return 2 diff --git a/testdata/project-pyrefly-excludes/nested/pyproject.toml b/testdata/project-pyrefly-excludes/nested/pyproject.toml new file mode 100644 index 0000000..f04f366 --- /dev/null +++ b/testdata/project-pyrefly-excludes/nested/pyproject.toml @@ -0,0 +1,4 @@ +[project] +name = "project-pyrefly-excludes-nested" +version = "0.1.0" +requires-python = ">=3.11" diff --git a/testdata/project-pyrefly-excludes/nested/pyrefly.toml b/testdata/project-pyrefly-excludes/nested/pyrefly.toml new file mode 100644 index 0000000..bd6d318 --- /dev/null +++ b/testdata/project-pyrefly-excludes/nested/pyrefly.toml @@ -0,0 +1,2 @@ +# The snake_case spelling Pyrefly also accepts. +project_excludes = ["scratch"] diff --git a/testdata/project-pyrefly-excludes/ok.py b/testdata/project-pyrefly-excludes/ok.py new file mode 100644 index 0000000..7c41bd2 --- /dev/null +++ b/testdata/project-pyrefly-excludes/ok.py @@ -0,0 +1,2 @@ +def value() -> int: + return 1 diff --git a/testdata/project-pyrefly-excludes/pyproject.toml b/testdata/project-pyrefly-excludes/pyproject.toml new file mode 100644 index 0000000..81f28de --- /dev/null +++ b/testdata/project-pyrefly-excludes/pyproject.toml @@ -0,0 +1,9 @@ +[project] +name = "project-pyrefly-excludes" +version = "0.1.0" +requires-python = ">=3.11" + +# A command-line --project-excludes for the nested project would replace this +# list, and generated/ would be checked. +[tool.pyrefly] # a comment here still opens the section +project-excludes = ["generated", "nested"]