Skip to content

Commit

Permalink
Merge pull request #974 from guardrails-ai/typer-version
Browse files Browse the repository at this point in the history
Typer Version & Bug Fix
  • Loading branch information
CalebCourier authored Jul 29, 2024
2 parents abd3c3d + f715ca4 commit fb6c157
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 12 deletions.
2 changes: 1 addition & 1 deletion guardrails/cli/hub/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def do_nothing_context(*args, **kwargs):
if has_rc_file:
# if we do want to remote then we don't want to install local models
use_remote_endpoint = (
not Credentials.from_rc_file(logger).use_remote_inferencing
Credentials.from_rc_file(logger).use_remote_inferencing
and module_has_endpoint
)
elif install_local_models is None and module_has_endpoint:
Expand Down
5 changes: 3 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ lxml = "^4.9.3"
openai = "^1.30.1"
rich = "^13.6.0"
pydantic = ">=2.0.0, <3.0"
typer = {extras = ["all"], version = "^0.9.0"}
typer = {extras = ["all"], version = ">=0.9.0, <0.13"}
griffe = "^0.36.9"
tenacity = ">=8.1.0"
regex = "^2023.10.3"
Expand Down
155 changes: 147 additions & 8 deletions tests/unit_tests/cli/hub/test_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@
import pytest
from typer.testing import CliRunner

from guardrails.classes.credentials import Credentials
from guardrails.cli.hub.install import hub_command, install
from guardrails.cli.server.module_manifest import ModuleManifest
from tests.unit_tests.mocks.mock_file import MockFile


@pytest.mark.parametrize(
"use_remote_inferencing",
[False, True],
)
class TestInstall:
def test_exits_early_if_uri_is_not_valid(self, mocker):
def test_exits_early_if_uri_is_not_valid(self, mocker, use_remote_inferencing):
mock_logger_error = mocker.patch("guardrails.cli.hub.install.logger.error")

mocker.patch(
"guardrails.cli.hub.install.Credentials.has_rc_file",
return_value=True,
)

from guardrails.cli.hub.install import sys

sys_exit_spy = mocker.spy(sys, "exit")
Expand All @@ -22,9 +32,23 @@ def test_exits_early_if_uri_is_not_valid(self, mocker):
mock_logger_error.assert_called_once_with("Invalid URI!")
sys_exit_spy.assert_called_once_with(1)

def test_install_local_models__false(self, mocker, monkeypatch):
def test_install_local_models__false(
self, mocker, monkeypatch, use_remote_inferencing
):
mock_logger_log = mocker.patch("guardrails.cli.hub.install.logger.log")

mocker.patch(
"guardrails.cli.hub.install.Credentials.has_rc_file",
return_value=True,
)

mocker.patch(
"guardrails.cli.hub.install.Credentials.from_rc_file",
return_value=Credentials.from_dict(
{"use_remote_inferencing": use_remote_inferencing}
),
)

mock_get_validator_manifest = mocker.patch(
"guardrails.cli.hub.install.get_validator_manifest"
)
Expand Down Expand Up @@ -86,9 +110,23 @@ def test_install_local_models__false(self, mocker, monkeypatch):

mock_add_to_hub_init.assert_called_once_with(manifest, site_packages)

def test_install_local_models__true(self, mocker, monkeypatch):
def test_install_local_models__true(
self, mocker, monkeypatch, use_remote_inferencing
):
mock_logger_log = mocker.patch("guardrails.cli.hub.install.logger.log")

mocker.patch(
"guardrails.cli.hub.install.Credentials.has_rc_file",
return_value=True,
)

mocker.patch(
"guardrails.cli.hub.install.Credentials.from_rc_file",
return_value=Credentials.from_dict(
{"use_remote_inferencing": use_remote_inferencing}
),
)

mock_get_validator_manifest = mocker.patch(
"guardrails.cli.hub.install.get_validator_manifest"
)
Expand Down Expand Up @@ -149,9 +187,23 @@ def test_install_local_models__true(self, mocker, monkeypatch):

mock_add_to_hub_init.assert_called_once_with(manifest, site_packages)

def test_install_local_models__none(self, mocker, monkeypatch):
def test_install_local_models__none(
self, mocker, monkeypatch, use_remote_inferencing
):
mock_logger_log = mocker.patch("guardrails.cli.hub.install.logger.log")

mocker.patch(
"guardrails.cli.hub.install.Credentials.has_rc_file",
return_value=True,
)

mocker.patch(
"guardrails.cli.hub.install.Credentials.from_rc_file",
return_value=Credentials.from_dict(
{"use_remote_inferencing": use_remote_inferencing}
),
)

mock_get_validator_manifest = mocker.patch(
"guardrails.cli.hub.install.get_validator_manifest"
)
Expand Down Expand Up @@ -209,9 +261,21 @@ def test_install_local_models__none(self, mocker, monkeypatch):

mock_add_to_hub_init.assert_called_once_with(manifest, site_packages)

def test_happy_path(self, mocker, monkeypatch):
def test_happy_path(self, mocker, monkeypatch, use_remote_inferencing):
mock_logger_log = mocker.patch("guardrails.cli.hub.install.logger.log")

mocker.patch(
"guardrails.cli.hub.install.Credentials.has_rc_file",
return_value=True,
)

mocker.patch(
"guardrails.cli.hub.install.Credentials.from_rc_file",
return_value=Credentials.from_dict(
{"use_remote_inferencing": use_remote_inferencing}
),
)

mock_get_validator_manifest = mocker.patch(
"guardrails.cli.hub.install.get_validator_manifest"
)
Expand All @@ -226,7 +290,7 @@ def test_happy_path(self, mocker, monkeypatch):
"package_name": "test-validator",
"module_name": "test_validator",
"exports": ["TestValidator"],
"tags": {"has_guardrails_endpoint": True},
"tags": {"has_guardrails_endpoint": False},
}
)
mock_get_validator_manifest.return_value = manifest
Expand Down Expand Up @@ -260,12 +324,16 @@ def test_happy_path(self, mocker, monkeypatch):

assert mock_get_site_packages_location.call_count == 1

def test_install_local_models_confirmation(self, mocker):
def test_install_local_models_confirmation(self, mocker, use_remote_inferencing):
# Mock dependencies
mocker.patch("guardrails.cli.hub.install.get_site_packages_location")
mocker.patch("guardrails.cli.hub.install.install_hub_module")
mocker.patch("guardrails.cli.hub.install.run_post_install")
mocker.patch("guardrails.cli.hub.install.add_to_hub_inits")
mocker.patch(
"guardrails.cli.hub.install.Credentials.has_rc_file",
return_value=False,
)

# Create a manifest with Guardrails endpoint
manifest_with_endpoint = ModuleManifest.from_dict(
Expand All @@ -279,7 +347,7 @@ def test_install_local_models_confirmation(self, mocker):
"package_name": "test-package",
"module_name": "test_module",
"exports": ["TestValidator"],
"tags": {"has_guardrails_endpoint": False},
"tags": {"has_guardrails_endpoint": True},
}
)
mocker.patch(
Expand All @@ -297,6 +365,77 @@ def test_install_local_models_confirmation(self, mocker):
# Check if the installation was successful
assert result.exit_code == 0

def test_use_remote_endpoint(
self, mocker, monkeypatch, use_remote_inferencing: bool
):
mock_logger_log = mocker.patch("guardrails.cli.hub.install.logger.log")

mocker.patch(
"guardrails.cli.hub.install.Credentials.has_rc_file",
return_value=True,
)

mocker.patch(
"guardrails.cli.hub.install.Credentials.from_rc_file",
return_value=Credentials.from_dict(
{"use_remote_inferencing": use_remote_inferencing}
),
)

mock_get_validator_manifest = mocker.patch(
"guardrails.cli.hub.install.get_validator_manifest"
)
manifest = ModuleManifest.from_dict(
{
"id": "id",
"name": "name",
"author": {"name": "me", "email": "me@me.me"},
"maintainers": [],
"repository": {"url": "some-repo"},
"namespace": "guardrails",
"package_name": "test-validator",
"module_name": "test_validator",
"exports": ["TestValidator"],
"tags": {"has_guardrails_endpoint": True},
}
)
mock_get_validator_manifest.return_value = manifest

mock_get_site_packages_location = mocker.patch(
"guardrails.cli.hub.install.get_site_packages_location"
)
site_packages = "./.venv/lib/python3.X/site-packages"
mock_get_site_packages_location.return_value = site_packages

mocker.patch("guardrails.cli.hub.install.install_hub_module")
mocker.patch("guardrails.cli.hub.install.run_post_install")
mocker.patch("guardrails.cli.hub.install.add_to_hub_inits")

runner = CliRunner()

runner.invoke(hub_command, ["install", "hub://guardrails/test-validator"])

msg = (
"Skipping post install, models will not be downloaded for local inference."
if use_remote_inferencing
else "Installing models locally!"
)

log_calls = [
call(level=5, msg="Installing hub://guardrails/test-validator..."),
call(
level=5,
msg=msg, # noqa
), # noqa
]

assert mock_logger_log.call_count == 3
mock_logger_log.assert_has_calls(log_calls)

mock_get_validator_manifest.assert_called_once_with("guardrails/test-validator")

assert mock_get_site_packages_location.call_count == 1


class TestPipProcess:
def test_no_package_string_format(self, mocker):
Expand Down

0 comments on commit fb6c157

Please sign in to comment.