Skip to content

Commit 8df2898

Browse files
committed
use full import paths for tests/utils.py files
1 parent 34061f4 commit 8df2898

34 files changed

+160
-95
lines changed

.github/workflows/ci-cd.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ jobs:
7373
run: echo "NO_ET=TRUE" >> $GITHUB_ENV
7474
- name: Pytest
7575
run: |
76-
pytest -vs -n auto pydra --doctest-modules --cov pydra --cov-config .coveragerc --cov-report xml:cov.xml --rootdir pydra
76+
pytest -vs -n auto pydra --doctest-modules --import-mode=importlib --cov pydra --cov-config .coveragerc --cov-report xml:cov.xml --rootdir pydra
7777
- name: Upload coverage to Codecov
7878
uses: codecov/codecov-action@v2
7979
with:
@@ -133,7 +133,7 @@ jobs:
133133
- name: Install pydra (test)
134134
run: pip install -e ".[test]"
135135
- name: Pytest
136-
run: pytest -vs --cov pydra --cov-config .coveragerc --cov-report xml:cov.xml pydra/engine/tests/test_singularity.py pydra/engine/tests/test_environments.py --rootdir pydra
136+
run: pytest -vs --import-mode=importlib --cov pydra --cov-config .coveragerc --cov-report xml:cov.xml pydra/engine/tests/test_singularity.py pydra/engine/tests/test_environments.py --rootdir pydra
137137
- name: Upload coverage to Codecov
138138
uses: codecov/codecov-action@v2
139139
with:
@@ -181,7 +181,7 @@ jobs:
181181
docker exec slurm bash -c "pip install --upgrade pip && pip install -e /pydra[test,psij] && python -c 'import pydra.engine; print(pydra.utils.__version__)'"
182182
- name: Run pytest
183183
run: |
184-
docker exec slurm bash -c "pytest /pydra/pydra/engine/tests/test_submitter.py --rootdir /pydra/pydra --only-worker=slurm --color=yes -vs --cov pydra --cov-config /pydra/.coveragerc --cov-report xml:/pydra/cov.xml"
184+
docker exec slurm bash -c "pytest /pydra/pydra/engine/tests/test_submitter.py --import-mode=importlib --rootdir /pydra/pydra --only-worker=slurm --color=yes -vs --cov pydra --cov-config /pydra/.coveragerc --cov-report xml:/pydra/cov.xml"
185185
- name: Upload coverage to Codecov
186186
uses: codecov/codecov-action@v2
187187
with:
@@ -261,7 +261,7 @@ jobs:
261261
# pip install --upgrade pip && pip install -e .[test] && python -c 'import pydra.engine; print(pydra.utils.__version__)'
262262
# - name: Run pytest
263263
# run: |
264-
# pytest pydra/engine/tests/test_submitter.py --rootdir . --only-worker=sge --color=yes -vs --cov pydra --cov-config .coveragerc --cov-report xml:cov.xml
264+
# pytest pydra/engine/tests/test_submitter.py --import-mode=importlib --rootdir . --only-worker=sge --color=yes -vs --cov pydra --cov-config .coveragerc --cov-report xml:cov.xml
265265
# - name: Upload coverage to Codecov
266266
# uses: codecov/codecov-action@v2
267267
# with:

docs/source/tutorial/7-canonical-form.ipynb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@
194194
"metadata": {},
195195
"outputs": [],
196196
"source": [
197+
"import typing as ty\n",
198+
"import re\n",
197199
"from pydra.compose import python, workflow\n",
200+
"from pydra.compose.base import is_set\n",
198201
"from pydra.utils import print_help, show_workflow\n",
199202
"\n",
200203
"\n",
@@ -213,16 +216,13 @@
213216
"class CanonicalWorkflowTask(workflow.Task[\"CanonicalWorkflowTask.Outputs\"]):\n",
214217
"\n",
215218
" @staticmethod\n",
216-
" def a_converter(value):\n",
217-
" if value is None:\n",
218-
" return value\n",
219-
" return float(value)\n",
219+
" def str2num(value: ty.Any) -> float | int:\n",
220+
" if isinstance(value, str) and re.match(r\"^\\d+(\\.\\d+)?$\", value):\n",
221+
" return eval(value) # use eval to convert string to number\n",
222+
" return value\n",
220223
"\n",
221224
" a: int\n",
222-
" b: float = workflow.arg(\n",
223-
" help=\"A float input\",\n",
224-
" converter=a_converter,\n",
225-
" )\n",
225+
" b: float = workflow.arg(help=\"A float input\", converter=str2num)\n",
226226
"\n",
227227
" @staticmethod\n",
228228
" def constructor(a, b):\n",

docs/source/tutorial/tst.py

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
1-
import os
2-
from pathlib import Path
3-
from fileformats.generic import File
4-
from pydra.compose import shell
5-
from pydra.utils import print_help
6-
7-
8-
# Arguments to the callable function can be one of
9-
def get_file_size(out_file: Path) -> int:
10-
"""Calculate the file size"""
11-
result = os.stat(out_file)
12-
return result.st_size
13-
14-
15-
ACommand = shell.define(
16-
"a-command",
17-
inputs={
18-
"in_file": shell.arg(type=File, help="output file", argstr="", position=-2)
19-
},
20-
outputs={
21-
"out_file": shell.outarg(type=File, help="output file", argstr="", position=-1),
22-
"out_file_size": {
23-
"type": int,
24-
"help": "size of the output directory",
25-
"callable": get_file_size,
26-
},
27-
},
28-
)
29-
30-
print_help(ACommand)
1+
from pydra.compose import python, workflow
2+
from pydra.utils import print_help, show_workflow
3+
4+
5+
# Example python tasks
6+
@python.define
7+
def Add(a, b):
8+
return a + b
9+
10+
11+
@python.define
12+
def Mul(a, b):
13+
return a * b
14+
15+
16+
@workflow.define
17+
class CanonicalWorkflowTask(workflow.Task["CanonicalWorkflowTask.Outputs"]):
18+
19+
@staticmethod
20+
def a_converter(value):
21+
try:
22+
return float(value)
23+
except (TypeError, ValueError):
24+
return value
25+
26+
a: int
27+
b: float = workflow.arg(
28+
help="A float input",
29+
converter=a_converter,
30+
)
31+
32+
@staticmethod
33+
def constructor(a, b):
34+
add = workflow.add(Add(a=a, b=b))
35+
mul = workflow.add(Mul(a=add.out, b=b))
36+
return mul.out
37+
38+
class Outputs(workflow.Outputs):
39+
out: float
40+
41+
42+
print_help(CanonicalWorkflowTask)
43+
show_workflow(CanonicalWorkflowTask)

pydra/compose/base/task.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
if ty.TYPE_CHECKING:
2626
from pydra.engine.job import Job
27-
from pydra.environments.base_environment import Environment
28-
from pydra.workers.base_worker import Worker
27+
from pydra.environments.base import Environment
28+
from pydra.workers.base import Worker
2929
from pydra.engine.result import Result
3030
from pydra.engine.hooks import TaskHooks
3131

pydra/compose/python.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,10 @@ def make(wrapped: ty.Callable | type) -> Task:
196196

197197

198198
@attrs.define(kw_only=True, auto_attribs=False, eq=False, repr=False)
199-
class Outputs(base.Outputs):
199+
class PythonOutputs(base.Outputs):
200200

201201
@classmethod
202-
def _from_task(cls, job: "Job[Task]") -> ty.Self:
202+
def _from_task(cls, job: "Job[PythonTask]") -> ty.Self:
203203
"""Collect the outputs of a job from a combination of the provided inputs,
204204
the objects in the output directory, and the stdout and stderr of the process.
205205
@@ -221,15 +221,15 @@ def _from_task(cls, job: "Job[Task]") -> ty.Self:
221221
return outputs
222222

223223

224-
PythonOutputsType = ty.TypeVar("OutputType", bound=Outputs)
224+
PythonOutputsType = ty.TypeVar("OutputType", bound=PythonOutputs)
225225

226226

227227
@attrs.define(kw_only=True, auto_attribs=False, eq=False, repr=False)
228-
class Task(base.Task[PythonOutputsType]):
228+
class PythonTask(base.Task[PythonOutputsType]):
229229

230230
_task_type = "python"
231231

232-
def _run(self, job: "Job[Task]", rerun: bool = True) -> None:
232+
def _run(self, job: "Job[PythonTask]", rerun: bool = True) -> None:
233233
# Prepare the inputs to the function
234234
inputs = attrs_values(self)
235235
del inputs["function"]
@@ -252,3 +252,8 @@ def _run(self, job: "Job[Task]", rerun: bool = True) -> None:
252252
raise RuntimeError(
253253
f"expected {len(return_names)} elements, but {returned} were returned"
254254
)
255+
256+
257+
# Alias ShellTask to Task so we can refer to it by shell.Task
258+
Task = PythonTask
259+
Outputs = PythonOutputs

pydra/compose/shell/task.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def additional_args_converter(value: ty.Any) -> list[str]:
230230

231231

232232
@attrs.define(kw_only=True, auto_attribs=False, eq=False, repr=False)
233-
class Task(base.Task[ShellOutputsType]):
233+
class ShellTask(base.Task[ShellOutputsType]):
234234

235235
_task_type = "shell"
236236

@@ -252,7 +252,7 @@ class Task(base.Task[ShellOutputsType]):
252252

253253
RESERVED_FIELD_NAMES = base.Task.RESERVED_FIELD_NAMES + ("cmdline",)
254254

255-
def _run(self, job: "Job[Task]", rerun: bool = True) -> None:
255+
def _run(self, job: "Job[ShellTask]", rerun: bool = True) -> None:
256256
"""Run the shell command."""
257257
job.return_values = job.environment.execute(job)
258258

@@ -504,3 +504,7 @@ def split_cmd(cmd: str | None):
504504
else:
505505
cmd_args.append(arg)
506506
return cmd_args
507+
508+
509+
# Alias ShellTask to Task so we can refer to it by shell.Task
510+
Task = ShellTask

pydra/compose/workflow.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
from pydra.engine.lazy import LazyOutField
2121
from pydra.engine.graph import DiGraph
2222
from pydra.engine.submitter import NodeExecution
23-
from pydra.environments.base_environment import Environment
23+
from pydra.environments.base import Environment
2424
from pydra.engine.hooks import TaskHooks
2525

2626

27-
__all__ = ["define", "add", "this", "arg", "out"]
27+
__all__ = ["define", "add", "this", "arg", "out", "Task", "Outputs", "cast"]
2828

2929

3030
@attrs.define
@@ -284,10 +284,10 @@ def cast(field: ty.Any, new_type: type[U]) -> U:
284284

285285

286286
@attrs.define(kw_only=True, auto_attribs=False, eq=False, repr=False)
287-
class Outputs(base.Outputs):
287+
class WorkflowOutputs(base.Outputs):
288288

289289
@classmethod
290-
def _from_task(cls, job: "Job[Task]") -> ty.Self:
290+
def _from_task(cls, job: "Job[WorkflowTask]") -> ty.Self:
291291
"""Collect the outputs of a workflow job from the outputs of the nodes in the
292292
293293
Parameters
@@ -342,23 +342,23 @@ def _from_task(cls, job: "Job[Task]") -> ty.Self:
342342
return outputs
343343

344344

345-
WorkflowOutputsType = ty.TypeVar("OutputType", bound=Outputs)
345+
WorkflowOutputsType = ty.TypeVar("OutputType", bound=WorkflowOutputs)
346346

347347

348348
@attrs.define(kw_only=True, auto_attribs=False, eq=False, repr=False)
349-
class Task(base.Task[WorkflowOutputsType]):
349+
class WorkflowTask(base.Task[WorkflowOutputsType]):
350350

351351
_task_type = "workflow"
352352

353353
RESERVED_FIELD_NAMES = base.Task.RESERVED_FIELD_NAMES + ("construct",)
354354

355355
_constructed = attrs.field(default=None, init=False, repr=False, eq=False)
356356

357-
def _run(self, job: "Job[Task]", rerun: bool) -> None:
357+
def _run(self, job: "Job[WorkflowTask]", rerun: bool) -> None:
358358
"""Run the workflow."""
359359
job.submitter.expand_workflow(job, rerun)
360360

361-
async def _run_async(self, job: "Job[Task]", rerun: bool) -> None:
361+
async def _run_async(self, job: "Job[WorkflowTask]", rerun: bool) -> None:
362362
"""Run the workflow asynchronously."""
363363
await job.submitter.expand_workflow_async(job, rerun)
364364

@@ -369,3 +369,8 @@ def construct(self) -> "Workflow":
369369
return self._constructed
370370
self._constructed = Workflow.construct(self)
371371
return self._constructed
372+
373+
374+
# Alias WorkflowTask to Task so we can refer to it as workflow.Task
375+
Task = WorkflowTask
376+
Outputs = WorkflowOutputs

pydra/engine/job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from pydra.utils.typing import copy_nested_files
3737
from pydra.compose.shell.templating import template_update
3838
from pydra.utils.messenger import AuditFlag
39-
from pydra.environments.base_environment import Environment
39+
from pydra.environments.base import Environment
4040

4141
logger = logging.getLogger("pydra")
4242

pydra/engine/node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
if ty.TYPE_CHECKING:
1111
from pydra.engine.workflow import Workflow
12-
from pydra.environments.base_environment import Environment
12+
from pydra.environments.base import Environment
1313
from pydra.compose import base
1414
from pydra.engine.hooks import TaskHooks
1515

pydra/engine/submitter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from pydra.utils.general import default_run_cache_dir
2626
from pydra.compose import workflow
2727
from pydra.engine.state import State
28-
from pydra.workers.base_worker import Worker
28+
from pydra.workers.base import Worker
2929
from pydra.compose.base import Task, Outputs
3030

3131
logger = logging.getLogger("pydra.submitter")
@@ -35,7 +35,7 @@
3535
from pydra.engine.result import Result
3636
from pydra.engine.hooks import TaskHooks
3737
from pydra.engine.workflow import Workflow
38-
from pydra.environments.base_environment import Environment
38+
from pydra.environments.base import Environment
3939

4040

4141
TaskType = ty.TypeVar("TaskType", bound="Task")
@@ -198,7 +198,7 @@ def __call__(
198198
result : Any
199199
The result of the job
200200
"""
201-
from pydra.environments.base_environment import Environment
201+
from pydra.environments.base import Environment
202202

203203
if raise_errors is None:
204204
raise_errors = self.worker.plugin_name() == "debug"

0 commit comments

Comments
 (0)