Skip to content

Commit

Permalink
chore: fix test linting - TODO: add docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
philtweir committed Aug 25, 2024
1 parent 3274ea9 commit 1dc5692
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 42 deletions.
3 changes: 0 additions & 3 deletions tests/_lib/extra.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
from dewret.tasks import task, workflow
from dewret.annotations import AtRender

from .other import nothing

JUMP: float = 1.0
test: float = nothing

from inspect import get_annotations

@workflow()
def try_nothing() -> int:
"""Check that we can see AtRender in another module."""

if nothing:
return increment(num=1)
return increment(num=0)
Expand Down
17 changes: 12 additions & 5 deletions tests/test_annotations.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Verify we can interrogate annotations."""

import pytest
import yaml
from typing import Literal

from dewret.tasks import task, construct, workflow, TaskException
from dewret.tasks import construct, workflow, TaskException
from dewret.renderers.cwl import render
from dewret.annotations import AtRender, FunctionAnalyser, Fixed
from dewret.core import set_configuration
Expand All @@ -13,12 +14,15 @@
ARG2: bool = False

class MyClass:
"""TODO: Docstring."""
def method(self, arg1: bool, arg2: AtRender[int]) -> float:
"""TODO: Docstring."""
arg3: float = 7.0
arg4: AtRender[float] = 8.0
return arg1 + arg2 + arg3 + arg4 + int(ARG1) + int(ARG2)

def fn(arg5: int, arg6: AtRender[int]) -> float:
"""TODO: Docstring."""
arg7: float = 7.0
arg8: AtRender[float] = 8.0
return arg5 + arg6 + arg7 + arg8 + int(ARG1) + int(ARG2)
Expand All @@ -35,6 +39,7 @@ def to_int(num: int, should_double: AtRender[bool]) -> int | float:
return increment(num=num) if should_double else sum(left=num, right=num)

def test_can_analyze_annotations():
"""TODO: Docstring."""
my_obj = MyClass()

analyser = FunctionAnalyser(my_obj.method)
Expand All @@ -56,6 +61,7 @@ def test_can_analyze_annotations():
assert analyser.argument_has("ARG1", AtRender) is False

def test_at_render() -> None:
"""TODO: Docstring."""
with pytest.raises(TaskException) as _:
result = to_int_bad(num=increment(num=3), should_double=True)
wkflw = construct(result, simplify_ids=True)
Expand Down Expand Up @@ -138,19 +144,20 @@ def test_at_render() -> None:


def test_at_render_between_modules() -> None:
nothing = False
"""TODO: Docstring."""
result = try_nothing()
wkflw = construct(result, simplify_ids=True)
subworkflows = render(wkflw, allow_complex_types=True)
rendered = subworkflows["__root__"]
subworkflows["__root__"]

list_2: Fixed[list[int]] = [0, 1, 2, 3]

def test_can_loop_over_fixed_length() -> None:
"""TODO: Docstring."""
@workflow()
def loop_over_lists(list_1: list[int]) -> list[int]:
result = []
for a, b in zip(list_1, list_2):
for a, b in zip(list_1, list_2, strict=False):
result.append(a + b + len(list_2))
return result

Expand Down
12 changes: 8 additions & 4 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
"""Check configuration is consistent and usable."""

import yaml
import pytest
from dewret.tasks import construct, task, factory, workflow, TaskException
from dewret.tasks import construct, workflow, TaskException
from dewret.renderers.cwl import render
from dewret.utils import hasher
from dewret.tasks import set_configuration
from dewret.annotations import AtRender
from ._lib.extra import increment, double, mod10, sum, triple_and_one
from ._lib.extra import increment

@pytest.fixture
def configuration():
"""TODO: Docstring."""
with set_configuration() as configuration:
yield configuration.get()

Expand All @@ -21,6 +23,7 @@ def floor(num: int, expected: AtRender[bool]) -> int:
return increment(num=num)

def test_cwl_with_parameter(configuration) -> None:
"""TODO: Docstring."""
with set_configuration(flatten_all_nested=True):
result = increment(num=floor(num=3, expected=True))
workflow = construct(result, simplify_ids=True)
Expand All @@ -35,8 +38,9 @@ def test_cwl_with_parameter(configuration) -> None:
workflow = construct(result, simplify_ids=True)
rendered = render(workflow)["__root__"]
num_param = list(workflow.find_parameters())[0]
assert num_param

assert rendered == yaml.safe_load(f"""
assert rendered == yaml.safe_load("""
cwlVersion: 1.2
class: Workflow
inputs:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cwl.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def test_cwl_with_positional_parameter() -> None:
Produces CWL for a call with a changeable raw value, that is converted
to a parameter, if and only if we are calling from outside a subworkflow.
"""
with pytest.raises(TaskException) as exc:
with pytest.raises(TaskException) as _:
result = increment(3)
with set_configuration(allow_positional_args=True):
result = increment(3)
Expand Down
16 changes: 4 additions & 12 deletions tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from dewret.workflow import Task, Lazy
from dewret.tasks import construct, task, workflow, TaskException
from dewret.annotations import AtRender
from dewret.renderers.cwl import render
from ._lib.extra import increment, pi, reverse_list # noqa: F401


Expand All @@ -14,7 +13,7 @@ def add_task(left: int, right: int) -> int:
return left + right


ADD_TASK_LINE_NO: int = 11
ADD_TASK_LINE_NO: int = 10


@workflow()
Expand All @@ -37,14 +36,6 @@ def __init__(self, task: Task):
...


@task()
def pi() -> float:
"""Get pi from math package."""
import math

return math.pi


@task()
def pi_exported_from_math() -> float:
"""Get pi from math package by name."""
Expand Down Expand Up @@ -242,14 +233,15 @@ def test_subworkflows_must_return_a_task() -> None:
good_num: int = 4

def test_must_annotate_global() -> None:
"""TODO: Docstrings."""
worse_num = 3

@workflow()
def check_annotation() -> int | float:
return increment(num=bad_num)

with pytest.raises(TaskException) as exc:
result = check_annotation()
check_annotation()

assert (
str(exc.value)
Expand All @@ -261,7 +253,7 @@ def check_annotation_2() -> int | float:
return increment(num=worse_num)

with pytest.raises(TaskException) as exc:
result = check_annotation_2()
check_annotation_2()

assert (
str(exc.value)
Expand Down
43 changes: 29 additions & 14 deletions tests/test_fieldable.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
"""Check field management works."""

from __future__ import annotations
import yaml
from dataclasses import dataclass

import pytest
from typing import Unpack, TypedDict

from dewret.tasks import task, construct, workflow, set_configuration
from dewret.workflow import param
from dewret.renderers.cwl import render
from dewret.annotations import Fixed

from ._lib.extra import double, mod10, sum, pi
from ._lib.extra import mod10, sum, pi

@dataclass
class Sides:
"""TODO: Docstring."""
left: int
right: int

SIDES: Sides = Sides(3, 6)

@workflow()
def sum_sides() -> float:
"""TODO: Docstring."""
return sum(left=SIDES.left, right=SIDES.right)

def test_fields_of_parameters_usable() -> None:
"""TODO: Docstring."""
result = sum_sides()
wkflw = construct(result, simplify_ids=True)
rendered = render(wkflw, allow_complex_types=True)["sum_sides-1"]
Expand Down Expand Up @@ -69,10 +73,12 @@ def test_fields_of_parameters_usable() -> None:

@dataclass
class MyDataclass:
"""TODO: Docstring."""
left: int
right: "MyDataclass"

def test_can_get_field_reference_from_parameter():
"""TODO: Docstring."""
my_param = param("my_param", typ=MyDataclass)
result = sum(left=my_param.left, right=sum(left=my_param.right.left, right=my_param))
wkflw = construct(result, simplify_ids=True)
Expand Down Expand Up @@ -116,6 +122,7 @@ def test_can_get_field_reference_from_parameter():
""")

def test_can_get_field_reference_iff_parent_type_has_field():
"""TODO: Docstring."""
@dataclass
class MyDataclass:
left: int
Expand All @@ -128,6 +135,7 @@ class MyDataclass:
assert param_reference.left.__type__ == int

def test_can_get_field_references_from_dataclass():
"""TODO: Docstring."""
@dataclass
class MyDataclass:
left: int
Expand All @@ -149,10 +157,12 @@ def get_left(my_dataclass: MyDataclass) -> int:
assert wkflw.result.__type__ == int

class MyDict(TypedDict):
"""TODO: Docstring."""
left: int
right: float

def test_can_get_field_references_from_typed_dict():
"""TODO: Docstring."""
@workflow()
def test_dict(**my_dict: Unpack[MyDict]) -> MyDict:
result: MyDict = {"left": mod10(num=my_dict["left"]), "right": pi()}
Expand All @@ -166,9 +176,11 @@ def test_dict(**my_dict: Unpack[MyDict]) -> MyDict:

@dataclass
class MyListWrapper:
"""TODO: Docstring."""
my_list: list[int]

def test_can_iterate():
"""TODO: Docstring."""
@task()
def test_task(alpha: int, beta: float, charlie: bool) -> int:
return int(alpha + beta)
Expand Down Expand Up @@ -289,6 +301,7 @@ def test_iterated_3(param: Fixed[list[tuple[int, int]]]) -> int:
""")

def test_can_use_plain_dict_fields():
"""TODO: Docstring."""
@workflow()
def test_dict(left: int, right: float) -> dict[str, float | int]:
result: dict[str, float | int] = {"left": mod10(num=left), "right": pi()}
Expand All @@ -302,27 +315,29 @@ def test_dict(left: int, right: float) -> dict[str, float | int]:

@dataclass
class IndexTest:
"""TODO: Docstring."""
left: Fixed[list[int]]

def test_can_configure_field_separator():
"""TODO: Docstring."""
@task()
def test_sep() -> IndexTest:
return IndexTest(left=[3])

with set_configuration(field_index_types="int"):
result = test_sep().left[0]
wkflw = construct(result, simplify_ids=True)
rendered = render(wkflw, allow_complex_types=True)["__root__"]
render(wkflw, allow_complex_types=True)["__root__"]
assert str(wkflw.result) == "test_sep-1/left[0]"

#with set_configuration(field_index_types="int,str"):
# result = test_sep().left[0]
# wkflw = construct(result, simplify_ids=True)
# rendered = render(wkflw, allow_complex_types=True)["__root__"]
# assert str(wkflw.result) == "test_sep-1[left][0]"

#with set_configuration(field_index_types=""):
# result = test_sep().left[0]
# wkflw = construct(result, simplify_ids=True)
# rendered = render(wkflw, allow_complex_types=True)["__root__"]
# assert str(wkflw.result) == "test_sep-1/left/0"
with set_configuration(field_index_types="int,str"):
result = test_sep().left[0]
wkflw = construct(result, simplify_ids=True)
render(wkflw, allow_complex_types=True)["__root__"]
assert str(wkflw.result) == "test_sep-1[left][0]"

with set_configuration(field_index_types=""):
result = test_sep().left[0]
wkflw = construct(result, simplify_ids=True)
render(wkflw, allow_complex_types=True)["__root__"]
assert str(wkflw.result) == "test_sep-1/left/0"
4 changes: 3 additions & 1 deletion tests/test_nested.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Check complex nested structures and expressions can mix."""

import yaml
import pytest
import math
from dewret.workflow import param
from dewret.tasks import construct
Expand All @@ -8,6 +9,7 @@
from ._lib.extra import reverse_list, max_list

def test_can_supply_nested_raw():
"""TODO: Docstrings."""
pi = param("pi", math.pi)
result = reverse_list(to_sort=[1., 3., pi])
workflow = construct(max_list(lst=result + result), simplify_ids=True)
Expand Down
7 changes: 5 additions & 2 deletions tests/test_render_module.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Check renderers can be imported live."""

from pathlib import Path
from dewret.tasks import construct, task, factory
from dewret.tasks import construct
from dewret.render import get_render_method

from ._lib.extra import increment, double, mod10, sum, triple_and_one
from ._lib.extra import increment, triple_and_one

def test_can_load_render_module():
"""TODO: Docstrings."""
result = triple_and_one(num=increment(num=3))
workflow = construct(result, simplify_ids=True)
workflow._name = "Fred"
Expand Down
2 changes: 2 additions & 0 deletions tests/test_subworkflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,12 +546,14 @@ def test_subworkflows_can_use_globals_in_right_scope() -> None:

@define
class PackResult:
"""TODO: Docstrings."""
hearts: int
clubs: int
spades: int
diamonds: int

def test_combining_attrs_and_factories():
"""TODO: Docstrings."""
Pack = factory(PackResult)

@task()
Expand Down

0 comments on commit 1dc5692

Please sign in to comment.