Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add TaskOnKart dump type #368

Merged
merged 1 commit into from
Sep 4, 2024
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
38 changes: 32 additions & 6 deletions gokart/build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from functools import partial
from logging import getLogger
from typing import Any, Optional
from typing import Literal, Optional, TypeVar, cast, overload

import backoff
import luigi
Expand All @@ -11,6 +11,8 @@
from gokart.target import TargetOnKart
from gokart.task import TaskOnKart

T = TypeVar('T')


class LoggerConfig:
def __init__(self, level: int):
Expand Down Expand Up @@ -41,13 +43,13 @@ def __init__(self):
self.flag: bool = False


def _get_output(task: TaskOnKart) -> Any:
def _get_output(task: TaskOnKart[T]) -> T:
output = task.output()
# FIXME: currently, nested output is not supported
if isinstance(output, list) or isinstance(output, tuple):
return [t.load() for t in output if isinstance(t, TargetOnKart)]
return cast(T, [t.load() for t in output if isinstance(t, TargetOnKart)])
if isinstance(output, dict):
return {k: t.load() for k, t in output.items() if isinstance(t, TargetOnKart)}
return cast(T, {k: t.load() for k, t in output.items() if isinstance(t, TargetOnKart)})
if isinstance(output, TargetOnKart):
return output.load()
raise ValueError(f'output type is not supported: {type(output)}')
Expand All @@ -65,15 +67,39 @@ def _reset_register(keep={'gokart', 'luigi'}):
]


@overload
def build(
task: TaskOnKart[T],
return_value: Literal[True] = True,
reset_register: bool = True,
log_level: int = logging.ERROR,
task_lock_exception_max_tries: int = 10,
task_lock_exception_max_wait_seconds: int = 600,
**env_params,
) -> T: ...


@overload
def build(
task: TaskOnKart[T],
return_value: Literal[False],
reset_register: bool = True,
log_level: int = logging.ERROR,
task_lock_exception_max_tries: int = 10,
task_lock_exception_max_wait_seconds: int = 600,
**env_params,
) -> None: ...


def build(
task: TaskOnKart,
task: TaskOnKart[T],
return_value: bool = True,
reset_register: bool = True,
log_level: int = logging.ERROR,
task_lock_exception_max_tries: int = 10,
task_lock_exception_max_wait_seconds: int = 600,
**env_params,
) -> Optional[Any]:
) -> Optional[T]:
"""
Run gokart task for local interpreter.
Sharing the most of its parameters with luigi.build (see https://luigi.readthedocs.io/en/stable/api/luigi.html?highlight=build#luigi.build)
Expand Down
19 changes: 14 additions & 5 deletions gokart/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import types
from importlib import import_module
from logging import getLogger
from typing import Any, Callable, Dict, List, Optional, Set, Union
from typing import Any, Callable, Dict, Generator, Generic, List, Optional, Set, TypeVar, Union, overload

import luigi
import pandas as pd
Expand All @@ -25,7 +25,10 @@
logger = getLogger(__name__)


class TaskOnKart(luigi.Task):
T = TypeVar('T')


class TaskOnKart(luigi.Task, Generic[T]):
"""
This is a wrapper class of luigi.Task.

Expand Down Expand Up @@ -282,7 +285,7 @@ def _load(targets):
return list(data.values())[0]
return data

def load_generator(self, target: Union[None, str, TargetOnKart] = None) -> Any:
def load_generator(self, target: Union[None, str, TargetOnKart] = None) -> Generator[Any, None, None]:
def _load(targets):
if isinstance(targets, list) or isinstance(targets, tuple):
for t in targets:
Expand Down Expand Up @@ -318,6 +321,12 @@ def _flatten_recursively(dfs):
data = data[list(required_columns)]
return data

@overload
def dump(self, obj: T, target: None = None) -> None: ...

@overload
def dump(self, obj: Any, target: Union[str, TargetOnKart]) -> None: ...

def dump(self, obj: Any, target: Union[None, str, TargetOnKart] = None) -> None:
PandasTypeConfigMap().check(obj, task_namespace=self.task_namespace)
if self.fail_on_empty_dump and isinstance(obj, pd.DataFrame):
Expand All @@ -336,13 +345,13 @@ def get_own_code(self):
own_codes = self.get_code(self)
return ''.join(sorted(list(own_codes - gokart_codes)))

def make_unique_id(self):
def make_unique_id(self) -> str:
unique_id = self.task_unique_id or self._make_hash_id()
if self.cache_unique_id:
self.task_unique_id = unique_id
return unique_id

def _make_hash_id(self):
def _make_hash_id(self) -> str:
def _to_str_params(task):
if isinstance(task, TaskOnKart):
return str(task.make_unique_id()) if task.significant else None
Expand Down
4 changes: 3 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ fakeredis = "*"
mypy = "*"
types-redis = "*"
matplotlib = "*"
typing-extensions = "^4.11.0"

[tool.ruff]
line-length = 160
Expand Down
21 changes: 15 additions & 6 deletions test/test_build.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import logging
import os
import sys
import unittest
from copy import copy
from typing import Dict

if sys.version_info >= (3, 11):
from typing import assert_type
else:
from typing_extensions import assert_type

import luigi
import luigi.mock
Expand All @@ -11,9 +18,9 @@
from gokart.conflict_prevention_lock.task_lock import TaskLockException


class _DummyTask(gokart.TaskOnKart):
class _DummyTask(gokart.TaskOnKart[str]):
task_namespace = __name__
param = luigi.Parameter()
param: str = luigi.Parameter()

def output(self):
return self.make_target('./test/dummy.pkl')
Expand All @@ -22,10 +29,10 @@ def run(self):
self.dump(self.param)


class _DummyTaskTwoOutputs(gokart.TaskOnKart):
class _DummyTaskTwoOutputs(gokart.TaskOnKart[Dict[str, str]]):
task_namespace = __name__
param1 = luigi.Parameter()
param2 = luigi.Parameter()
param1: str = luigi.Parameter()
param2: str = luigi.Parameter()

def output(self):
return {'out1': self.make_target('./test/dummy1.pkl'), 'out2': self.make_target('./test/dummy2.pkl')}
Expand All @@ -42,7 +49,7 @@ def run(self):
raise RuntimeError


class _ParallelRunner(gokart.TaskOnKart):
class _ParallelRunner(gokart.TaskOnKart[str]):
def requires(self):
return [_DummyTask(param=str(i)) for i in range(10)]

Expand Down Expand Up @@ -79,6 +86,7 @@ def test_read_config(self):
config_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config', 'test_config.ini')
gokart.utils.add_config(config_file_path)
output = gokart.build(_DummyTask(), reset_register=False)
assert_type(output, str)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍 👍 👍

self.assertEqual(output, 'test')

def test_build_dict_outputs(self):
Expand All @@ -87,6 +95,7 @@ def test_build_dict_outputs(self):
'out2': 'test2',
}
output = gokart.build(_DummyTaskTwoOutputs(param1=param_dict['out1'], param2=param_dict['out2']), reset_register=False)
assert_type(output, Dict[str, str])
self.assertEqual(output, param_dict)

def test_failed_task(self):
Expand Down