Skip to content

Declare python_type on the dialect's TypeDecorators - #85

Open
0z5a wants to merge 1 commit into
databricks:mainfrom
0z5a:typedecorator-python-type
Open

0z5a wants to merge 1 commit into
databricks:mainfrom
0z5a:typedecorator-python-type

Conversation

@0z5a

@0z5a 0z5a commented Nov 21, 2026

Copy link
Copy Markdown

I started from #81 and checked it against the code.

What did you change, and why?

TypeDecorator does not take python_type from its impl. Five of the dialect's types are TypeDecorators that don't declare it, so they report the TypeEngine default: NotImplementedError on SQLAlchemy 2.0 and object on 2.1.

Type Returned by python_type before after
TIMESTAMP reflection of timestamp object / raises datetime
TIMESTAMP_NTZ reflection of timestamp_ntz, colspecs for DateTime object / raises datetime
TINYINT reflection of tinyint object / raises int
DatabricksTimeType colspecs for Time object / raises time
DatabricksStringType colspecs for String object / raises str

Each type now declares python_type, matching what its process_result_value returns (or its impl returns, where there is no process_result_value). DatabricksArray, DatabricksMap and DatabricksVariant still report object, as #81 explains: their values depend on connector settings.

How I found it

From #81. I reproduced it offline on SQLAlchemy 2.1.9 (object for all five) and 2.0.58 (NotImplementedError for all five). As a control, the stock types they wrap, DateTime() and BigInteger(), report datetime and int on both versions.

Where this can show up in practice

The report in #81 is from generating ORM models with sqlacodegen: TIMESTAMP and TINYINT columns came out as Mapped[object | None] instead of Mapped[datetime | None] and Mapped[int | None]. On SQLAlchemy 2.0, any code that reads column.type.python_type on a reflected timestamp, timestamp_ntz or tinyint column gets a NotImplementedError instead.

How do you know it works?

  • Added TestPythonType in tests/test_local/test_types.py:
    • checks python_type for the five types
    • checks that every type in GET_COLUMNS_TYPE_MAP except variant reports a concrete type, so a type added to the map later is covered too
  • Python 3.12 on macOS:
    • On unmodified main with SQLAlchemy 2.1.9, 8 of the new cases fail: the five types plus the timestamp, timestamp_ntz and tinyint entries of the reflection map. The other 16 new cases pass.
    • With the fix, tests/test_local/test_types.py passes on SQLAlchemy 2.1.9 and on 2.0.58 (83 tests each).
    • With the fix, all 340 tests in tests/test_local, excluding e2e, pass on SQLAlchemy 2.1.9.
  • black --check src passes.
  • I did not run the warehouse e2e tests or sqlacodegen itself.

TypeDecorator does not take python_type from its impl, so TIMESTAMP,
TIMESTAMP_NTZ, TINYINT, DatabricksTimeType and DatabricksStringType fell
back to the TypeEngine default: NotImplementedError on SQLAlchemy 2.0 and
`object` on 2.1. TIMESTAMP, TIMESTAMP_NTZ and TINYINT are what column
reflection returns for timestamp, timestamp_ntz and tinyint, so tools
that read python_type from reflected columns (sqlacodegen, for one) got
no usable type for them.

Declare python_type on each of the five types, matching the values their
process_result_value returns.

Signed-off-by: Dezhen Lu <dezhen.lu@student.uni-tuebingen.de>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant