11import re
2- from typing import Any , List , Optional , Dict , Collection , Iterable , Tuple
2+ from typing import Any , List , Optional , Dict , Union , Collection , Iterable , Tuple
33
44import databricks .sqlalchemy ._ddl as dialect_ddl_impl
55import databricks .sqlalchemy ._types as dialect_type_impl
@@ -73,10 +73,12 @@ class DatabricksDialect(default.DefaultDialect):
7373
7474 # SQLAlchemy requires that a table with no primary key
7575 # constraint return a dictionary that looks like this.
76- EMPTY_PK = {"constrained_columns" : [], "name" : None }
76+ EMPTY_PK : Dict [ str , Any ] = {"constrained_columns" : [], "name" : None }
7777
7878 # SQLAlchemy requires that a table with no foreign keys
7979 # defined return an empty list. Same for indexes.
80+ EMPTY_FK : List
81+ EMPTY_INDEX : List
8082 EMPTY_FK = EMPTY_INDEX = []
8183
8284 @classmethod
@@ -139,7 +141,7 @@ def _describe_table_extended(
139141 catalog_name : Optional [str ] = None ,
140142 schema_name : Optional [str ] = None ,
141143 expect_result = True ,
142- ) -> List [Dict [str , str ]]:
144+ ) -> Union [ List [Dict [str , str ]], None ]:
143145 """Run DESCRIBE TABLE EXTENDED on a table and return a list of dictionaries of the result.
144146
145147 This method is the fastest way to check for the presence of a table in a schema.
@@ -158,7 +160,7 @@ def _describe_table_extended(
158160 stmt = DDL (f"DESCRIBE TABLE EXTENDED { _target } " )
159161
160162 try :
161- result = connection .execute (stmt ). all ()
163+ result = connection .execute (stmt )
162164 except DatabaseError as e :
163165 if _match_table_not_found_string (str (e )):
164166 raise sqlalchemy .exc .NoSuchTableError (
@@ -197,9 +199,11 @@ def get_pk_constraint(
197199 schema_name = schema ,
198200 )
199201
200- raw_pk_constraints : List = get_pk_strings_from_dte_output (result )
202+ # Type ignore is because mypy knows that self._describe_table_extended *can*
203+ # return None (even though it never will since expect_result defaults to True)
204+ raw_pk_constraints : List = get_pk_strings_from_dte_output (result ) # type: ignore
201205 if not any (raw_pk_constraints ):
202- return self .EMPTY_PK
206+ return self .EMPTY_PK # type: ignore
203207
204208 if len (raw_pk_constraints ) > 1 :
205209 logger .warning (
@@ -212,11 +216,12 @@ def get_pk_constraint(
212216 pk_name = first_pk_constraint .get ("col_name" )
213217 pk_constraint_string = first_pk_constraint .get ("data_type" )
214218
215- return build_pk_dict (pk_name , pk_constraint_string )
219+ # TODO: figure out how to return sqlalchemy.interfaces in a way that mypy respects
220+ return build_pk_dict (pk_name , pk_constraint_string ) # type: ignore
216221
217222 def get_foreign_keys (
218223 self , connection , table_name , schema = None , ** kw
219- ) -> ReflectedForeignKeyConstraint :
224+ ) -> List [ ReflectedForeignKeyConstraint ] :
220225 """Return information about foreign_keys in `table_name`."""
221226
222227 result = self ._describe_table_extended (
@@ -225,7 +230,9 @@ def get_foreign_keys(
225230 schema_name = schema ,
226231 )
227232
228- raw_fk_constraints : List = get_fk_strings_from_dte_output (result )
233+ # Type ignore is because mypy knows that self._describe_table_extended *can*
234+ # return None (even though it never will since expect_result defaults to True)
235+ raw_fk_constraints : List = get_fk_strings_from_dte_output (result ) # type: ignore
229236
230237 if not any (raw_fk_constraints ):
231238 return self .EMPTY_FK
@@ -239,7 +246,8 @@ def get_foreign_keys(
239246 )
240247 fk_constraints .append (this_constraint_dict )
241248
242- return fk_constraints
249+ # TODO: figure out how to return sqlalchemy.interfaces in a way that mypy respects
250+ return fk_constraints # type: ignore
243251
244252 def get_indexes (self , connection , table_name , schema = None , ** kw ):
245253 """SQLAlchemy requires this method. Databricks doesn't support indexes."""
0 commit comments