Skip to content

Commit

Permalink
perf: pass in name
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Nov 25, 2024
1 parent 9d0b4e0 commit ca4b8ad
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
12 changes: 10 additions & 2 deletions src/ape/cli/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion src/ape/cli/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/ape/managers/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion src/ape_console/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion tests/functional/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 7 additions & 14 deletions tests/functional/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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):
Expand Down

0 comments on commit ca4b8ad

Please sign in to comment.