Skip to content

Block new mocks without spec or autospec in tests - #77463

Open
Andrushika wants to merge 8 commits into
apache:mainfrom
Andrushika:check-mock-spec
Open

Andrushika wants to merge 8 commits into
apache:mainfrom
Andrushika:check-mock-spec

Conversation

@Andrushika

@Andrushika Andrushika commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Why

Airflow tests must give every mock a spec or autospec. The rule is in AGENTS.md and 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-mocks prek 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 no spec or spec_set. A patch or patch.object call counts when it has no autospec, because it creates the same MagicMock.

A patch call with spec_set= or new= does not count. With new=, the test picks the replacement itself. A new_callable= that names a mock class or PropertyMock still counts, because it builds the same MagicMock.

A spec argument set to None, False or mock.DEFAULT counts 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 of check-no-new-airflow-exceptions.

Only prek hooks read the generated/known_*.txt allowlists. 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?
  • Yes (please specify the tool below)

Generated-by: Claude Code following the guidelines

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.
@github-actions github-actions Bot added the fork ci required Gated by AIP-120: CI must pass in the contributor's fork. Managed by the fork CI reconciler. label Nov 22, 2026
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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 (.github/, the breeze sources, the breeze installation or scripts/ci/prek/), and the fork run used those changed files. A maintainer will review the change and run project CI for it before it can be merged.

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 main at 112b1a4db4, as project CI would, 4 commits behind main when the run turned green.

Latest run: https://github.com/Andrushika/airflow/actions/runs/46903887306

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.

@github-actions

Copy link
Copy Markdown
Contributor

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:

  • scripts/ci/prek/check_unspecced_mocks.py

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.

@github-actions github-actions Bot added the needs project ci Green fork CI, but CI files changed: a maintainer reviews and adds use project ci. label Nov 22, 2026
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.
@github-actions github-actions Bot added needs project ci Green fork CI, but CI files changed: a maintainer reviews and adds use project ci. and removed needs project ci Green fork CI, but CI files changed: a maintainer reviews and adds use project ci. labels Nov 22, 2026

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 the backport-to-v3-4-test label, but the allowlist is generated from main's tests. Cherry-picked as-is, it won't match v3-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 --generate on v3-4-test and 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.

Comment thread .pre-commit-config.yaml Resolved
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.
@github-actions github-actions Bot removed the needs project ci Green fork CI, but CI files changed: a maintainer reviews and adds use project ci. label Nov 24, 2026
@potiuk

potiuk commented Nov 24, 2026

Copy link
Copy Markdown
Member

On your two questions:

Should patch / patch.object count? Yes. AGENTS.md says "Use spec/autospec when mocking", and a patch() without autospec creates the same unspecced MagicMock. If they don't count, two thirds of the cases stay unchecked. Some calls are fine without autospec, though, because they don't create an unspecced mock: new=<real object> and new_callable= (which picks the class). autospec also doesn't work everywhere: instance attributes set in __init__ and some properties need new= or spec_set. So please count new= (except new=mock.DEFAULT), new_callable= and spec_set= as specced, and test those cases.

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 SyntaxError handling, spec=None, and require_serial. Also, the static checks job in your fork CI is red, so please check whether this hook is the cause.


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.
@Andrushika

Copy link
Copy Markdown
Contributor Author

Thanks, went with both answers. new_callable= counts as specced now, and there are tests for new=, spec_set= and new_callable=.

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.

@github-actions github-actions Bot added the needs project ci Green fork CI, but CI files changed: a maintainer reviews and adds use project ci. label Nov 25, 2026

@potiuk potiuk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 from main's tests, so cherry-picked as is it does not match v3-4-test's counts and that branch's static checks fail. Drop the label, or plan the backport to run --generate on v3-4-test and 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

Comment thread .pre-commit-config.yaml
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$

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@potiuk
potiuk enabled auto-merge (squash) December 1, 2026 16:10
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.
auto-merge was automatically disabled December 2, 2026 14:32

Head branch was pushed to by a user without write access

@Andrushika

Copy link
Copy Markdown
Contributor Author

Thanks for the approval! For the backport, I'd drop the label, the hook only needs to guard main and v3-4-test would need its own allowlist. Could you remove it? I don't have permission to change labels.

@github-actions github-actions Bot added needs project ci Green fork CI, but CI files changed: a maintainer reviews and adds use project ci. and removed needs project ci Green fork CI, but CI files changed: a maintainer reviews and adds use project ci. labels Dec 2, 2026

This branch has not been deployed

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

Labels

area:dev-tools backport-to-v3-4-test Backport to v3-4-test fork ci required Gated by AIP-120: CI must pass in the contributor's fork. Managed by the fork CI reconciler. needs project ci Green fork CI, but CI files changed: a maintainer reviews and adds use project ci.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants