diff --git a/src/databricks/sqlalchemy/_types.py b/src/databricks/sqlalchemy/_types.py index c180404..cd9c588 100644 --- a/src/databricks/sqlalchemy/_types.py +++ b/src/databricks/sqlalchemy/_types.py @@ -159,6 +159,10 @@ def process_result_value(self, value: Union[None, datetime], dialect): return None return value.replace(tzinfo=None) + @property + def python_type(self) -> type: + return datetime + class TIMESTAMP(sqlalchemy.types.TypeDecorator): """Represents values comprising values of fields year, month, day, hour, minute, and second, @@ -191,6 +195,10 @@ def process_result_value(self, value: Union[None, datetime], dialect): return value.replace(tzinfo=timezone.utc) return value + @property + def python_type(self) -> type: + return datetime + def process_bind_param( self, value: Union[datetime, None], dialect ) -> Optional[datetime]: @@ -279,6 +287,10 @@ def process_result_value( return self._parse_result_string(value) + @property + def python_type(self) -> type: + return time + class DatabricksStringType(sqlalchemy.types.TypeDecorator): """We have to implement our own String() type because SQLAlchemy's default implementation @@ -290,6 +302,10 @@ class DatabricksStringType(sqlalchemy.types.TypeDecorator): cache_ok = True pe = ParamEscaper() + @property + def python_type(self) -> type: + return str + def process_literal_param(self, value, dialect) -> str: """SQLAlchemy's default string escaping for backslashes doesn't work for databricks. The logic here implements the same logic as our legacy inline escaping logic. @@ -406,6 +422,10 @@ class TINYINT(sqlalchemy.types.TypeDecorator): impl = sqlalchemy.types.SmallInteger cache_ok = True + @property + def python_type(self) -> type: + return int + @compiles(TINYINT, "databricks") def compile_tinyint(type_, compiler, **kw): diff --git a/tests/test_local/test_types.py b/tests/test_local/test_types.py index 7036c8d..426216a 100644 --- a/tests/test_local/test_types.py +++ b/tests/test_local/test_types.py @@ -1,3 +1,4 @@ +import datetime import enum from uuid import UUID @@ -13,6 +14,7 @@ TIMESTAMP, TIMESTAMP_NTZ, ) +from databricks.sqlalchemy._parse import GET_COLUMNS_TYPE_MAP class DatabricksDataType(enum.Enum): @@ -366,3 +368,35 @@ def test_as_uuid_false_round_trip_normalizes_hex_input(self): assert bind(self.HYPHENATED) == self.HYPHENATED assert result(self.HYPHENATED) == self.HYPHENATED assert result(self.HEX) == self.HYPHENATED + + +class TestPythonType: + """TypeDecorator does not take python_type from its impl, so each of our + TypeDecorators has to declare it. Without that, SQLAlchemy 2.0 raises + NotImplementedError and 2.1 returns ``object``. + """ + + dialect = DatabricksDialect() + + @pytest.mark.parametrize( + "type_, expected", + [ + (TIMESTAMP(), datetime.datetime), + (TIMESTAMP_NTZ(), datetime.datetime), + (TINYINT(), int), + # The dialect swaps these in for Time() and String() via colspecs. + (sqlalchemy.Time().dialect_impl(dialect), datetime.time), + (sqlalchemy.String().dialect_impl(dialect), str), + ], + ) + def test_python_type(self, type_, expected): + assert type_.python_type is expected + + @pytest.mark.parametrize( + "type_name", + sorted(name for name in GET_COLUMNS_TYPE_MAP if name != "variant"), + ) + def test_reflected_types_report_python_type(self, type_name): + """Every type that column reflection can return, except VARIANT, maps + to a concrete Python type.""" + assert GET_COLUMNS_TYPE_MAP[type_name]().python_type is not object