Uh oh!
There was an error while loading. Please reload this page.
Add prek hook to enforce keyword-only session on @provide_session - #67150
Conversation
There was a problem hiding this comment.
Pull request overview
This PR introduces a new prek/pre-commit hook intended to prevent regressions where functions decorated with @provide_session accept session positionally, enforcing the documented convention that session should be keyword-only.
Changes:
- Add
check_provide_session_kwargs.pyprek hook that AST-scans Python files for@provide_sessionfunctions with positionalsession. - Introduce a shrinking allowlist (
known_provide_session_positional.txt) to grandfather existing violations while preventing increases. - Add unit tests for decorator detection, violation counting, and allowlist tightening/cleanup behavior; wire the hook into
.pre-commit-config.yaml.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
scripts/ci/prek/check_provide_session_kwargs.py | Implements the new AST-based checker, allowlist manager, and CLI modes (--all-files, --cleanup, --generate). |
scripts/ci/prek/known_provide_session_positional.txt | Initial allowlist of existing positional-session offenders with per-file maximum counts. |
scripts/tests/ci/prek/test_check_provide_session_kwargs.py | Unit tests for the new hook’s core logic and allowlist behavior. |
.pre-commit-config.yaml | Registers the new hook so it runs on relevant Python paths (and the allowlist/script paths). |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
e8fdddb to
cccdbc6CompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
When only the allowlist file is changed, the hook now unions the previous allowlist (from `git show HEAD:<file>`) with the current one, so removing an entry can't silently drop coverage for a file that still has positional `session` arguments.
184add7 to
19f2e2aCompareUh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Lee-W
left a comment
There was a problem hiding this comment.
a few nits, but nothing is blocking
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Drop ``task-sdk`` from ``_PROJECT_SOURCE_ROOTS`` and the docstring/help text; task-sdk does not use ``@provide_session``. - Rename ``_previous_allowlist`` to ``_parse_tracked_allowlist`` so the name is a verb and reflects that it parses the git-tracked (``HEAD``) version. - Rename test fixtures ``fake_repo`` and ``git_repo`` to ``create_fake_repo`` and ``create_git_repo`` to match the verb-prefix naming convention, and use ``@pytest.mark.usefixtures`` for tests that only need the side-effect.
Uh oh!
There was an error while loading. Please reload this page.
airflow-ctl talks to Airflow via the API and has no @provide_session callsites, so scanning it is wasted work and misleading in the help text. The .pre-commit-config.yaml files pattern already only matches airflow-core|providers, so this just brings the script's own --all-files/--generate scope and docs in line.
Uh oh!
There was an error while loading. Please reload this page.
Why
The project convention is that any function decorated with
@provide_sessionmust declaresessionas keyword-only (after a bare*), so callers cannot pass it positionally by accident. Today this is only documented incontributing-docs/05_pull_requests.rst#database-session-handling, so regressions slip in during review.What
check-no-new-provide-session-positionalscans changed files (or--all-files) for@provide_sessionfunctions whosesessionparameter is still positional.scripts/ci/prek/known_provide_session_positional.txtaspath::Nentries (max count per file). The hook fails when a file exceeds its limit, and auto-tightens the entry when the count decreases so the allowlist only shrinks over time. (Following convention of build(prek-hook): Check whether new "raise AirflowException" is added #55416 )--all-files,--cleanup(drop stale entries), and--generate(rebuild the allowlist from scratch).Was generative AI tooling used to co-author this PR?