From f036b3b1a9f4d938baffb75cf17d25caa8648637 Mon Sep 17 00:00:00 2001 From: Vitaly Markov Date: Thu, 23 Nov 2023 20:15:36 +0000 Subject: [PATCH] Fix ruff warnings, reformat --- snowddl/app/base.py | 2 +- snowddl/parser/external_access_integration.py | 20 ++++- snowddl/parser/function.py | 4 +- snowddl/parser/network_rule.py | 2 +- snowddl/resolver/_utils.py | 2 +- .../resolver/external_access_integration.py | 20 ++--- snowddl/resolver/function.py | 22 ++++-- snowddl/resolver/network_policy.py | 2 +- snowddl/resolver/network_rule.py | 23 +++--- snowddl/resolver/procedure.py | 22 ++++-- snowddl/resolver/secret.py | 75 ++++++++++++------- test/conftest.py | 6 +- test/function/fn008.py | 30 ++++---- test/procedure/pr004.py | 26 +++---- 14 files changed, 153 insertions(+), 103 deletions(-) diff --git a/snowddl/app/base.py b/snowddl/app/base.py index e8d65c2..d21dfeb 100644 --- a/snowddl/app/base.py +++ b/snowddl/app/base.py @@ -8,7 +8,7 @@ from snowflake.connector import connect from traceback import TracebackException -from snowddl.blueprint import ObjectType +from snowddl.blueprint import Ident, ObjectType from snowddl.config import SnowDDLConfig from snowddl.engine import SnowDDLEngine from snowddl.parser import default_parser_sequence, PlaceholderParser diff --git a/snowddl/parser/external_access_integration.py b/snowddl/parser/external_access_integration.py index 8cb1387..3c5ddc1 100644 --- a/snowddl/parser/external_access_integration.py +++ b/snowddl/parser/external_access_integration.py @@ -45,19 +45,31 @@ class ExternalAccessIntegrationParser(AbstractParser): def load_blueprints(self): - self.parse_single_file(self.base_path / "external_access_integration.yaml", external_access_integration_json_schema, self.process_external_access_integration) + self.parse_single_file( + self.base_path / "external_access_integration.yaml", + external_access_integration_json_schema, + self.process_external_access_integration, + ) def process_external_access_integration(self, file: ParsedFile): for integration_name, integration in file.params.items(): - allowed_network_rules = [SchemaObjectIdent(self.env_prefix, *network_rule.split(".")) for network_rule in integration.get("allowed_network_rules")] + allowed_network_rules = [ + SchemaObjectIdent(self.env_prefix, *network_rule.split(".")) + for network_rule in integration.get("allowed_network_rules") + ] allowed_api_authentication_integrations = None allowed_authentication_secrets = None if integration.get("allowed_api_authentication_integrations"): - allowed_api_authentication_integrations = [Ident(api_integration) for api_integration in integration.get("allowed_api_authentication_integrations")] + allowed_api_authentication_integrations = [ + Ident(api_integration) for api_integration in integration.get("allowed_api_authentication_integrations") + ] if integration.get("allowed_authentication_secrets"): - allowed_authentication_secrets = [SchemaObjectIdent(self.env_prefix, *secret_name.split(".")) for secret_name in integration.get("allowed_authentication_secrets")] + allowed_authentication_secrets = [ + SchemaObjectIdent(self.env_prefix, *secret_name.split(".")) + for secret_name in integration.get("allowed_authentication_secrets") + ] bp = ExternalAccessIntegrationBlueprint( full_name=AccountObjectIdent(self.env_prefix, integration_name), diff --git a/snowddl/parser/function.py b/snowddl/parser/function.py index 1cd511e..0f37e25 100644 --- a/snowddl/parser/function.py +++ b/snowddl/parser/function.py @@ -207,6 +207,8 @@ def get_external_access_integrations(self, f: ParsedFile): def get_secrets(self, f: ParsedFile): if f.params.get("secrets"): - return {k: build_schema_object_ident(self.env_prefix, v, f.database, f.schema) for k, v in f.params.get("secrets").items()} + return { + k: build_schema_object_ident(self.env_prefix, v, f.database, f.schema) for k, v in f.params.get("secrets").items() + } return None diff --git a/snowddl/parser/network_rule.py b/snowddl/parser/network_rule.py index 10b5641..d439096 100644 --- a/snowddl/parser/network_rule.py +++ b/snowddl/parser/network_rule.py @@ -1,4 +1,4 @@ -from snowddl.blueprint import NetworkRuleBlueprint, SchemaObjectIdent, Ident +from snowddl.blueprint import NetworkRuleBlueprint, SchemaObjectIdent from snowddl.parser.abc_parser import AbstractParser, ParsedFile diff --git a/snowddl/resolver/_utils.py b/snowddl/resolver/_utils.py index 8a59873..fe44d45 100644 --- a/snowddl/resolver/_utils.py +++ b/snowddl/resolver/_utils.py @@ -29,7 +29,7 @@ def compare_dynamic_param_value(bp_value: Union[bool, int, float, str], existing def dtypes_from_arguments(arguments: str) -> str: - arguments = arguments.translate(str.maketrans('', '', '[] ')) + arguments = arguments.translate(str.maketrans("", "", "[] ")) start_dtypes_idx = arguments.index("(") finish_dtypes_idx = arguments.index(")") diff --git a/snowddl/resolver/external_access_integration.py b/snowddl/resolver/external_access_integration.py index b7a96c7..f4d7565 100644 --- a/snowddl/resolver/external_access_integration.py +++ b/snowddl/resolver/external_access_integration.py @@ -15,7 +15,7 @@ def get_existing_objects(self): "SHOW EXTERNAL ACCESS INTEGRATIONS LIKE {env_prefix:ls}", { "env_prefix": self.config.env_prefix, - } + }, ) for r in cur: @@ -36,7 +36,7 @@ def get_owner_from_grant(self, name): "SHOW GRANTS ON INTEGRATION {name:i}", { "name": name, - } + }, ) for r in cur: @@ -55,7 +55,7 @@ def create_object(self, bp: ExternalAccessIntegrationBlueprint): "CREATE EXTERNAL ACCESS INTEGRATION {name:i}", { "name": bp.full_name, - } + }, ) create_query.append_nl(common_query) @@ -81,7 +81,7 @@ def compare_object(self, bp: ExternalAccessIntegrationBlueprint, row: dict): "CREATE OR REPLACE EXTERNAL ACCESS INTEGRATION {name:i}", { "name": bp.full_name, - } + }, ) replace_query.append_nl(common_query) @@ -92,7 +92,7 @@ def compare_object(self, bp: ExternalAccessIntegrationBlueprint, row: dict): { "name": bp.full_name, "comment": common_query.add_short_hash(bp.comment), - } + }, ) return ResolveResult.ALTER @@ -116,7 +116,7 @@ def _build_common_external_access_integration_sql(self, bp: ExternalAccessIntegr "ALLOWED_NETWORK_RULES = ({allowed_network_rules:i})", { "allowed_network_rules": bp.allowed_network_rules, - } + }, ) if bp.allowed_api_authentication_integrations: @@ -124,7 +124,7 @@ def _build_common_external_access_integration_sql(self, bp: ExternalAccessIntegr "ALLOWED_API_AUTHENTICATION_INTEGRATIONS = ({allowed_api_authentication_integrations:i})", { "allowed_api_authentication_integrations": bp.allowed_api_authentication_integrations, - } + }, ) if bp.allowed_authentication_secrets: @@ -132,14 +132,14 @@ def _build_common_external_access_integration_sql(self, bp: ExternalAccessIntegr "ALLOWED_AUTHENTICATION_SECRETS = ({allowed_authentication_secrets:i})", { "allowed_authentication_secrets": bp.allowed_authentication_secrets, - } + }, ) query.append_nl( "ENABLED = {enabled:b}", { "enabled": bp.enabled, - } + }, ) if bp.comment: @@ -147,7 +147,7 @@ def _build_common_external_access_integration_sql(self, bp: ExternalAccessIntegr "COMMENT = {comment}", { "comment": bp.comment, - } + }, ) return query diff --git a/snowddl/resolver/function.py b/snowddl/resolver/function.py index 248ea08..f9de315 100644 --- a/snowddl/resolver/function.py +++ b/snowddl/resolver/function.py @@ -189,19 +189,25 @@ def _build_create_function(self, bp: FunctionBlueprint): if bp.external_access_integrations: # Snowflake bug: EXTERNAL_ACCESS_INTEGRATIONS must be identifiers # It does not accept identifiers in double-quotes - query.append_nl("EXTERNAL_ACCESS_INTEGRATIONS = ({external_access_integrations:r})", { - "external_access_integrations": bp.external_access_integrations, - }) + query.append_nl( + "EXTERNAL_ACCESS_INTEGRATIONS = ({external_access_integrations:r})", + { + "external_access_integrations": bp.external_access_integrations, + }, + ) if bp.secrets: query.append_nl("SECRETS = (") for idx, (var_name, secret_name) in enumerate(bp.secrets.items()): - query.append("{comma:r}{var_name} = {secret_name:i}", { - "comma": "" if idx == 0 else ", ", - "var_name": var_name, - "secret_name": secret_name, - }) + query.append( + "{comma:r}{var_name} = {secret_name:i}", + { + "comma": "" if idx == 0 else ", ", + "var_name": var_name, + "secret_name": secret_name, + }, + ) query.append(")") diff --git a/snowddl/resolver/network_policy.py b/snowddl/resolver/network_policy.py index 35fe17b..9831f93 100644 --- a/snowddl/resolver/network_policy.py +++ b/snowddl/resolver/network_policy.py @@ -36,7 +36,7 @@ def get_owner_from_grant(self, name): "SHOW GRANTS ON NETWORK POLICY {name:i}", { "name": name, - } + }, ) for r in cur: diff --git a/snowddl/resolver/network_rule.py b/snowddl/resolver/network_rule.py index 47700f7..c10b28a 100644 --- a/snowddl/resolver/network_rule.py +++ b/snowddl/resolver/network_rule.py @@ -16,7 +16,7 @@ def get_existing_objects_in_schema(self, schema: dict): { "database": schema["database"], "schema": schema["schema"], - } + }, ) for r in cur: @@ -43,7 +43,7 @@ def create_object(self, bp: NetworkRuleBlueprint): "CREATE NETWORK RULE {full_name:i}", { "full_name": bp.full_name, - } + }, ) create_query.append(common_query) @@ -62,7 +62,12 @@ def compare_object(self, bp: NetworkRuleBlueprint, row: dict): desc = cur.fetchone() is_replace_required = False - if bp.type != row["type"] or bp.mode != row["mode"] or bp.value_list != desc["value_list"].split(",") or bp.comment != row["comment"]: + if ( + bp.type != row["type"] + or bp.mode != row["mode"] + or bp.value_list != desc["value_list"].split(",") + or bp.comment != row["comment"] + ): is_replace_required = True if not is_replace_required: @@ -75,7 +80,7 @@ def compare_object(self, bp: NetworkRuleBlueprint, row: dict): "CREATE OR REPLACE NETWORK RULE {full_name:i}", { "full_name": bp.full_name, - } + }, ) replace_query.append(common_query) @@ -90,7 +95,7 @@ def drop_object(self, row: dict): "database": row["database"], "schema": row["schema"], "name": row["name"], - } + }, ) return ResolveResult.DROP @@ -102,21 +107,21 @@ def _build_common_network_rule_sql(self, bp: NetworkRuleBlueprint): "TYPE = {type}", { "type": bp.type, - } + }, ) query.append_nl( "VALUE_LIST = ({value_list})", { "value_list": bp.value_list, - } + }, ) query.append_nl( "MODE = {mode}", { "mode": bp.mode, - } + }, ) if bp.comment: @@ -124,7 +129,7 @@ def _build_common_network_rule_sql(self, bp: NetworkRuleBlueprint): "COMMENT = {comment}", { "comment": bp.comment, - } + }, ) return query diff --git a/snowddl/resolver/procedure.py b/snowddl/resolver/procedure.py index fef1d22..cb1227b 100644 --- a/snowddl/resolver/procedure.py +++ b/snowddl/resolver/procedure.py @@ -170,19 +170,25 @@ def _build_create_procedure(self, bp: ProcedureBlueprint): if bp.external_access_integrations: # Snowflake bug: EXTERNAL_ACCESS_INTEGRATIONS must be identifiers # It does not accept identifiers in double-quotes - query.append_nl("EXTERNAL_ACCESS_INTEGRATIONS = ({external_access_integrations:r})", { - "external_access_integrations": bp.external_access_integrations, - }) + query.append_nl( + "EXTERNAL_ACCESS_INTEGRATIONS = ({external_access_integrations:r})", + { + "external_access_integrations": bp.external_access_integrations, + }, + ) if bp.secrets: query.append_nl("SECRETS = (") for idx, (var_name, secret_name) in enumerate(bp.secrets.items()): - query.append("{comma:r}{var_name} = {secret_name:i}", { - "comma": "" if idx == 0 else ", ", - "var_name": var_name, - "secret_name": secret_name, - }) + query.append( + "{comma:r}{var_name} = {secret_name:i}", + { + "comma": "" if idx == 0 else ", ", + "var_name": var_name, + "secret_name": secret_name, + }, + ) query.append(")") diff --git a/snowddl/resolver/secret.py b/snowddl/resolver/secret.py index 60de2c9..9324933 100644 --- a/snowddl/resolver/secret.py +++ b/snowddl/resolver/secret.py @@ -16,7 +16,7 @@ def get_existing_objects_in_schema(self, schema: dict): { "database": schema["database"], "schema": schema["schema"], - } + }, ) for r in cur: @@ -42,7 +42,7 @@ def create_object(self, bp: SecretBlueprint): "CREATE SECRET {full_name:i}", { "full_name": bp.full_name, - } + }, ) create_query.append(common_query) @@ -70,7 +70,7 @@ def compare_object(self, bp: SecretBlueprint, row: dict): "CREATE OR REPLACE SECRET {full_name:i}", { "full_name": bp.full_name, - } + }, ) replace_query.append(common_query) @@ -85,7 +85,7 @@ def drop_object(self, row: dict): "database": row["database"], "schema": row["schema"], "name": row["name"], - } + }, ) return ResolveResult.DROP @@ -97,50 +97,71 @@ def _build_common_secret_sql(self, bp: SecretBlueprint): "TYPE = {type}", { "type": bp.type, - } + }, ) if bp.api_authentication: - query.append_nl("API_AUTHENTICATION = {api_authentication:i}", { - "api_authentication": bp.api_authentication, - }) + query.append_nl( + "API_AUTHENTICATION = {api_authentication:i}", + { + "api_authentication": bp.api_authentication, + }, + ) if bp.oauth_scopes: - query.append_nl("OAUTH_SCOPES = ({oauth_scopes})", { - "oauth_scopes": bp.oauth_scopes, - }) + query.append_nl( + "OAUTH_SCOPES = ({oauth_scopes})", + { + "oauth_scopes": bp.oauth_scopes, + }, + ) if bp.oauth_refresh_token: - query.append_nl("OAUTH_REFRESH_TOKEN = {oauth_refresh_token}", { - "oauth_refresh_token": bp.oauth_refresh_token, - }) + query.append_nl( + "OAUTH_REFRESH_TOKEN = {oauth_refresh_token}", + { + "oauth_refresh_token": bp.oauth_refresh_token, + }, + ) if bp.oauth_refresh_token_expiry_time: - query.append_nl("OAUTH_REFRESH_TOKEN_EXPIRY_TIME = {oauth_refresh_token_expiry_time}", { - "oauth_refresh_token_expiry_time": bp.oauth_refresh_token_expiry_time, - }) + query.append_nl( + "OAUTH_REFRESH_TOKEN_EXPIRY_TIME = {oauth_refresh_token_expiry_time}", + { + "oauth_refresh_token_expiry_time": bp.oauth_refresh_token_expiry_time, + }, + ) if bp.username: - query.append_nl("USERNAME = {username}", { - "username": bp.username, - }) + query.append_nl( + "USERNAME = {username}", + { + "username": bp.username, + }, + ) if bp.password: - query.append_nl("PASSWORD = {password}", { - "password": bp.password, - }) + query.append_nl( + "PASSWORD = {password}", + { + "password": bp.password, + }, + ) if bp.secret_string: - query.append_nl("SECRET_STRING = {secret_string}", { - "secret_string": bp.secret_string, - }) + query.append_nl( + "SECRET_STRING = {secret_string}", + { + "secret_string": bp.secret_string, + }, + ) if bp.comment: query.append_nl( "COMMENT = {comment}", { "comment": bp.comment, - } + }, ) return query diff --git a/test/conftest.py b/test/conftest.py index 09e5b5a..6e07e61 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -67,9 +67,7 @@ def desc_view(self, database, schema, name): return {r["name"]: r for r in cur} def desc_external_access_integration(self, name): - cur = self.execute( - "DESC EXTERNAL ACCESS INTEGRATION {name:i}", {"name": AccountObjectIdent(self.env_prefix, name)} - ) + cur = self.execute("DESC EXTERNAL ACCESS INTEGRATION {name:i}", {"name": AccountObjectIdent(self.env_prefix, name)}) return {r["property"]: r["property_value"] for r in cur} @@ -363,7 +361,7 @@ def is_edition_business_critical(self): return self.edition >= Edition.BUSINESS_CRITICAL def dtypes_from_arguments(self, arguments): - arguments = arguments.translate(str.maketrans('', '', '[] ')) + arguments = arguments.translate(str.maketrans("", "", "[] ")) start_dtypes_idx = arguments.index("(") finish_dtypes_idx = arguments.index(")") diff --git a/test/function/fn008.py b/test/function/fn008.py index 064eabb..850c53e 100644 --- a/test/function/fn008.py +++ b/test/function/fn008.py @@ -76,21 +76,21 @@ def test_step2(helper): # Compare returns signature assert ( - function_desc["returns"] == "TABLE (" - "NUM1 NUMBER, NUM2 NUMBER, " - "DBL FLOAT, " - "BIN1 BINARY, " - "VAR1 VARCHAR, " - "DT1 DATE, " - "TM1 TIME, " - "LTZ1 TIMESTAMP_LTZ, " - "NTZ1 TIMESTAMP_NTZ, " - "TZ1 TIMESTAMP_TZ, " - "VAR VARIANT, " - "OBJ OBJECT, " - "ARR ARRAY, " - "GEO1 GEOGRAPHY, " - "GEO2 GEOMETRY)" + function_desc["returns"] == "TABLE (" + "NUM1 NUMBER, NUM2 NUMBER, " + "DBL FLOAT, " + "BIN1 BINARY, " + "VAR1 VARCHAR, " + "DT1 DATE, " + "TM1 TIME, " + "LTZ1 TIMESTAMP_LTZ, " + "NTZ1 TIMESTAMP_NTZ, " + "TZ1 TIMESTAMP_TZ, " + "VAR VARIANT, " + "OBJ OBJECT, " + "ARR ARRAY, " + "GEO1 GEOGRAPHY, " + "GEO2 GEOMETRY)" ) assert function_desc["language"] == "SQL" diff --git a/test/procedure/pr004.py b/test/procedure/pr004.py index 91a6311..a96e8f8 100644 --- a/test/procedure/pr004.py +++ b/test/procedure/pr004.py @@ -69,19 +69,19 @@ def test_step2(helper): # Compare returns signature assert ( - procedure_desc["returns"] == "TABLE (" - "NUM1 NUMBER, NUM2 NUMBER, " - "DBL FLOAT, " - "BIN1 BINARY, " - "VAR1 VARCHAR, " - "DT1 DATE, " - "TM1 TIME, " - "LTZ1 TIMESTAMP_LTZ, " - "NTZ1 TIMESTAMP_NTZ, " - "TZ1 TIMESTAMP_TZ, " - "VAR VARIANT, " - "OBJ OBJECT, " - "ARR ARRAY)" + procedure_desc["returns"] == "TABLE (" + "NUM1 NUMBER, NUM2 NUMBER, " + "DBL FLOAT, " + "BIN1 BINARY, " + "VAR1 VARCHAR, " + "DT1 DATE, " + "TM1 TIME, " + "LTZ1 TIMESTAMP_LTZ, " + "NTZ1 TIMESTAMP_NTZ, " + "TZ1 TIMESTAMP_TZ, " + "VAR VARIANT, " + "OBJ OBJECT, " + "ARR ARRAY)" ) assert "[" in procedure_show["arguments"]