Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/cosy_luigi/utils/traversals.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import inspect
from abc import ABC
from collections.abc import Sequence
from collections.abc import Iterable, Sequence
from typing import TYPE_CHECKING, cast

from cosy_luigi import CoSyLuigiTask

if TYPE_CHECKING:
from collections.abc import Iterable, Sequence
from collections.abc import Sequence


def flatten(
Expand All @@ -32,8 +32,12 @@ def flatten(
for task_or_task_collection in heterogeneous_task_collection
for task in (
flatten(*cast("Iterable[type[CoSyLuigiTask]]", task_or_task_collection))
if isinstance(task_or_task_collection, (tuple, list))
else cast("type[CoSyLuigiTask]", task_or_task_collection).get_all_variants()
if isinstance(task_or_task_collection, Iterable)
else (
variant
for variant in cast("type[CoSyLuigiTask]", task_or_task_collection).get_all_variants()
if not (inspect.isabstract(variant) or ABC in variant.__bases__)
)
if inspect.isabstract(task_or_task_collection)
or ABC in cast("type[CoSyLuigiTask]", task_or_task_collection).__bases__
else (task_or_task_collection,)
Expand Down
15 changes: 14 additions & 1 deletion tests/test_abstract_variant_expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class ABCInheritedTask(CoSyLuigiTask, ABC):
"""An abstract class. This class is abstract because it inherits from ABC."""


class ABCTaskFromABCInherited(ABCInheritedTask, ABC):
"""An abstract class that implements ABCInheritedTask."""


class DeeperConcreteTaskFromABCTaskFromABCInherited(ABCTaskFromABCInherited):
"""A class that indirectly implements ABCInheritedTask by inheriting from ABCTaskFromABCInherited."""


class ConcreteTaskFromABCInherited(ABCInheritedTask):
"""A class that implements ABCInheritedTask."""

Expand Down Expand Up @@ -81,7 +89,11 @@ def test_expansion_from_abc():
"""Tests if adding a class that is abstract because it inherits from ABC expands to all of its subclasses when
added to a CoSyLuigiRepo."""
repo = CoSyLuigiRepo(ABCInheritedTask)
assert repo.luigi_repo == {ConcreteTaskFromABCInherited, DeeperConcreteTaskFromABCInherited}
assert repo.luigi_repo == {
ConcreteTaskFromABCInherited,
DeeperConcreteTaskFromABCInherited,
DeeperConcreteTaskFromABCTaskFromABCInherited,
}


def test_expansion_from_abstract():
Expand All @@ -99,6 +111,7 @@ def test_expansion_from_abc_and_abstract():
repo = CoSyLuigiRepo(ABCInheritedTask, AbstractTask)
assert repo.luigi_repo == {
ConcreteTaskFromABCInherited,
DeeperConcreteTaskFromABCTaskFromABCInherited,
DeeperConcreteTaskFromABCInherited,
ConcreteTaskFromAbstract,
DeeperConcreteTaskFromAbstract,
Expand Down
Loading