Skip to content

Commit c39dec6

Browse files
committed
fix(sqlalchemy-spanner): instantiate types from _type_map and fix black formatting
_designate_type() was returning type classes instead of instances, causing isinstance() checks to fail. Handles the mixed _type_map where most entries are classes but NUMERIC is already an instance (with precision=38, scale=9). Also adds stacklevel=2 to the unknown-type warning and reformats test_types.py to pass black.
1 parent c2f5788 commit c39dec6

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

packages/sqlalchemy-spanner/google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,11 +1248,13 @@ def _designate_type(self, str_repr):
12481248
return _type_map["ARRAY"](inner_type)
12491249
else:
12501250
try:
1251-
return _type_map[str_repr]
1251+
col_type = _type_map[str_repr]
1252+
return col_type() if isinstance(col_type, type) else col_type
12521253
except KeyError:
12531254
warnings.warn(
12541255
"Did not recognize Spanner type '%s', "
1255-
"mapping it to NullType" % str_repr
1256+
"mapping it to NullType" % str_repr,
1257+
stacklevel=2,
12561258
)
12571259
return types.NullType()
12581260

packages/sqlalchemy-spanner/tests/unit/test_types.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ def test_in_type_map(self):
7070

7171
def test_in_inverse_type_map(self):
7272
assert sqlalchemy_spanner.TOKENLIST in sqlalchemy_spanner._type_map_inv
73-
assert sqlalchemy_spanner._type_map_inv[sqlalchemy_spanner.TOKENLIST] == "TOKENLIST"
73+
assert (
74+
sqlalchemy_spanner._type_map_inv[sqlalchemy_spanner.TOKENLIST]
75+
== "TOKENLIST"
76+
)
7477

7578
def test_type_compiler_roundtrip(self):
7679
compiler = sqlalchemy_spanner.SpannerTypeCompiler(

0 commit comments

Comments
 (0)