From a13e1840b93758dc9bd15d512c2b7fb77fe45174 Mon Sep 17 00:00:00 2001 From: Joseph Catrambone Date: Mon, 8 Jul 2024 16:27:08 -0700 Subject: [PATCH 1/4] Bugfix: when 'None' is passed explicitly to _set_num_reasks we don't use the default of '1'. --- guardrails/guard.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/guardrails/guard.py b/guardrails/guard.py index fc8546ec8..d4c7244ec 100644 --- a/guardrails/guard.py +++ b/guardrails/guard.py @@ -235,8 +235,15 @@ def configure( self._set_tracer(tracer) self._configure_telemtry(allow_metrics_collection) - def _set_num_reasks(self, num_reasks: Optional[int] = 1): - self._num_reasks = num_reasks + def _set_num_reasks(self, num_reasks: Optional[int]): + # Configure may check if num_reasks is none, but this method still needs to be + # defensive for when it's called internally. Setting a default parameter + # doesn't help the case where the method is explicitly passed a 'None'. + if num_reasks is None: + logger.info("_set_num_reasks called with 'None'. Defaulting to 1.") + self._num_reasks = 1 + else: + self._num_reasks = num_reasks def _set_tracer(self, tracer: Optional[Tracer] = None) -> None: self._tracer = tracer From 910dd11a46c22c9e2ec001f5e90efa769939eac3 Mon Sep 17 00:00:00 2001 From: Joseph Catrambone Date: Mon, 8 Jul 2024 16:49:06 -0700 Subject: [PATCH 2/4] Swap default=1 to default=None. --- guardrails/guard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guardrails/guard.py b/guardrails/guard.py index d4c7244ec..21b82721c 100644 --- a/guardrails/guard.py +++ b/guardrails/guard.py @@ -235,7 +235,7 @@ def configure( self._set_tracer(tracer) self._configure_telemtry(allow_metrics_collection) - def _set_num_reasks(self, num_reasks: Optional[int]): + def _set_num_reasks(self, num_reasks: Optional[int] = None) -> None: # Configure may check if num_reasks is none, but this method still needs to be # defensive for when it's called internally. Setting a default parameter # doesn't help the case where the method is explicitly passed a 'None'. From 332ea528fcb71bef35c2ba8e4013f632e106c3ef Mon Sep 17 00:00:00 2001 From: Joseph Catrambone Date: Tue, 9 Jul 2024 10:54:20 -0700 Subject: [PATCH 3/4] Make --validators an optional parameter. Create an empty config file without it. --- guardrails/cli/create.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/guardrails/cli/create.py b/guardrails/cli/create.py index 1486b1f2c..e1ce76410 100644 --- a/guardrails/cli/create.py +++ b/guardrails/cli/create.py @@ -22,8 +22,9 @@ @gr_cli.command(name="create") def create_command( - validators: str = typer.Option( - help="A comma-separated list of validator hub URIs. ", + validators: Optional[str] = typer.Option( + default=None, + help="A comma-separated list of validator hub URIs.", ), name: Optional[str] = typer.Option( default=None, help="The name of the guard to define in the file." @@ -74,7 +75,11 @@ def check_filename(filename: Union[str, os.PathLike]) -> str: def split_and_install_validators(validators: str, dry_run: bool = False): """Given a comma-separated list of validators, check the hub to make sure all of - them exist, install them, and return a list of 'imports'.""" + them exist, install them, and return a list of 'imports'. If validators is empty, + returns an empty list.""" + if not validators: + return [] + stripped_validators = list() manifests = list() site_packages = get_site_packages_location() @@ -123,7 +128,7 @@ def generate_config_file(validators: List[str], name: Optional[str] = None) -> s # Import one or more validators. if len(validators) == 1: config_lines.append(f"from guardrails.hub import {validators[0]}") - else: + elif len(validators) > 1: multiline_import = ",\n\t".join(validators) config_lines.append(f"from guardrails.hub import (\n\t{multiline_import}\n)") @@ -135,7 +140,7 @@ def generate_config_file(validators: List[str], name: Optional[str] = None) -> s # Append validators: if len(validators) == 1: config_lines.append(f"guard.use({validators[0]}( TODO Fill these parameters ))") - else: + elif len(validators) > 1: multi_use = ",\n".join( [ "\t" + validator + "( TODO fill these parameters )" From 8327b4f6b21a01c2c21e99d015f98da6e2bdd72d Mon Sep 17 00:00:00 2001 From: Joseph Catrambone Date: Tue, 9 Jul 2024 11:21:00 -0700 Subject: [PATCH 4/4] Fixing lint. --- guardrails/cli/create.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/guardrails/cli/create.py b/guardrails/cli/create.py index e1ce76410..7241909be 100644 --- a/guardrails/cli/create.py +++ b/guardrails/cli/create.py @@ -22,8 +22,8 @@ @gr_cli.command(name="create") def create_command( - validators: Optional[str] = typer.Option( - default=None, + validators: str = typer.Option( + default="", help="A comma-separated list of validator hub URIs.", ), name: Optional[str] = typer.Option(