Skip to content

Commit

Permalink
allow only kwargs with Beaker.experiment.create() (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh authored Dec 15, 2023
1 parent 5cdc035 commit da3d95a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use patch releases for compatibility fixes instead.

## Unreleased

### Fixed

- Allow only key-word arguments with `Beaker.experiment.create()`.

## [v1.22.0](https://github.com/allenai/beaker-py/releases/tag/v1.22.0) - 2023-09-25

### Changed
Expand Down
39 changes: 37 additions & 2 deletions beaker/services/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,38 @@ def _parse_create_args(
spec: Optional[Union[ExperimentSpec, PathOrStr]] = kwargs.pop("spec", None)
name: Optional[str] = kwargs.pop("name", None)
workspace: Optional[Union[Workspace, str]] = kwargs.pop("workspace", None)
if len(args) == 2:
if len(args) == 3:
if name is not None or spec is not None or workspace is not None:
raise TypeError(
"ExperimentClient.create() got an unexpected number of positional arguments"
)
if (
isinstance(args[0], str)
and isinstance(args[1], (ExperimentSpec, Path, str))
and isinstance(
args[2],
(
str,
Workspace,
),
)
):
name, spec, workspace = args
elif (
isinstance(args[0], (ExperimentSpec, Path, str))
and isinstance(args[1], str)
and isinstance(
args[2],
(
str,
Workspace,
),
)
):
spec, name, workspace = args
else:
raise TypeError("ExperimentClient.create() got an unexpected positional argument")
elif len(args) == 2:
if name is not None or spec is not None:
raise TypeError(
"ExperimentClient.create() got an unexpected number of positional arguments"
Expand All @@ -90,14 +121,18 @@ def _parse_create_args(
name = args[0]
else:
raise TypeError("ExperimentClient.create() got an unexpected positional argument")
else:
elif len(args) != 0:
raise TypeError(
"ExperimentClient.create() got an unexpected number of positional arguments"
)
elif spec is None:
raise TypeError("ExperimentClient.create() is missing 1 required argument: 'spec'")

if kwargs:
raise TypeError(
f"ExperimentClient.create() got unexpected keyword arguments {tuple(kwargs.keys())}"
)

assert spec is not None
return spec, name, workspace

Expand Down
18 changes: 18 additions & 0 deletions tests/experiment_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ def test_parse_create_args(client: Beaker):
assert name == "my-experiment"
assert spec is not None

spec, name, workspace = client.experiment._parse_create_args(
name="my-experiment",
spec=ExperimentSpec.new(docker_image="hello-world"),
workspace="ai2/petew",
)
assert workspace == "ai2/petew"
assert name == "my-experiment"
assert spec is not None

spec, name, workspace = client.experiment._parse_create_args(
"my-experiment",
ExperimentSpec.new(docker_image="hello-world"),
"ai2/petew",
)
assert workspace == "ai2/petew"
assert name == "my-experiment"
assert spec is not None


def test_experiment_get(client: Beaker, hello_world_experiment_id: str):
exp = client.experiment.get(hello_world_experiment_id)
Expand Down

0 comments on commit da3d95a

Please sign in to comment.