From 170f15f3079ca8ea786d2497006f25520ffdb820 Mon Sep 17 00:00:00 2001 From: Pamela Fox Date: Wed, 27 Dec 2023 06:19:29 -0800 Subject: [PATCH 1/4] Fix repr and add tests --- src/flask_sqlalchemy/extension.py | 10 +++-- tests/test_extension_repr.py | 61 +++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 tests/test_extension_repr.py diff --git a/src/flask_sqlalchemy/extension.py b/src/flask_sqlalchemy/extension.py index 43e1b9a4..3429e059 100644 --- a/src/flask_sqlalchemy/extension.py +++ b/src/flask_sqlalchemy/extension.py @@ -281,12 +281,14 @@ def __repr__(self) -> str: if not has_app_context(): return f"<{type(self).__name__}>" - message = f"{type(self).__name__} {self.engine.url}" + num_default_engines = 1 if self.engines.get(None) else 0 + engine_str = self.engine.url if num_default_engines else "(No default engine)" - if len(self.engines) > 1: - message = f"{message} +{len(self.engines) - 1}" + num_other_engines = len(self.engines) - num_default_engines + if num_other_engines >= 1: + engine_str = f"{engine_str} +{num_other_engines} engines" - return f"<{message}>" + return f"<{type(self).__name__} {engine_str}>" def init_app(self, app: Flask) -> None: """Initialize a Flask application for use with this extension instance. This diff --git a/tests/test_extension_repr.py b/tests/test_extension_repr.py new file mode 100644 index 00000000..cfa94c75 --- /dev/null +++ b/tests/test_extension_repr.py @@ -0,0 +1,61 @@ +from __future__ import annotations + +from flask import Flask + +from flask_sqlalchemy import SQLAlchemy + + +def test_repr_no_context() -> None: + db = SQLAlchemy() + app = Flask(__name__) + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://" + + db.init_app(app) + assert repr(db) == "" + + +def test_repr_default() -> None: + db = SQLAlchemy() + app = Flask(__name__) + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://" + + db.init_app(app) + with app.app_context(): + assert repr(db) == "" + + +def test_repr_default_plustwo() -> None: + db = SQLAlchemy() + app = Flask(__name__) + app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite://" + app.config["SQLALCHEMY_BINDS"] = { + "a": "sqlite:///:memory:", + "b": "sqlite:///test.db", + } + + db.init_app(app) + with app.app_context(): + assert repr(db) == "" + + +def test_repr_nodefault() -> None: + db = SQLAlchemy() + app = Flask(__name__) + app.config["SQLALCHEMY_BINDS"] = {"x": "sqlite:///:memory:"} + + db.init_app(app) + with app.app_context(): + assert repr(db) == "" + + +def test_repr_nodefault_plustwo() -> None: + db = SQLAlchemy() + app = Flask(__name__) + app.config["SQLALCHEMY_BINDS"] = { + "a": "sqlite:///:memory:", + "b": "sqlite:///test.db", + } + + db.init_app(app) + with app.app_context(): + assert repr(db) == "" From c050828c54b29743548139c6d48ea0b6cce7416b Mon Sep 17 00:00:00 2001 From: Pamela Fox Date: Wed, 27 Dec 2023 06:25:00 -0800 Subject: [PATCH 2/4] update changes --- CHANGES.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 996fe3f3..724c5848 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,3 +1,9 @@ +Version 3.1.2 +------------- + +- Fix issue with calling ``repr()`` on ``SQLAlchemy`` instance with no default engine. :issue:`1295` + + Version 3.1.1 ------------- From 4334d7274482de71a41354a60b45e521dd088af7 Mon Sep 17 00:00:00 2001 From: Pamela Fox Date: Fri, 29 Dec 2023 14:51:55 -0800 Subject: [PATCH 3/4] Try without cache --- .github/workflows/tests.yaml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 48c9a23d..412034db 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -37,11 +37,5 @@ jobs: python-version: ${{ matrix.python }} cache: 'pip' cache-dependency-path: 'requirements/*.txt' - - name: cache mypy - uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 - with: - path: ./.mypy_cache - key: mypy|${{ matrix.python }}|${{ hashFiles('requirements/mypy.txt') }} - if: matrix.tox == 'typing' - run: pip install tox - run: tox r -e ${{ matrix.tox }} From 08a1886788eb51890dc1653b743c269201c36ad9 Mon Sep 17 00:00:00 2001 From: Pamela Fox Date: Fri, 29 Dec 2023 14:54:11 -0800 Subject: [PATCH 4/4] Now bring back mypy cache --- .github/workflows/tests.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 412034db..48c9a23d 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -37,5 +37,11 @@ jobs: python-version: ${{ matrix.python }} cache: 'pip' cache-dependency-path: 'requirements/*.txt' + - name: cache mypy + uses: actions/cache@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 + with: + path: ./.mypy_cache + key: mypy|${{ matrix.python }}|${{ hashFiles('requirements/mypy.txt') }} + if: matrix.tox == 'typing' - run: pip install tox - run: tox r -e ${{ matrix.tox }}