From 56d408c5b951a014f87e80e5233312731dad4753 Mon Sep 17 00:00:00 2001 From: Randy Syring Date: Tue, 26 Mar 2019 15:50:31 -0400 Subject: [PATCH] remove external dependency for parsing SA version --- flask_sqlalchemy/utils.py | 16 +++++++++++++++- tests/test_utils.py | 4 ++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/flask_sqlalchemy/utils.py b/flask_sqlalchemy/utils.py index 30619b07..52ea94e0 100644 --- a/flask_sqlalchemy/utils.py +++ b/flask_sqlalchemy/utils.py @@ -1,8 +1,22 @@ -from pkg_resources import parse_version import sqlalchemy +def parse_version(v): + """ + Take a string version and conver it to a tuple (for easier comparison), e.g.: + + "1.2.3" --> (1, 2, 3) + "1.2" --> (1, 2, 0) + "1" --> (1, 0, 0) + """ + parts = v.split(".") + # Pad the list to make sure there is three elements so that we get major, minor, point + # comparisons that default to "0" if not given. I.e. "1.2" --> (1, 2, 0) + parts = (parts + 3 * ['0'])[:3] + return tuple(int(x) for x in parts) + + def sqlalchemy_version(op, val): sa_ver = parse_version(sqlalchemy.__version__) target_ver = parse_version(val) diff --git a/tests/test_utils.py b/tests/test_utils.py index e6dddaaa..4c673f9a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -4,6 +4,10 @@ class TestSQLAlchemyVersion: + def test_parse_version(self): + assert utils.parse_version('1.2.3') == (1, 2, 3) + assert utils.parse_version('1.2') == (1, 2, 0) + assert utils.parse_version('1') == (1, 0, 0) @mock.patch.object(utils, 'sqlalchemy') def test_sqlalchemy_version(self, m_sqlalchemy):