Repository navigation
Block new mocks without spec or autospec in tests - #77463
Andrushika wants to merge 8 commits into
Conversation
AGENTS.md and the review instructions ask for spec or autospec on every mock in tests. A mock without a spec accepts any attribute and any call, so a test can pass against a method that does not exist. No check enforces the rule, so reviewers ask for it by hand. Add the check-no-new-unspecced-mocks prek hook. It counts mock class calls and patch or patch.object calls without a spec in test files. The 28,578 existing ones in 1,344 files go into generated/known_unspecced_mocks.txt, with the same per-file allowlist as check-no-new-airflow-exceptions.
|
Fork CI: green, needs a maintainer. CI for the latest commit passed in your fork, but this pull request changes files that decide which CI jobs run and where ( Pull requests from contributors run their CI in the contributor's own fork. GitHub Actions is free and has no minute limit on public forks, while the CI capacity of the Apache Software Foundation is shared by every ASF project and is finite. Fork CI tested this branch merged with How this works: CI in your fork. This comment was posted automatically by the fork CI monitor from templates drafted with an AI-assisted tool, so it may contain mistakes. An Apache Airflow maintainer, a real person, will review the pull request once its fork CI is green. Why the first pass is automated. |
|
Your CI change needs a maintainer. CI for this pull request runs in your fork, and the workflow runs it starts in apache/airflow are approved automatically while they only run the fork CI gate. This pull request changes files that define what CI runs:
Such a change can make project CI run on the project's runners, so its workflow runs in apache/airflow wait for a maintainer to review the change and approve them. CI in your fork is not affected, and its result stays visible on this pull request. Pull requests from contributors run their CI in the contributor's own fork. GitHub Actions is free and has no minute limit on public forks, while the CI capacity of the Apache Software Foundation is shared by every ASF project and is finite. This comment was posted automatically by the fork CI monitor from templates drafted with an AI-assisted tool, so it may contain mistakes. An Apache Airflow maintainer, a real person, will review the pull request once its fork CI is green. Why the first pass is automated. |
Write the violation message as one dedented block, so it prints the same text without the literal double backticks. Rename the per-call lookup to _get_spec_rule and its result to _SpecRule, since it returns the rule a call is checked against. Group the tests under the class they exercise, and add a test that fails when the hook's files pattern in .pre-commit-config.yaml and the scanned project roots drift apart.
potiuk
left a comment
There was a problem hiding this comment.
Good idea, and it reuses AllowlistManager cleanly; a few things to fix before it starts blocking — mostly about where the allowlist lives and how it reacts to unparseable files and explicit spec=None.
Smaller observations
generated/known_unspecced_mocks.txt— The PR has thebackport-to-v3-4-testlabel, but the allowlist is generated frommain's tests. Cherry-picked as-is, it won't matchv3-4-test's counts: higher counts there fail and lower ones get tightened, so the backport's static checks fail. Either drop the label or plan for the backport to run--generateonv3-4-testand commit that branch's own allowlist.- See inline comments on
scripts/ci/prek/check_unspecced_mocks.py:248,scripts/ci/prek/check_unspecced_mocks.py:168,scripts/ci/prek/check_unspecced_mocks.py:149,.pre-commit-config.yaml:1914.
This review was drafted by an AI-assisted tool and
confirmed by an Apache Airflow maintainer. The findings
below are observations, not blockers; an Apache Airflow
maintainer — a real person — will take the next look at the
PR. If you think a finding is mis-applied, please reply on
the PR and a maintainer will weigh in.More on how Apache Airflow handles maintainer review:
Contributing guide.
Count a spec argument set to None or False, new=DEFAULT and new_callable=None as no spec, by keyword or by position. A test file that the hook cannot read or parse now fails the hook and keeps its allowlist entry. Run the hook serially so parallel batches do not overwrite each other's tightened entries. Only prek hooks read the generated/known_*.txt allowlists, so a change to them no longer selects the image build-time check.
|
On your two questions: Should Block from the start or manual stage first? Block from the start. The allowlist already grandfathers the 28,578 existing cases, so a blocking hook only stops new ones. In the manual stage nobody runs it, so the allowlist is never tightened and the counts drift. You already measured that: 35 of the last 200 commits add one. Before it blocks, please fix the issues from my review: the allowlist location, the Drafted with an AI-assisted tool and reviewed by me before posting. |
A new_callable argument picks the class of the replacement on purpose, so only new_callable=None counts as no spec. Regenerate the allowlist against current main.
|
Thanks, went with both answers. The red static checks were this hook, since fork CI merges main and main had added new mocks after I generated the allowlist. I merged main and regenerated it. The mypy-task-sdk failure came from the k8s client v37 bump, which #77523 already fixed on main. |
potiuk
left a comment
There was a problem hiding this comment.
Thanks, @Andrushika, all the points from my last review are in: an unparseable file now fails and keeps its entry, None/False/DEFAULT count as no spec, require_serial is on, new=/spec_set=/new_callable= are covered by tests, and keeping the allowlist in generated/ while taking known_*.txt out of the image-build files (with the selective-checks doc and test updated) is a better answer than the move I suggested. Approving. The inline points are follow-ups, fine in this PR or a next one: checking edits to the allowlist itself, the provider compatibility matrix, and new_callable=MagicMock, where my earlier advice was too broad. The backport-to-v3-4-test label from last time still needs a decision too.
Smaller observations
general— Still open from my last review: the allowlist is generated frommain's tests, so cherry-picked as is it does not matchv3-4-test's counts and that branch's static checks fail. Drop the label, or plan the backport to run--generateonv3-4-testand commit that branch's own allowlist.- See inline comments on
.pre-commit-config.yaml:1916,dev/breeze/src/airflow_breeze/utils/selective_checks.py:709,scripts/ci/prek/check_unspecced_mocks.py:163,scripts/ci/prek/check_unspecced_mocks.py:170.
This review was drafted by an AI-assisted tool and
confirmed by an Apache Airflow maintainer. The maintainer
approving this PR has read the findings and signed off. If
something feels off, please reply on the PR and a maintainer
will follow up.More on how Apache Airflow handles maintainer review:
Contributing guide.
Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting
| language: python | ||
| pass_filenames: true | ||
| require_serial: true | ||
| files: ^(airflow-core|airflow-ctl|airflow-ctl-tests|airflow-e2e-tests|chart|dev|devel-common|docker-tests|kubernetes-tests|performance|providers|scripts|shared|task-sdk|task-sdk-integration-tests)/(.*/)?tests/.*\.py$ |
There was a problem hiding this comment.
Editing the allowlist itself is not checked
The hook only matches test .py files, so a change that raises a count in generated/known_unspecced_mocks.txt, drops entries in a conflict resolution, or deletes the file runs nothing: the hook never re-counts the files the allowlist names. check-no-new-provide-session-positional covers this by also matching its allowlist and its script and re-checking every file the allowlist lists when either changes. Add ^generated/known_unspecced_mocks\.txt$ and the script to files, and when either is among the changed files, check the files the allowlist names (and fail when the allowlist is missing), as check_provide_session_kwargs.py does.
Flagged by antigravity (adversarial review); cross-checked.
Drafted-by: Claude Code (Opus 5.5); reviewed by @potiuk before posting
There was a problem hiding this comment.
Makes sense. I'll do it in a follow-up that moves the allowlist-edit check from check_provide_session_kwargs.py into AllowlistManager, so all three allowlist hooks get it (plus require_serial on the other two).
A new_callable that names a mock class or PropertyMock builds the same unspecced mock as a bare patch, so it counts unless spec, spec_set or autospec is also passed. The string keys of a dict literal unpacked with ** are read as ordinary keywords. Only prek hooks read the generated/known_*.txt allowlists, so a change to them is hidden from the provider compatibility tests too.
Head branch was pushed to by a user without write access
|
Thanks for the approval! For the backport, I'd drop the label, the hook only needs to guard main and |
Why
Airflow tests must give every mock a
specorautospec. The rule is inAGENTS.mdand in the review instructions under.github/instructions/.A mock without a spec accepts any attribute and any call. A test can then pass against a method that does not exist.
No check enforces the rule, so reviewers ask for it by hand (#73221, #77073, #76908).
What
Add the
check-no-new-unspecced-mocksprek hook. It counts calls in test files that create a mock without a spec.A mock class call such as
MagicMock()counts when it has nospecorspec_set. Apatchorpatch.objectcall counts when it has noautospec, because it creates the sameMagicMock.A
patchcall withspec_set=ornew=does not count. Withnew=, the test picks the replacement itself. Anew_callable=that names a mock class orPropertyMockstill counts, because it builds the sameMagicMock.A spec argument set to
None,Falseormock.DEFAULTcounts as missing. A test file that the hook cannot parse fails the hook.The 29,447 existing ones in 1,359 files go into
generated/known_unspecced_mocks.txt. A file fails when its count goes up, and its entry goes down when the count drops. This works like the allowlist ofcheck-no-new-airflow-exceptions.Only prek hooks read the
generated/known_*.txtallowlists. A change to them does not select the image build-time check or the provider compatibility tests in selective checks.The counts change often on main, so I will regenerate the allowlist again before merge.
Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code following the guidelines