<#3240 fix: sqlalchemy type property bug> Pull req...
# meltano-repo-activity
g
#3240 fix: sqlalchemy type property bug Pull request opened by kgpayne on <!date^1755273511^{date_short}|2025-08-15T15:58:31Z> Fixes the case when
python_type
is implemented as a
property
on the SQLAlchemy type class. In that case,
sql_type.python_type
returns a
property
instance in stead of a python type. Instantiating the SQLAlchemy type first returns the correct python type.
Copy code
=================================== FAILURES ===================================
__________ TestTargetDatabricks.test_target_camelcase_complex_schema ___________

self = <tests.core.DatabricksTargetCamelcaseComplexSchema object at 0x7fa532bc0370>
config = SuiteConfig(max_records_limit=150, ignore_no_records=False, ignore_no_records_for_streams=[])
resource = None
runner = <singer_sdk.testing.runners.TargetTestRunner object at 0x7fa5395cddf0>

    def run(
        self,
        config: SuiteConfig,
        resource: t.Any,
        runner: TargetTestRunner,
    ) -> None:
        """Test main run method.
    
        Args:
            config: SuiteConfig instance, to use for test.
            resource: A generic external resource, provided by a pytest fixture.
            runner: A Tap runner instance, to use with this test.
        """
        # get input from file
        if getattr(self, "singer_filepath", None):
            assert self.singer_filepath.is_file(), (
                f"Singer file {self.singer_filepath} does not exist."
            )
            runner.input_filepath = self.singer_filepath
>       super().run(config, resource, runner)

.tox/3.9/lib/python3.9/site-packages/singer_sdk/testing/templates.py:340: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
.tox/3.9/lib/python3.9/site-packages/singer_sdk/testing/templates.py:303: in run
    super().run(config, resource, runner)
.tox/3.9/lib/python3.9/site-packages/singer_sdk/testing/templates.py:122: in run
    self.test()
.tox/3.9/lib/python3.9/site-packages/singer_sdk/testing/templates.py:68: in test
    self.runner.sync_all()
.tox/3.9/lib/python3.9/site-packages/singer_sdk/testing/runners.py:275: in sync_all
    stdout, stderr = self._execute_sync(
.tox/3.9/lib/python3.9/site-packages/singer_sdk/testing/runners.py:307: in _execute_sync
    target.process_lines(target_input)
.tox/3.9/lib/python3.9/site-packages/singer_sdk/plugin_base.py:828: in process_lines
    return self.message_reader.process_lines(
.tox/3.9/lib/python3.9/site-packages/singer_sdk/singerlib/encoding/base.py:70: in process_lines
    callback(line_dict)
.tox/3.9/lib/python3.9/site-packages/singer_sdk/target_base.py:396: in _process_schema_message
    _ = self.get_sink(
.tox/3.9/lib/python3.9/site-packages/singer_sdk/target_base.py:189: in get_sink
    return self.add_sink(stream_name, schema, key_properties)
.tox/3.9/lib/python3.9/site-packages/singer_sdk/target_base.py:259: in add_sink
    sink.setup()
target_databricks/sinks.py:389: in setup
    super().setup()
.tox/3.9/lib/python3.9/site-packages/singer_sdk/sinks/sql.py:245: in setup
    self.connector.prepare_table(
.tox/3.9/lib/python3.9/site-packages/singer_sdk/connectors/sql.py:1420: in prepare_table
    self.prepare_column(
.tox/3.9/lib/python3.9/site-packages/singer_sdk/connectors/sql.py:1469: in prepare_column
    self._adapt_column_type(
.tox/3.9/lib/python3.9/site-packages/singer_sdk/connectors/sql.py:1781: in _adapt_column_type
    compatible_sql_type = self.merge_sql_types([current_type, sql_type])
.tox/3.9/lib/python3.9/site-packages/singer_sdk/connectors/sql.py:1529: in merge_sql_types
    sql_types = self._sort_types(sql_types)
.tox/3.9/lib/python3.9/site-packages/singer_sdk/connectors/sql.py:1602: in _sort_types
    return sorted(sql_types, key=_get_type_sort_key, reverse=True)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

sql_type = <class 'sqlalchemy.sql.sqltypes.String'>

    def _get_type_sort_key(
        sql_type: sqlalchemy.types.TypeEngine,
    ) -> tuple[int, int]:
        # return rank, with higher numbers ranking first
    
        len_ = int(getattr(sql_type, "length", 0) or 0)
    
        pytype = t.cast("type", sql_type.python_type)
>       if issubclass(pytype, (str, bytes)):
E       TypeError: issubclass() arg 1 must be a class

.tox/3.9/lib/python3.9/site-packages/singer_sdk/connectors/sql.py:1591: TypeError
String source here ## Summary by Sourcery Bug Fixes: • Detect and handle python_type properties on SQLAlchemy type classes by instantiating the type before accessing the property ## Summary by Sourcery Fix python_type property handling in SQL type sorting and add tests to ensure correct type precedence. Bug Fixes: • Instantiate SQLAlchemy type classes when python_type is defined as a property to avoid returning the property instance in type sorting. Enhancements: • Convert _sort_types to a staticmethod for improved clarity. Tests: • Add unit test to verify correct sorting order of SQL types, including length-based precedence. meltano/sdk