-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix type annotations on template decorators
Use `Concatenate` to add the missing `self` parameter to the class methods annotated with `@_add_type_hints` in `TemplateDecoratorFuncsMixin`. Without this, mypy raises a `misc` error on usage, complaining that the method "does not accept self argument". Signed-off-by: Alice Purcell <alicederyn@gmail.com>
- Loading branch information
1 parent
6dea908
commit 02f7c4e
Showing
2 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from pathlib import Path | ||
from subprocess import run | ||
from tempfile import TemporaryDirectory | ||
from textwrap import dedent | ||
from typing import Iterator, Tuple | ||
|
||
import pytest | ||
|
||
from hera.workflows import Step, Task | ||
|
||
COMMON_SETUP = """ | ||
from hera.shared import global_config | ||
from hera.workflows import Input, Output, Workflow | ||
w = Workflow() | ||
""" | ||
|
||
|
||
def run_mypy(python_code: str): | ||
with TemporaryDirectory() as d: | ||
python_file = Path(d) / "example.py" | ||
python_file.write_text(python_code) | ||
mypy_cmd = ["mypy", "--config-file", "tests/typehints/test-mypy.toml", str(python_file)] | ||
result = run(mypy_cmd, check=False, capture_output=True, encoding="utf-8") | ||
if result.returncode != 0: | ||
msg = f"Error calling {' '.join(mypy_cmd)}:\n{result.stderr}{result.stdout}" | ||
raise AssertionError(msg) | ||
return result.stdout.replace(d, "") | ||
|
||
|
||
def test_script_decoration_no_arguments(): | ||
"""Verify a script can be decorated with no extra arguments.""" | ||
SCRIPT = """ | ||
@w.script() | ||
def simple_script(_: Input) -> Output: | ||
return Output() | ||
reveal_type(simple_script(Input())) | ||
""" | ||
result = run_mypy(COMMON_SETUP + dedent(SCRIPT)) | ||
assert 'Revealed type is "hera.workflows.io.v2.Output"' in result | ||
|
||
|
||
def test_script_decoration_accepts_name_argument(): | ||
"""Verify the script decorator can be passed a name.""" | ||
SCRIPT = """ | ||
@w.script(name = "some_script") | ||
def simple_script(_: Input) -> Output: | ||
return Output() | ||
reveal_type(simple_script(Input())) | ||
""" | ||
result = run_mypy(COMMON_SETUP + dedent(SCRIPT)) | ||
assert 'Revealed type is "hera.workflows.io.v2.Output"' in result |