4
4
5
5
import atexit
6
6
import io
7
+ import itertools
7
8
import signal
9
+ import sys
8
10
import typing as t
9
11
from contextlib import contextmanager
10
12
from functools import cached_property
@@ -95,7 +97,7 @@ def interpret_content_encoding(self) -> bool:
95
97
"""
96
98
return self .config .get ("interpret_content_encoding" , False )
97
99
98
- def prepare_table ( # type: ignore[override]
100
+ def prepare_table ( # type: ignore[override] # noqa: PLR0913
99
101
self ,
100
102
full_table_name : str | FullyQualifiedName ,
101
103
schema : dict ,
@@ -121,7 +123,7 @@ def prepare_table( # type: ignore[override]
121
123
meta = sa .MetaData (schema = schema_name )
122
124
table : sa .Table
123
125
if not self .table_exists (full_table_name = full_table_name ):
124
- table = self .create_empty_table (
126
+ return self .create_empty_table (
125
127
table_name = table_name ,
126
128
meta = meta ,
127
129
schema = schema ,
@@ -130,7 +132,6 @@ def prepare_table( # type: ignore[override]
130
132
as_temp_table = as_temp_table ,
131
133
connection = connection ,
132
134
)
133
- return table
134
135
meta .reflect (connection , only = [table_name ])
135
136
table = meta .tables [
136
137
full_table_name
@@ -269,7 +270,7 @@ def to_sql_type(self, jsonschema_type: dict) -> sa.types.TypeEngine: # type: ig
269
270
270
271
return PostgresConnector .pick_best_sql_type (sql_type_array = sql_type_array )
271
272
272
- def pick_individual_type (self , jsonschema_type : dict ):
273
+ def pick_individual_type (self , jsonschema_type : dict ): # noqa: PLR0911
273
274
"""Select the correct sql type assuming jsonschema_type has only a single type.
274
275
275
276
Args:
@@ -307,11 +308,7 @@ def pick_individual_type(self, jsonschema_type: dict):
307
308
return ARRAY (self .to_sql_type ({"type" : items_type }))
308
309
309
310
# Case 3: tuples
310
- if isinstance (items , list ):
311
- return ARRAY (JSONB ())
312
-
313
- # All other cases, return JSONB
314
- return JSONB ()
311
+ return ARRAY (JSONB ()) if isinstance (items , list ) else JSONB ()
315
312
316
313
# string formats
317
314
if jsonschema_type .get ("format" ) == "date-time" :
@@ -324,9 +321,7 @@ def pick_individual_type(self, jsonschema_type: dict):
324
321
):
325
322
return HexByteString ()
326
323
individual_type = th .to_sql_type (jsonschema_type )
327
- if isinstance (individual_type , VARCHAR ):
328
- return TEXT ()
329
- return individual_type
324
+ return TEXT () if isinstance (individual_type , VARCHAR ) else individual_type
330
325
331
326
@staticmethod
332
327
def pick_best_sql_type (sql_type_array : list ):
@@ -355,13 +350,12 @@ def pick_best_sql_type(sql_type_array: list):
355
350
NOTYPE ,
356
351
]
357
352
358
- for sql_type in precedence_order :
359
- for obj in sql_type_array :
360
- if isinstance (obj , sql_type ):
361
- return obj
353
+ for sql_type , obj in itertools .product (precedence_order , sql_type_array ):
354
+ if isinstance (obj , sql_type ):
355
+ return obj
362
356
return TEXT ()
363
357
364
- def create_empty_table ( # type: ignore[override]
358
+ def create_empty_table ( # type: ignore[override] # noqa: PLR0913
365
359
self ,
366
360
table_name : str ,
367
361
meta : sa .MetaData ,
@@ -397,7 +391,7 @@ def create_empty_table( # type: ignore[override]
397
391
raise RuntimeError (
398
392
f"Schema for table_name: '{ table_name } '"
399
393
f"does not define properties: { schema } "
400
- )
394
+ ) from None
401
395
402
396
for property_name , property_jsonschema in properties .items ():
403
397
is_primary_key = property_name in primary_keys
@@ -531,7 +525,7 @@ def get_column_add_ddl( # type: ignore[override]
531
525
},
532
526
)
533
527
534
- def _adapt_column_type ( # type: ignore[override]
528
+ def _adapt_column_type ( # type: ignore[override] # noqa: PLR0913
535
529
self ,
536
530
schema_name : str ,
537
531
table_name : str ,
@@ -669,7 +663,7 @@ def get_sqlalchemy_query(self, config: dict) -> dict:
669
663
# ssl_enable is for verifying the server's identity to the client.
670
664
if config ["ssl_enable" ]:
671
665
ssl_mode = config ["ssl_mode" ]
672
- query . update ({ "sslmode" : ssl_mode })
666
+ query [ "sslmode" ] = ssl_mode
673
667
query ["sslrootcert" ] = self .filepath_or_certificate (
674
668
value = config ["ssl_certificate_authority" ],
675
669
alternative_name = config ["ssl_storage_directory" ] + "/root.crt" ,
@@ -764,7 +758,7 @@ def catch_signal(self, signum, frame) -> None:
764
758
signum: The signal number
765
759
frame: The current stack frame
766
760
"""
767
- exit (1 ) # Calling this to be sure atexit is called, so clean_up gets called
761
+ sys . exit (1 ) # Calling this to be sure atexit is called, so clean_up gets called
768
762
769
763
def _get_column_type ( # type: ignore[override]
770
764
self ,
0 commit comments