From 559aa0179c1dc9a65ccfdd1b2684457b064330d9 Mon Sep 17 00:00:00 2001 From: Mathieu Leplatre Date: Mon, 18 Mar 2024 11:01:29 +0100 Subject: [PATCH] Fix #79: add support for recent versions of Flask (#103) * Add support for Flask 2.3 * Add support for Flask 3.0 * Formatting * Update changelog --- docs/changelog.rst | 7 +++++++ src/dockerflow/checks/messages.py | 1 + src/dockerflow/django/signals.py | 1 + src/dockerflow/flask/checks/__init__.py | 1 + src/dockerflow/flask/checks/messages.py | 1 + src/dockerflow/flask/signals.py | 1 + src/dockerflow/sanic/checks.py | 1 + tests/constraints/flask-2.3.txt | 3 +++ tests/constraints/flask-3.0.txt | 3 +++ tests/flask/test_flask.py | 12 +++++++++--- tox.ini | 8 +++++--- 11 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 tests/constraints/flask-2.3.txt create mode 100644 tests/constraints/flask-3.0.txt diff --git a/docs/changelog.rst b/docs/changelog.rst index 889343b..768bdd1 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -1,6 +1,13 @@ Changelog --------- + +2024.3.1 (unreleased) +~~~~~~~~~~~~~~~~~~~~~ + +- Add support for Flask 2.3 and 3.0 (#103) + + 2024.3.0 ~~~~~~~~~~~~~~~~~~~~~ diff --git a/src/dockerflow/checks/messages.py b/src/dockerflow/checks/messages.py index e009093..7e83267 100644 --- a/src/dockerflow/checks/messages.py +++ b/src/dockerflow/checks/messages.py @@ -5,6 +5,7 @@ This is a minor port of the Django checks system messages to be used for Dockerflow checks of the Flask and Sanic extensions. """ + # Levels DEBUG = 10 INFO = 20 diff --git a/src/dockerflow/django/signals.py b/src/dockerflow/django/signals.py index ba049af..87925ba 100644 --- a/src/dockerflow/django/signals.py +++ b/src/dockerflow/django/signals.py @@ -33,6 +33,7 @@ def heartbeat_passed_handler(sender, level, **kwargs): def heartbeat_failed_handler(sender, level, **kwargs): statsd.incr('heartbeat.fail') """ + from django.dispatch import Signal heartbeat_passed = Signal() diff --git a/src/dockerflow/flask/checks/__init__.py b/src/dockerflow/flask/checks/__init__.py index 77c8ce7..d464978 100644 --- a/src/dockerflow/flask/checks/__init__.py +++ b/src/dockerflow/flask/checks/__init__.py @@ -4,6 +4,7 @@ """ This module contains a few built-in checks for the Flask integration. """ + from sqlalchemy import text from ... import health diff --git a/src/dockerflow/flask/checks/messages.py b/src/dockerflow/flask/checks/messages.py index 7d28079..d7f8d1e 100644 --- a/src/dockerflow/flask/checks/messages.py +++ b/src/dockerflow/flask/checks/messages.py @@ -5,6 +5,7 @@ This exposes dockerflow.checks.messages as dockerflow.flask.checks.messages for backwards compatibility """ + import warnings from ...checks.messages import * # noqa diff --git a/src/dockerflow/flask/signals.py b/src/dockerflow/flask/signals.py index 1a6073d..1dd8966 100644 --- a/src/dockerflow/flask/signals.py +++ b/src/dockerflow/flask/signals.py @@ -33,6 +33,7 @@ def heartbeat_failed_handler(sender, level, **extra): statsd.incr('heartbeat.fail') """ + from flask.signals import Namespace dockerflow_signals = Namespace() diff --git a/src/dockerflow/sanic/checks.py b/src/dockerflow/sanic/checks.py index 4e3f21a..891e9ec 100644 --- a/src/dockerflow/sanic/checks.py +++ b/src/dockerflow/sanic/checks.py @@ -4,6 +4,7 @@ """ This module contains built-in checks for the Sanic integration. """ + from .. import health from ..checks import ( # noqa CRITICAL, diff --git a/tests/constraints/flask-2.3.txt b/tests/constraints/flask-2.3.txt new file mode 100644 index 0000000..56b4cad --- /dev/null +++ b/tests/constraints/flask-2.3.txt @@ -0,0 +1,3 @@ +Flask>=2.3,<3 +Flask-SQLAlchemy<4 +Werkzeug<4 diff --git a/tests/constraints/flask-3.0.txt b/tests/constraints/flask-3.0.txt new file mode 100644 index 0000000..fd24722 --- /dev/null +++ b/tests/constraints/flask-3.0.txt @@ -0,0 +1,3 @@ +Flask>=3,<4 +Flask-SQLAlchemy<4 +Werkzeug>=3,<4 diff --git a/tests/flask/test_flask.py b/tests/flask/test_flask.py index 26fb4b7..36a1869 100644 --- a/tests/flask/test_flask.py +++ b/tests/flask/test_flask.py @@ -13,7 +13,7 @@ from flask_login.mixins import UserMixin from flask_migrate import Migrate from flask_redis import FlaskRedis -from flask_sqlalchemy import SQLAlchemy, get_debug_queries +from flask_sqlalchemy import SQLAlchemy from sqlalchemy.exc import DBAPIError, SQLAlchemyError from dockerflow import checks, health @@ -24,6 +24,12 @@ check_redis_connected, ) +try: + from flask_sqlalchemy.record_queries import get_recorded_queries +except ImportError: + # flask-sqlalchemy < 3 + from flask_sqlalchemy import get_debug_queries as get_recorded_queries + class MockUser(UserMixin): def __init__(self, id): @@ -186,10 +192,10 @@ def warning_check(): def test_lbheartbeat_makes_no_db_queries(dockerflow, app): with app.app_context(): - assert len(get_debug_queries()) == 0 + assert len(get_recorded_queries()) == 0 response = app.test_client().get("/__lbheartbeat__") assert response.status_code == 200 - assert len(get_debug_queries()) == 0 + assert len(get_recorded_queries()) == 0 def test_full_redis_check(mocker): diff --git a/tox.ini b/tox.ini index b7213c2..604c999 100644 --- a/tox.ini +++ b/tox.ini @@ -8,7 +8,7 @@ envlist = py{38,39,310,311}-dj{40,41,42} py{310,311,312}-dj{50} py{38,39,310,311,312}-fa100 - py{38,39,310,311}-fl{20,21,22} + py{38,39,310,311}-fl{20,21,22,23,30} py{38,39,310,311}-s{21,22} [testenv] @@ -21,7 +21,7 @@ deps = -rtests/requirements/default.txt dj{32,40,41,42,50}: -rtests/requirements/django.txt fa100: -rtests/requirements/fastapi.txt - fl{20,21,22}: -rtests/requirements/flask.txt + fl{20,21,22,23,30}: -rtests/requirements/flask.txt s{21,22}: -rtests/requirements/sanic.txt dj32: -ctests/constraints/django-3.2.txt dj40: -ctests/constraints/django-4.0.txt @@ -32,13 +32,15 @@ deps = fl20: -ctests/constraints/flask-2.0.txt fl21: -ctests/constraints/flask-2.1.txt fl22: -ctests/constraints/flask-2.2.txt + fl23: -ctests/constraints/flask-2.3.txt + fl30: -ctests/constraints/flask-3.0.txt s21: -ctests/constraints/sanic-21.txt s22: -ctests/constraints/sanic-22.txt commands = python --version dj{32,40,41,42,50}: pytest --no-migrations -o DJANGO_SETTINGS_MODULE=tests.django.settings -o django_find_project=false {posargs:tests/core/ tests/django} fa{100}: pytest {posargs: tests/core/ tests/fastapi/} - fl{20,21,22}: pytest {posargs:tests/core/ tests/flask/} + fl{20,21,22,23,30}: pytest {posargs:tests/core/ tests/flask/} s{21,22}: pytest {posargs:tests/core/ tests/sanic/} [testenv:py311-docs]