|
31 | 31 | "Python tasks in dataclass form are decorated by `pydra.design.python.define`\n", |
32 | 32 | "with inputs listed as type annotations. Outputs are similarly defined in a nested class\n", |
33 | 33 | "called `Outputs`. The function to be executed should be a staticmethod called `function`.\n", |
34 | | - "Default values can also be set directly, as with Attrs classes.\n" |
| 34 | + "Default values can also be set directly, as with Attrs classes.\n", |
| 35 | + "\n", |
| 36 | + "In order to allow static type-checkers to check the type of outputs of tasks added\n", |
| 37 | + "to workflows, it is also necessary to explicitly extend from the `pydra.engine.specs.PythonDef`\n", |
| 38 | + "and `pydra.engine.specs.PythonOutputs` classes (they are otherwise set as bases by the\n", |
| 39 | + "`define` method implicitly). Thus the \"canonical form\" of Python task definition is as\n", |
| 40 | + "follows" |
35 | 41 | ] |
36 | 42 | }, |
37 | 43 | { |
|
47 | 53 | "\n", |
48 | 54 | "\n", |
49 | 55 | "@python.define\n", |
50 | | - "class CanonicalPythonDef:\n", |
| 56 | + "class CanonicalPythonDef(PythonDef[\"CanonicalPythonDef.Outputs\"]):\n", |
51 | 57 | " \"\"\"Canonical Python task definition class for testing\n", |
52 | 58 | "\n", |
53 | 59 | " Args:\n", |
|
59 | 65 | " a: int\n", |
60 | 66 | " b: float = 2.0 # set default value\n", |
61 | 67 | "\n", |
62 | | - " class Outputs:\n", |
| 68 | + " class Outputs(PythonOutputs):\n", |
63 | 69 | " \"\"\"\n", |
64 | 70 | " Args:\n", |
65 | 71 | " c: Sum of a and b\n", |
|
95 | 101 | "\n", |
96 | 102 | "\n", |
97 | 103 | "@python.define\n", |
98 | | - "class CanonicalPythonDef:\n", |
| 104 | + "class CanonicalPythonDef(PythonDef[\"CanonicalPythonDef.Outputs\"]):\n", |
99 | 105 | " \"\"\"Canonical Python task definition class for testing\n", |
100 | 106 | "\n", |
101 | 107 | " Args:\n", |
|
107 | 113 | " a: int = python.arg(allowed_values=[1, 2, 3, 4, 5])\n", |
108 | 114 | " b: float = python.arg(default=2.0, validator=attrs.validators.not_(0))\n", |
109 | 115 | "\n", |
110 | | - " class Outputs:\n", |
| 116 | + " class Outputs(PythonOutputs):\n", |
111 | 117 | " \"\"\"\n", |
112 | 118 | " Args:\n", |
113 | 119 | " c: Sum of a and b\n", |
|
125 | 131 | "pprint(fields_dict(CanonicalPythonDef.Outputs))" |
126 | 132 | ] |
127 | 133 | }, |
128 | | - { |
129 | | - "cell_type": "markdown", |
130 | | - "metadata": {}, |
131 | | - "source": [ |
132 | | - "In order to allow static type-checkers to check the type of outputs of tasks added\n", |
133 | | - "to workflows, it is also necessary to explicitly extend from the `pydra.engine.specs.PythonDef`\n", |
134 | | - "and `pydra.engine.specs.PythonOutputs` classes (they are otherwise set as bases by the\n", |
135 | | - "`define` method implicitly). Thus the \"canonical\" is as follows" |
136 | | - ] |
137 | | - }, |
138 | | - { |
139 | | - "cell_type": "code", |
140 | | - "execution_count": null, |
141 | | - "metadata": {}, |
142 | | - "outputs": [], |
143 | | - "source": [ |
144 | | - "\n", |
145 | | - "@python.define\n", |
146 | | - "class CanonicalPythonDef(PythonDef[\"CanonicalPythonDef.Outputs\"]):\n", |
147 | | - " \"\"\"Canonical Python task definition class for testing\n", |
148 | | - "\n", |
149 | | - " Args:\n", |
150 | | - " a: First input\n", |
151 | | - " to be inputted\n", |
152 | | - " b: Second input\n", |
153 | | - " \"\"\"\n", |
154 | | - "\n", |
155 | | - " a: int\n", |
156 | | - " b: float = 2.0 # set default value\n", |
157 | | - "\n", |
158 | | - " class Outputs(PythonOutputs):\n", |
159 | | - " \"\"\"\n", |
160 | | - " Args:\n", |
161 | | - " c: Sum of a and b\n", |
162 | | - " d: Product of a and b\n", |
163 | | - " \"\"\"\n", |
164 | | - "\n", |
165 | | - " c: float\n", |
166 | | - " d: float\n", |
167 | | - "\n", |
168 | | - " @staticmethod\n", |
169 | | - " def function(a, b):\n", |
170 | | - " return a + b, a / b" |
171 | | - ] |
172 | | - }, |
173 | 134 | { |
174 | 135 | "cell_type": "markdown", |
175 | 136 | "metadata": {}, |
|
0 commit comments