From ca4b8ad10b1cca4e0ddcb86af7be2d94a9530a42 Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Mon, 25 Nov 2024 09:33:59 -0600 Subject: [PATCH] perf: pass in name --- src/ape/cli/choices.py | 12 ++++++++++-- src/ape/cli/options.py | 5 ++++- src/ape/managers/plugins.py | 2 +- src/ape_console/_cli.py | 7 ++++++- tests/functional/test_cli.py | 4 +++- tests/functional/test_console.py | 21 +++++++-------------- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/ape/cli/choices.py b/src/ape/cli/choices.py index 628707b513..0962a4f1e2 100644 --- a/src/ape/cli/choices.py +++ b/src/ape/cli/choices.py @@ -8,7 +8,12 @@ import click from click import Choice, Context, Parameter -from ape.exceptions import AccountsError +from ape.exceptions import ( + AccountsError, + EcosystemNotFoundError, + NetworkNotFoundError, + ProviderNotFoundError, +) if TYPE_CHECKING: from ape.api.accounts import AccountAPI @@ -396,7 +401,10 @@ def convert(self, value: Any, param: Optional[Parameter], ctx: Optional[Context] from ape.utils.basemodel import ManagerAccessMixin as access networks = access.network_manager - value = networks.get_provider_from_choice(network_choice=value) + try: + value = networks.get_provider_from_choice(network_choice=value) + except (EcosystemNotFoundError, NetworkNotFoundError, ProviderNotFoundError) as err: + self.fail(str(err)) return self.callback(ctx, param, value) if self.callback else value diff --git a/src/ape/cli/options.py b/src/ape/cli/options.py index d279dc7aba..1c492e113f 100644 --- a/src/ape/cli/options.py +++ b/src/ape/cli/options.py @@ -264,7 +264,10 @@ def decorator(f): def callback(ctx, param, value): keep_as_choice_str = param.type.base_type is str - provider_obj = _get_provider(value, default, keep_as_choice_str) + try: + provider_obj = _get_provider(value, default, keep_as_choice_str) + except Exception as err: + raise click.BadOptionUsage("--network", str(err), ctx) if provider_obj: _update_context_with_network(ctx, provider_obj, requested_network_objects) diff --git a/src/ape/managers/plugins.py b/src/ape/managers/plugins.py index 4bcc977313..ebf3a5a76f 100644 --- a/src/ape/managers/plugins.py +++ b/src/ape/managers/plugins.py @@ -133,7 +133,7 @@ def _register_plugins(self): handled.add(module_name) try: module = import_module(module_name) - pluggy_manager.register(module) + pluggy_manager.register(module, name=module_name) except Exception as err: if module_name in CORE_PLUGINS or module_name == "ape": # Always raise core plugin registration errors. diff --git a/src/ape_console/_cli.py b/src/ape_console/_cli.py index 423492b95a..d03d02dc8d 100644 --- a/src/ape_console/_cli.py +++ b/src/ape_console/_cli.py @@ -207,10 +207,15 @@ def console( # Required for click.testing.CliRunner support. embed = True - namespace = ApeConsoleNamespace(**(extra_locals or {})) + namespace = _create_namespace(**(extra_locals or {})) _launch_console(namespace, ipy_config, embed, banner, code=code) +def _create_namespace(**values) -> dict: + # Abstracted for testing purposes. + return ApeConsoleNamespace(**values) + + def _launch_console( namespace: dict, ipy_config: "IPythonConfig", diff --git a/tests/functional/test_cli.py b/tests/functional/test_cli.py index 35ea0dc489..e7148683b9 100644 --- a/tests/functional/test_cli.py +++ b/tests/functional/test_cli.py @@ -314,7 +314,9 @@ def test_network_option_specify_custom_network( def cmd(network): click.echo(f"Value is '{getattr(network, 'name', network)}'") - result = runner.invoke(cmd, ("--network", f"ethereum:{network_name}:node")) + result = runner.invoke( + cmd, ("--network", f"ethereum:{network_name}:node"), catch_exceptions=False + ) assert result.exit_code == 0 assert f"Value is '{network_name}'" in result.output diff --git a/tests/functional/test_console.py b/tests/functional/test_console.py index a55650fea3..7451109c5b 100644 --- a/tests/functional/test_console.py +++ b/tests/functional/test_console.py @@ -4,7 +4,7 @@ from ape import Project from ape.utils import ManagerAccessMixin, create_tempdir -from ape_console._cli import console +from ape_console._cli import ApeConsoleNamespace, console from ape_console.plugin import custom_exception_handler @@ -14,28 +14,21 @@ def mock_console(mocker): return mocker.patch("ape_console._cli._launch_console") -@pytest.fixture(autouse=True) -def mock_ape_console_extras(mocker): - """Prevent actually loading console extras files.""" - return mocker.patch("ape_console._cli.load_console_extras") - - -def test_console_extras_uses_ape_namespace(mocker, mock_console, mock_ape_console_extras): +def test_console_extras_uses_ape_namespace(mocker, mock_console): """ Test that if console is given extras, those are included in the console but not as args to the extras files, as those files expect items from the default ape namespace. """ + namespace_patch = mocker.patch("ape_console._cli._create_namespace") accounts_custom = mocker.MagicMock() extras = {"accounts": accounts_custom} console(extra_locals=extras) + actual = namespace_patch.call_args[1] + assert actual["accounts"] == accounts_custom - # Show extras file still load using Ape namespace. - actual = mock_ape_console_extras.call_args[1] - assert actual["accounts"] != accounts_custom - - # Show the custom accounts do get used in console. - assert mock_console.call_args[0][0]["accounts"] == accounts_custom + extras = ApeConsoleNamespace() + assert extras["accounts"] def test_console_custom_project(mock_console, mock_ape_console_extras):