From ba516e50ad124407e9c72171266e0e432d351d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Ram=C3=ADrez=20Mondrag=C3=B3n?= Date: Thu, 28 Nov 2024 17:53:25 -0600 Subject: [PATCH] refactor: Use `sql_to_jsonschema_converter` --- tap_postgres/client.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tap_postgres/client.py b/tap_postgres/client.py index 7023716..a861e2b 100644 --- a/tap_postgres/client.py +++ b/tap_postgres/client.py @@ -35,12 +35,20 @@ class PostgresSQLToJSONSchema(SQLToJSONSchema): """Custom SQL to JSON Schema conversion for Postgres.""" - def __init__(self, dates_as_string: bool, json_as_object: bool, *args, **kwargs): + def __init__(self, *, dates_as_string: bool, json_as_object: bool, **kwargs): """Initialize the SQL to JSON Schema converter.""" - super().__init__(*args, **kwargs) + super().__init__(**kwargs) self.dates_as_string = dates_as_string self.json_as_object = json_as_object + @classmethod + def from_config(cls, config: dict) -> PostgresSQLToJSONSchema: + """Instantiate the SQL to JSON Schema converter from a config dictionary.""" + return cls( + dates_as_string=config["dates_as_string"], + json_as_object=config["json_as_object"], + ) + @functools.singledispatchmethod def to_jsonschema(self, column_type: t.Any) -> dict: """Customize the JSON Schema for Postgres types.""" @@ -132,6 +140,8 @@ def patched_conform(elem: t.Any, property_schema: dict) -> t.Any: class PostgresConnector(SQLConnector): """Connects to the Postgres SQL source.""" + sql_to_jsonschema_converter = PostgresSQLToJSONSchema + def __init__( self, config: dict | None = None, @@ -160,14 +170,6 @@ def __init__( super().__init__(config=config, sqlalchemy_url=sqlalchemy_url) - @functools.cached_property - def sql_to_jsonschema(self): - """Return a mapping of SQL types to JSON Schema types.""" - return PostgresSQLToJSONSchema( - dates_as_string=self.config["dates_as_string"], - json_as_object=self.config["json_as_object"], - ) - def get_schema_names(self, engine: Engine, inspected: Inspector) -> list[str]: """Return a list of schema names in DB, or overrides with user-provided values.