Skip to content

Commit

Permalink
Make metric check cfg contructor flexy
Browse files Browse the repository at this point in the history
  • Loading branch information
m1n0 committed May 23, 2024
1 parent 321169c commit 91550fe
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions soda/core/soda/sodacl/sodacl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from numbers import Number
from textwrap import dedent
from typing import List
import inspect

from antlr4 import CommonTokenStream, InputStream
from antlr4.error.ErrorListener import ErrorListener
Expand Down Expand Up @@ -926,25 +927,39 @@ def __parse_metric_check(
f"Invalid syntax used in '{check_str}'. More than one check attribute is not supported. A check like this will be skipped in future versions of Soda Core"
)

return metric_check_cfg_class(
source_header=header_str,
source_line=check_str,
source_configurations=check_configurations,
location=self.location,
name=name,
metric_name=metric_name,
metric_args=metric_args,
missing_and_valid_cfg=missing_and_valid_cfg,
filter=filter,
condition=condition,
metric_expression=metric_expression,
metric_query=metric_query,
change_over_time_cfg=change_over_time_cfg,
fail_threshold_cfg=fail_threshold_cfg,
warn_threshold_cfg=warn_threshold_cfg,
samples_limit=samples_limit,
failed_rows_query=failed_rows_query,
)
def takes_keyword_argument(cls, keyword):
signature = inspect.signature(cls.__init__)
return keyword in signature.parameters

# Some arguments make no sense for certain metric checks, so we only pass the ones that are supported by the given class constructor.
# Do this instead of accepting kwargs and passing all arguments to the constructor, because it's easier to see what arguments are supported and they do not disappear in the constructor.
all_args = {
"source_header": header_str,
"source_line": check_str,
"source_configurations": check_configurations,
"location": self.location,
"name": name,
"metric_name": metric_name,
"metric_args": metric_args,
"missing_and_valid_cfg": missing_and_valid_cfg,
"filter": filter,
"condition": condition,
"metric_expression": metric_expression,
"metric_query": metric_query,
"change_over_time_cfg": change_over_time_cfg,
"fail_threshold_cfg": fail_threshold_cfg,
"warn_threshold_cfg": warn_threshold_cfg,
"samples_limit": samples_limit,
"failed_rows_query": failed_rows_query,
}

use_args = {}

for arg in all_args.keys():
if takes_keyword_argument(metric_check_cfg_class, arg):
use_args[arg] = all_args[arg]

return metric_check_cfg_class(**use_args)

def __parse_configuration_threshold_condition(self, value) -> ThresholdCfg | None:
if isinstance(value, str):
Expand Down

0 comments on commit 91550fe

Please sign in to comment.