Skip to content

Commit 078a04b

Browse files
committed
remove external dependency for parsing SA version
1 parent b393824 commit 078a04b

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

flask_sqlalchemy/utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
11

2-
from pkg_resources import parse_version
32
import sqlalchemy
43

54

5+
def parse_version(v):
6+
"""
7+
Take a string version and conver it to a tuple (for easier comparison), e.g.:
8+
9+
"1.2.3" --> (1, 2, 3)
10+
"1.2" --> (1, 2, 0)
11+
"1" --> (1, 0, 0)
12+
"""
13+
parts = v.split(".")
14+
# Pad the list to make sure there is three elements so that we get major, minor, point
15+
# comparisons that default to "0" if not given. I.e. "1.2" --> (1, 2, 0)
16+
parts = (parts + 3 * ['0'])[:3]
17+
return tuple(int(x) for x in parts)
18+
19+
620
def sqlalchemy_version(op, val):
721
sa_ver = parse_version(sqlalchemy.__version__)
822
target_ver = parse_version(val)

tests/test_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55

66
class TestSQLAlchemyVersion:
7+
def test_parse_version(self):
8+
assert utils.parse_version('1.2.3') == (1, 2, 3)
9+
assert utils.parse_version('1.2') == (1, 2, 0)
10+
assert utils.parse_version('1') == (1, 0, 0)
711

812
@mock.patch.object(utils, 'sqlalchemy')
913
def test_sqlalchemy_version(self, m_sqlalchemy):

0 commit comments

Comments
 (0)