Skip to content

fix(custom_static): import escape from markupsafe for Flask 3 compatibility - #216

Open
escooterclinic wants to merge 1 commit into
ActivityWatch:masterfrom
eMobility-Innovations:fix/flask3-markupsafe-escape
Open

escooterclinic wants to merge 1 commit into
ActivityWatch:masterfrom
eMobility-Innovations:fix/flask3-markupsafe-escape

Conversation

@escooterclinic

Copy link
Copy Markdown

We run aw-server as a central server and wanted to move its venv to Flask 3.1. Importing aw_server on Flask 3 fails before anything starts:

aw_server/custom_static.py:24: in <module>
    from flask import (
E   ImportError: cannot import name 'escape' from 'flask'

Flask 3.0 dropped the flask.escape re-export (it was deprecated in 2.3). This imports escape from markupsafe instead, which is the same function and already a Flask dependency, so nothing changes on Flask 2.x.

I added tests/test_custom_static.py, which checks that the custom-static 404 message still HTML-escapes the watcher name and path. Tested on current master with Python 3.12:

  • Flask 2.3.3 / Werkzeug 2.3.8: the new test plus test_server, test_host_header, test_input_validation and test_api_heartbeat pass (64 passed).
  • Flask 3.1 / Werkzeug 3.1: same 64 pass. Without this change, the conftest import fails with the error above.

pyproject.toml still pins flask = "^2.2" and werkzeug = "^2.3.3", so pip won't pick Flask 3 yet. I left that alone because it also means regenerating poetry.lock. I can add it here or in a follow-up, whichever you'd rather. The JSON side already works on Flask 3 since 7f30c17 moved to a JSON provider.

The change was prepared and tested with an AI coding agent. I reviewed it.

…bility

Flask 3.0 removed the flask.escape re-export, so importing aw_server fails
with ImportError on Flask 3. markupsafe is already a Flask dependency and
provides the same function on Flask 2.x.
@greptile-apps

greptile-apps Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Imports escape from MarkupSafe instead of Flask, removing the import that fails on Flask 3.

  • The custom-page routes load with Flask 3 and keep escaping missing-page text.

Reviews (1) · Last reviewed commit: "fix(custom_static): import escape from m..." · Reviewed by Greptile

@TimeToBuildBob

Copy link
Copy Markdown
Contributor

Thanks! flask.escape was removed in Flask 3, and markupsafe.escape is the same function, so the import change is right. The regression test covers the escaping in the 404 body, which is the part that matters.

CI needs to run on this head before it's ready (only Greptile has reported; the test suite hasn't run because the workflow needs approval for forks). If it passes, no further changes are needed from my side.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants