Skip to content

Add --dry-run flag to pyrefly infer#3991

Open
nitishagar wants to merge 1 commit into
facebook:mainfrom
nitishagar:nitishagar/issue-3920-infer-dry-run
Open

Add --dry-run flag to pyrefly infer#3991
nitishagar wants to merge 1 commit into
facebook:mainfrom
nitishagar:nitishagar/issue-3920-infer-dry-run

Conversation

@nitishagar

Copy link
Copy Markdown
Contributor

Summary

pyrefly infer rewrites files in place and always exits 0, so it cannot be used as a CI check for whether the formatter would make any changes.

This adds a --dry-run flag to pyrefly infer that runs the full inference, prints what changes would be made, writes nothing, and exits 0 if no changes would be made or 1 if any changes would be made — enabling CI check mode.

The change set is already computed by the existing read-only phase of run_inner; dry-run simply skips the two file writes (gated on a new dry_run flag on InferFlags), logs the planned changes per file, and returns UserError when any changes are pending. Non-dry-run behavior is unchanged.

$ pyrefly infer --dry-run file.py
 INFO file.py: would add 1 annotation(s) and 0 import(s)
$ echo $?
1
# file.py is unchanged

Fixes #3920.

Test plan

  • cargo build -p pyrefly
  • cargo test -p pyrefly dry_run — 12 passed, asserting both the exit status (UserError when changes, Success when none) and that the file is left byte-identical.
  • cargo clippy -p pyrefly --tests
  • Manual: pyrefly infer --dry-run on an unannotated file leaves it unchanged and exits 1 with the planned change printed; without --dry-run the file is annotated and exits 0.
Summary: pyrefly infer rewrites files in place and always exits 0, so it cannot be used as a CI check for whether the formatter would make changes. Add a --dry-run flag that runs the full inference, prints what changes would be made, writes nothing, and exits 1 if any file would change else 0. The change set is already computed by the existing read-only phase of run_inner; dry-run simply skips the two file writes (gated on a new dry_run flag on InferFlags), logs the planned changes per file, and returns UserError when any changes are pending. Non-dry-run behavior is unchanged.

Fixes facebook#3920

Test Plan: cargo build -p pyrefly; cargo test -p pyrefly dry_run (12 passed); cargo clippy -p pyrefly --tests; manual - pyrefly infer --dry-run on an unannotated file leaves it unchanged and exits 1 with the planned change printed; without --dry-run the file is annotated and exits 0.
@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

1 participant