Skip to content

Commit

Permalink
Remove styxdefs generics
Browse files Browse the repository at this point in the history
  • Loading branch information
nx10 committed May 9, 2024
1 parent a61d16d commit 167c2dc
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/styx/compiler/compile/descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ def _generate_run_function(
# Function
func = PyFunc(
name=symbols.function,
return_type=f"{symbols.output_class}[R]",
return_type=symbols.output_class,
return_descr=f"NamedTuple of outputs " f"(described in `{symbols.output_class}`).",
docstring_body=command.doc,
)
module.funcs.append(func)

# Function arguments
func.args.append(PyArg(name="runner", type="Runner[P, R]", default=None, docstring="Command runner"))
func.args.append(PyArg(name="runner", type="Runner", default=None, docstring="Command runner"))
func.args.extend(build_input_arguments(inputs, sub_aliases))

# Constraint checking
Expand Down Expand Up @@ -80,8 +80,8 @@ def compile_descriptor(descriptor: Descriptor, settings: CompilerSettings) -> st

# Module level symbols
scopes.module.add_or_die("styx")
scopes.module.add_or_die("P")
scopes.module.add_or_die("R")
scopes.module.add_or_die("InputFileType")
scopes.module.add_or_die("OutputFileType")
scopes.module.add_or_die("Runner")
scopes.module.add_or_die("Execution")
scopes.module.add_or_die("Metadata")
Expand Down
2 changes: 1 addition & 1 deletion src/styx/compiler/compile/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def _base() -> str:
case InputTypePrimitive.Integer:
return "int"
case InputTypePrimitive.File:
return "P"
return "InputPathType"
case InputTypePrimitive.Flag:
return "bool"
case InputTypePrimitive.SubCommand:
Expand Down
4 changes: 2 additions & 2 deletions src/styx/compiler/compile/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def generate_outputs_definition(
module.header.extend([
"",
"",
f"class {symbols.output_class}(typing.NamedTuple, typing.Generic[R]):",
f"class {symbols.output_class}(typing.NamedTuple):",
*indent([
'"""',
f"Output object returned when calling `{symbols.function}(...)`.",
Expand All @@ -24,7 +24,7 @@ def generate_outputs_definition(
# Declaration
module.header.extend(
indent([
f"{out.symbol}: R",
f"{out.symbol}: OutputPathType",
f'"""{out.data.doc}"""',
])
)
Expand Down
2 changes: 1 addition & 1 deletion src/styx/compiler/compile/subcommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _generate_sub_command(
docstring_body="Build command line arguments. This method is called by the main command.",
args=[
PyArg(name="self", type=None, default=None, docstring="The sub-command object."),
PyArg(name="execution", type="Execution[P, R]", default=None, docstring="The execution object."),
PyArg(name="execution", type="Execution", default=None, docstring="The execution object."),
],
return_type="list[str]",
body=[
Expand Down
12 changes: 6 additions & 6 deletions src/styx/runners/dummy.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
from styx.runners.styxdefs import Execution, Metadata, Runner
from styx.runners.styxdefs import Execution, InputPathType, Metadata, OutputPathType, Runner


class DummyRunner(Runner[str, str], Execution[str, str]):
class DummyRunner(Runner, Execution):
def __init__(self) -> None:
self.last_cargs: list[str] | None = None
self.last_metadata: Metadata | None = None

def start_execution(self, metadata: Metadata) -> Execution[str, str]:
def start_execution(self, metadata: Metadata) -> Execution:
self.last_metadata = metadata
return self

def input_file(self, host_file: str) -> str:
return host_file
def input_file(self, host_file: InputPathType) -> str:
return str(host_file)

def output_file(self, local_file: str, optional: bool = False) -> str:
def output_file(self, local_file: str, optional: bool = False) -> OutputPathType:
return local_file

def run(self, cargs: list[str]) -> None:
Expand Down
15 changes: 8 additions & 7 deletions src/styx/runners/styxdefs.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import pathlib
import typing

P = typing.TypeVar("P", contravariant=True)
InputPathType: typing.TypeAlias = pathlib.Path | str
"""Input host file type."""
R = typing.TypeVar("R", covariant=True)
OutputPathType: typing.TypeAlias = pathlib.Path | str
"""Output host file type."""


class Execution(typing.Protocol[P, R]):
class Execution(typing.Protocol):
"""Execution object used to execute commands.
Created by `Runner.start_execution()`.
"""

def input_file(self, host_file: P) -> str:
def input_file(self, host_file: InputPathType) -> str:
"""Resolve host input files.
Returns a local filepath.
Expand All @@ -29,7 +30,7 @@ def run(self, cargs: list[str]) -> None:
"""
...

def output_file(self, local_file: str, optional: bool = False) -> R:
def output_file(self, local_file: str, optional: bool = False) -> OutputPathType:
"""Resolve local output files.
Returns a host filepath.
Expand Down Expand Up @@ -60,15 +61,15 @@ class Metadata(typing.NamedTuple):
"""Container-level arguments for the application. Example: --privileged"""


class Runner(typing.Protocol[P, R]):
class Runner(typing.Protocol):
"""Runner object used to execute commands.
Possible examples would be `LocalRunner`,
`DockerRunner`, `DebugRunner`, ...
Used as a factory for `Execution` objects.
"""

def start_execution(self, metadata: Metadata) -> Execution[P, R]:
def start_execution(self, metadata: Metadata) -> Execution:
"""Start an execution.
Called before any `Execution.input_file()` calls.
Expand Down

0 comments on commit 167c2dc

Please sign in to comment.