Skip to content

Commit f443df5

Browse files
refactor: remove runtime packaging dependency for SQLAlchemy version checks (#190)
Signed-off-by: Balram Choudhary <bchoudhary@rocketsoftware.com>
1 parent 25a1f67 commit f443df5

File tree

5 files changed

+26
-24
lines changed

5 files changed

+26
-24
lines changed

ibm_db_sa/base.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
from sqlalchemy.sql import compiler
3030
from sqlalchemy.sql import operators
3131
from sqlalchemy.engine import default
32-
from sqlalchemy import __version__ as SA_Version
32+
from sqlalchemy import __version__ as SA_VERSION_STR
3333
from . import reflection as ibm_reflection
34-
from packaging import version
3534

36-
SQLALCHEMY_VERSION = version.parse(sqlalchemy.__version__)
35+
m = re.match(r"^\s*(\d+)\.(\d+)", SA_VERSION_STR)
36+
SA_VERSION_MM = (int(m.group(1)), int(m.group(2))) if m else (0, 0)
3737

38-
if SQLALCHEMY_VERSION >= version.parse("2.0"):
38+
if SA_VERSION_MM >= (2, 0):
3939
from sqlalchemy.sql.sqltypes import NullType, NULLTYPE, _Binary
4040
from sqlalchemy.sql.sqltypes import (
4141
ARRAY, BIGINT, BigInteger, BINARY, BLOB, BOOLEAN, Boolean,
@@ -68,8 +68,6 @@
6868
UserDefinedType, Variant
6969
)
7070

71-
SA_Version = [int(ver_token) for ver_token in SA_Version.split('.')[0:2]]
72-
7371
# as documented from:
7472
# http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0001095.htm
7573
RESERVED_WORDS = set(
@@ -373,7 +371,7 @@ def visit_large_binary(self, type_, **kw):
373371

374372

375373
class DB2Compiler(compiler.SQLCompiler):
376-
if SA_Version < [0, 9]:
374+
if SA_VERSION_MM < (0, 9):
377375
def visit_false(self, expr, **kw):
378376
return '0'
379377

@@ -550,7 +548,7 @@ def visit_function(self, func, result_map=None, **kwargs):
550548
def visit_cast(self, cast, **kw):
551549
type_ = cast.typeclause.type
552550

553-
if SQLALCHEMY_VERSION >= version.parse("2.0"):
551+
if SA_VERSION_MM >= (2, 0):
554552
valid_types = (
555553
CHAR, VARCHAR, CLOB, String, Text, Unicode, UnicodeText,
556554
BLOB, LargeBinary, VARBINARY,
@@ -718,7 +716,7 @@ def create_table_constraints(self, table, **kw):
718716
return result
719717

720718
def visit_create_index(self, create, include_schema=True, include_table_schema=True, **kw):
721-
if SA_Version < [0, 8]:
719+
if SA_VERSION_MM < (0, 8):
722720
sql = super(DB2DDLCompiler, self).visit_create_index(create, **kw)
723721
else:
724722
sql = super(DB2DDLCompiler, self).visit_create_index(create, include_schema, include_table_schema, **kw)
@@ -798,9 +796,9 @@ class DB2Dialect(default.DefaultDialect):
798796
supports_char_length = False
799797
supports_unicode_statements = False
800798
supports_unicode_binds = False
801-
if SA_Version < [1, 4]:
799+
if SA_VERSION_MM < (1, 4):
802800
returns_unicode_strings = False
803-
elif SA_Version < [2, 0]:
801+
elif SA_VERSION_MM < (2, 0):
804802
returns_unicode_strings = sa_types.String.RETURNS_CONDITIONAL
805803
else:
806804
returns_unicode_strings = True

ibm_db_sa/ibm_db.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
# | Contributors: Jaimy Azle, Mike Bayer,Hemlata Bhatt |
1818
# +--------------------------------------------------------------------------+
1919

20-
from sqlalchemy import __version__ as SA_Version
21-
SA_Version = [int(ver_token) for ver_token in SA_Version.split('.')[0:2]]
20+
import re
21+
from sqlalchemy import __version__ as SA_VERSION_STR
22+
m = re.match(r"^\s*(\d+)\.(\d+)", SA_VERSION_STR)
23+
SA_VERSION_MM = (int(m.group(1)), int(m.group(2))) if m else (0, 0)
2224

2325
from .base import DB2ExecutionContext, DB2Dialect
2426

25-
if SA_Version < [2,0]:
27+
if SA_VERSION_MM < (2, 0):
2628
from sqlalchemy import processors, types as sa_types, util
2729
else:
2830
from sqlalchemy import types as sa_types, util
@@ -36,7 +38,7 @@
3638
SQL_TXN_SERIALIZABLE = 8
3739
SQL_ATTR_TXN_ISOLATION = 108
3840

39-
if SA_Version < [0, 8]:
41+
if SA_VERSION_MM < (0, 8):
4042
from sqlalchemy.engine import base
4143
else:
4244
from sqlalchemy.engine import result as _result
@@ -76,7 +78,7 @@ def pre_exec(self):
7678

7779
def get_result_proxy(self):
7880
if self._callproc_result and self._out_parameters:
79-
if SA_Version < [0, 8]:
81+
if SA_VERSION_MM < (0, 8):
8082
result = base.ResultProxy(self)
8183
else:
8284
result = _result.ResultProxy(self)
@@ -89,7 +91,7 @@ def get_result_proxy(self):
8991

9092
return result
9193
else:
92-
if SA_Version < [0, 8]:
94+
if SA_VERSION_MM < (0, 8):
9395
result = base.ResultProxy(self)
9496
else:
9597
result = _result.ResultProxy(self)
@@ -115,7 +117,7 @@ class DB2Dialect_ibm_db(DB2Dialect):
115117
}
116118
)
117119

118-
if SA_Version < [2, 0]:
120+
if SA_VERSION_MM < (2, 0):
119121
@classmethod
120122
def dbapi(cls):
121123
""" Returns: the underlying DBAPI driver module

ibm_db_sa/ibm_db_as400.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from sqlalchemy import __version__ as SA_Version
2-
SA_Version = [int(ver_token) for ver_token in SA_Version.split('.')[0:2]]
1+
import re
2+
from sqlalchemy import __version__ as SA_VERSION_STR
3+
m = re.match(r"^\s*(\d+)\.(\d+)", SA_VERSION_STR)
4+
SA_VERSION_MM = (int(m.group(1)), int(m.group(2))) if m else (0, 0)
35
from .base import DB2ExecutionContext, DB2Dialect
4-
if SA_Version < [2,0]:
6+
if SA_VERSION_MM < (2, 0):
57
from sqlalchemy import processors, types as sa_types, util
68
else:
79
from sqlalchemy import types as sa_types, util

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[build-system]
2-
requires = ["setuptools>=42", "wheel", "packaging>=20.0"]
2+
requires = ["setuptools>=42", "wheel"]
33
build-backend = "setuptools.build_meta"

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
readme = os.path.join(os.path.dirname(__file__), 'README.md')
1313
if 'USE_PYODBC' in os.environ and os.environ['USE_PYODBC'] == '1':
14-
require = ['sqlalchemy>=0.7.3', 'packaging>=20.0']
14+
require = ['sqlalchemy>=0.7.3']
1515
else:
16-
require = ['sqlalchemy>=0.7.3','ibm_db>=2.0.0', 'packaging>=20.0']
16+
require = ['sqlalchemy>=0.7.3','ibm_db>=2.0.0']
1717

1818

1919
setup(

0 commit comments

Comments
 (0)