Repository navigation
ci: flag Flask imports with ast-grep - #730
Conversation
Add the app rules flask-import and flask-import-tests (severity warning until the ASGI release; in FOCA scoped to the examples) and the FOCA baseline rule no-new-flask-internals (error in foca/, excluding the modules that still need Flask in 0.22). Each rule has rule tests. Document the rules in the contributing guide and the migration guide to 0.22. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cover Flask extensions and Connexion's Flask modules, merge the tests rule into flask-import (apps list their package and tests in files:), add rule tests for parenthesized, aliased and relative imports, and correct the docs: the note names the replacements, and apps update their copies to error with the ASGI release. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Also flag `from connexion.apps import flask_app` and the like, and FlaskApp and FlaskApi from connexion. Mark the rule test files as twins, and rewrap the docs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Reviewer's GuideConfigurationReview the two matching ast-grep rules together: they define which Flask, Werkzeug, and Connexion imports are reported in examples and prevented in FOCA, including the baseline exclusions.
DocumentationReview the contributing and migration guidance together for rule scope, severity, adoption, and replacement recommendations.
TestsCheck that the paired rule-test files cover the same accepted and rejected import forms, including aliases, parenthesized imports, relative imports, and wildcard imports.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #730 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 74 74
Lines 9325 9325
=========================================
Hits 9325 9325
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path=".ast-grep/rules/flask-import.yml" line_range="48" />
<code_context>
+ kind: dotted_name
+ regex: ^(context|request|[Ff]lask\w*)$
+ stopBy: end
+ - pattern: connexion.request
+ - pattern: connexion.context
</code_context>
<issue_to_address>
**Flask class accesses pass scan**
When code uses `connexion.FlaskApp` or `connexion.FlaskApi` through an unaliased `import connexion`, the `connexion.request` and `connexion.context` patterns do not match those class accesses, so the migration scan produces no finding and the Flask API use remains until the ASGI migration.
Extend the attribute patterns to flag `connexion.FlaskApp` and `connexion.FlaskApi`.
Also at `.ast-grep/rules/no-new-flask-internals.yml:55-56`, `.ast-grep/rules/flask-import.yml:49`.
</issue_to_address>
### Comment 2
<location path=".ast-grep/rules/flask-import.yml" line_range="46" />
<code_context>
+ regex: ^connexion(\.[\w.]+)?$
+ - has:
+ kind: dotted_name
+ regex: ^(context|request|[Ff]lask\w*)$
+ stopBy: end
+ - pattern: connexion.request
</code_context>
<issue_to_address>
**Wildcard imports pass migration scan**
When an app uses a Connexion 2 wildcard import that exports Flask internals, the Connexion import checks have no named symbol to match, so the scan produces no warning and Flask-specific exports can remain in the app until Connexion 3 removes them.
Add a rule that flags wildcard imports from Connexion.
Also at `.ast-grep/rules/no-new-flask-internals.yml:52-54`.
</issue_to_address>
### Comment 3
<location path="docs/migration-0.22.md" line_range="223" />
<code_context>
+accessors above, `foca.errors`, `foca.testing`, `foca.get_config()`, and
+tuples as responses. Werkzeug stays installed, but don't import it.
+
+To adopt it, copy the file into your app's `.ast-grep/rules/`, set its
+`files:` to your package and tests (e.g., `my_app/**` and `tests/**`), and run
+`ast-grep scan` (e.g., via pre-commit). The rule has the severity `warning`
</code_context>
<issue_to_address>
**Installed package lacks rule**
When a user follows the migration guide using an installed FOCA distribution, the source distribution excludes `.ast-grep`, and the package configuration does not include it, so users cannot obtain the rule from the installed package and cannot adopt it as instructed.
Include the rule in FOCA distributions or direct users to a repository copy they can download.
</issue_to_address>Linked issues
#636 ci: flag Flask imports with ast-grep · 5 of 6 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Flag the specified Flask, Werkzeug and Connexion imports in the app rule, with package and test coverage and the requested severities. | ✅ Met | The rule covers the requested import forms in FOCA’s examples. Apps can set its files: to both <pkg>/** and tests/**; that provides the requested coverage with one rule. The rule is warning-level, and the docs say apps should switch their copies to error for the ASGI release. |
| 2 | Name the FOCA 0.22 replacements in the app rule’s note. | ✅ Met | The note names request accessors, token and claim accessors, errors, testing utilities, configuration, and response tuples; the referenced FOCA accessors and utilities are present. |
| 3 | Add the FOCA baseline rule at error severity, excluding modules that still need Flask and removing exclusions as they are decoupled. | ✅ Met | The rule covers foca/**, lists the exclusions, and documents removing them when no longer needed. |
| 4 | Test both rules and have scans warn on imports in examples and fail on new imports in non-excluded FOCA modules. | ✅ Met | There is a test file for each rule with matching cases, and the rule scopes and severities implement the requested scan behavior. The PR also reports successful scans and probes. |
| 5 | Add the rule to the contributing docs and the preparation page, and document its severity per FOCA version. | 🟡 Partly met | The contributing guide and migration guide document the rule and severities, but the preparation page is not updated. The PR says, “The 'Preparing for the ASGI release' page follows in #637.” |
| 6 | Keep Werkzeug installed; this is a rule against importing it, not a dependency-removal change. | ✅ Met | The change adds the import rule and does not remove Werkzeug as a dependency. |
Sourcery assessment
Approval pending. 3 findings to address first.
Blocking findings: .ast-grep/rules/flask-import.yml:48, .ast-grep/rules/flask-import.yml:46, docs/migration-0.22.md:223
Flag connexion.FlaskApp and connexion.FlaskApi as attributes and wildcard imports from connexion, and link the rule file in the migration guide, since it isn't part of the Python package. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Linked issues
#636 ci: flag Flask imports with ast-grep · 6 of 7 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Flag the requested Flask, Werkzeug and Connexion imports, with an app rule covering package and test files at the required severities. | ✅ Met | The flask-import rule covers the requested import forms, including Flask extensions, Connexion Flask modules, and Connexion request/context imports. Apps are instructed to set files: to both their package and tests; the rule is a warning in FOCA 0.22 and the ASGI release is documented as error. |
| 2 | Name the replacements for request data, exceptions, tests, configuration and responses in the rule note. | ✅ Met | The rule note names the FOCA accessors and modules, foca.get_config(), and tuple responses. |
| 3 | Add a no-new-flask-internals error rule for foca/**, excluding modules that still need Flask and removing exclusions as modules are decoupled. |
✅ Met | The baseline rule scopes to foca/**, lists the current exclusions, and instructs maintainers to remove exclusions once modules no longer need Flask. |
| 4 | Add ast-grep tests for both rules. | ✅ Met | There is a rule-test file for each rule, with matching cases. |
| 5 | Add the rule to the contributing documentation and the preparation page, and document its severity per FOCA version. | 🟡 Partly met | The contributing guide documents the rules and severities, and the migration guide explains adoption; the preparation page is deferred to #637 and is not updated here. |
| 6 | Ensure every replacement named in the rule notes exists in FOCA 0.22. | ✅ Met | The named accessors and get_config() are present, and the foca.errors and foca.testing APIs are provided. |
| 7 | Keep Werkzeug installed; make this a rule against importing it, not a dependency removal. | ✅ Met | The change adds import checks and does not remove a dependency. |
Closes #636
Summary
flask-import(.ast-grep/rules/flask-import.yml, severitywarning)flask(includingflask.globals.request_ctx) and Flask extensions (flask_*, e.g.,flask_cors,flask_pymongo);werkzeug;connexion.apis.flask_*,connexion.apps.flask_app,connexion.security.flask_*, also when imported by name from their package) andconnexion.FlaskApp/FlaskApi(imported or as attributes), and wildcard imports fromconnexion;connexion.requestandconnexion.context, as imports and as attributes.foca.context.current_request();current_token_info()andcurrent_claims()offoca.security.auth;foca.errors;foca.testing;foca.get_config();connexion.App, and attributes of an aliasedconnexionmodule.examples/**. Apps setfiles:to their package and their tests.no-new-flask-internals(severityerror,foca/**)dev. A reviewer confirmed that none of these exclusions is stale..ast-grep/rule-tests/, one file per rule, with the same cases. The cases include parenthesized, aliased, relative and wildcard imports.errorwith the ASGI release.Deviation from the issue: the issue asked for a second rule with the same pattern for
tests/**. Instead, apps list<pkg>/**andtests/**in thefiles:of the singleflask-importrule. A separate rule would have been dead in FOCA, which has no example tests, and would have added a fourth copy of the rule body.Verification
ast-grep test --skip-snapshot-tests: 9 passed.ast-grep scan: exit 0, with 2flask-importwarnings, inexamples/petstore/controllers.pyandexamples/petstore-access-control/controllers.py.foca/_probe.pywithfrom werkzeug.exceptions import NotFoundmakes the scan fail (exit 1).examples/_probe.pyonly warns (exit 0).pre-commit run --all-files: passed.mkdocs build --strict: passed.FlaskAppas an attribute, wildcard imports, the rule missing from the package), all fixed in a00609c; threads resolved.Declined review findings
from connexion import App: declined. FOCA 0.22 has no replacement for it yet. The ASGI release changes the app type, and docs: add "Preparing for the ASGI release" #637 documents that. The note says it isn't covered.connexionmodule (import connexion as cx; cx.request): declined. It's rare, and the note says it isn't covered.utils:, or add a sync check: declined. Apps would have to copy two files. The rule files and test files carry "keep in sync" comments instead.Open questions
None.
🤖 Generated with Claude Code