Skip to content

Commit 32b501d

Browse files
Improve compiler type safety with isinstance checks
- Replace hasattr() with isinstance() for AthenaStruct and AthenaMap - Add explicit type imports for better type safety - Simplify struct field checking logic with guaranteed type safety - More robust and performant type checking following SQLAlchemy patterns This eliminates potential AttributeError risks and provides clearer code semantics. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 61854ed commit 32b501d

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

pyathena/sqlalchemy/compiler.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
AthenaPartitionTransform,
1818
AthenaRowFormatSerde,
1919
)
20+
from pyathena.sqlalchemy.types import AthenaMap, AthenaStruct
2021

2122
if TYPE_CHECKING:
2223
from sqlalchemy import (
@@ -140,19 +141,21 @@ def visit_enum(self, type_, **kw):
140141
return self.visit_string(type_, **kw)
141142

142143
def visit_struct(self, type_, **kw): # noqa: N802
143-
if hasattr(type_, "fields") and type_.fields:
144-
field_specs = []
145-
for field_name, field_type in type_.fields.items():
146-
field_type_str = self.process(field_type, **kw)
147-
field_specs.append(f"{field_name} {field_type_str}")
148-
return f"ROW({', '.join(field_specs)})"
144+
if isinstance(type_, AthenaStruct):
145+
if type_.fields:
146+
field_specs = []
147+
for field_name, field_type in type_.fields.items():
148+
field_type_str = self.process(field_type, **kw)
149+
field_specs.append(f"{field_name} {field_type_str}")
150+
return f"ROW({', '.join(field_specs)})"
151+
return "ROW()"
149152
return "ROW()"
150153

151154
def visit_STRUCT(self, type_, **kw): # noqa: N802
152155
return self.visit_struct(type_, **kw)
153156

154157
def visit_map(self, type_, **kw): # noqa: N802
155-
if hasattr(type_, "key_type") and hasattr(type_, "value_type"):
158+
if isinstance(type_, AthenaMap):
156159
key_type_str = self.process(type_.key_type, **kw)
157160
value_type_str = self.process(type_.value_type, **kw)
158161
return f"MAP<{key_type_str}, {value_type_str}>"

0 commit comments

Comments
 (0)