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 TypeVar for Job creation #173

Merged
merged 1 commit into from
Jan 15, 2024
Merged
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
10 changes: 7 additions & 3 deletions sisyphus/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import sys
import time
import traceback
from typing import List, Iterator
from typing import List, Iterator, Type, TypeVar

from sisyphus import block, tools
from sisyphus.task import Task
Expand Down Expand Up @@ -61,12 +61,15 @@ def job_finished(path):
)


T = TypeVar("T", bound="Job")


class JobSingleton(type):

"""Meta class to ensure that every Job with the same hash value is
only created once"""

def __call__(cls, *args, **kwargs):
def __call__(cls: Type[T], *args, **kwargs) -> T:
"""Implemented to ensure that each job is created only once"""
try:
if "sis_tags" in kwargs:
Expand Down Expand Up @@ -109,6 +112,7 @@ def __call__(cls, *args, **kwargs):
else:
# create new object
job = super(Job, cls).__new__(cls)
assert isinstance(job, Job)
job._sis_tags = tags

# store _sis_id
Expand Down Expand Up @@ -175,7 +179,7 @@ def get_lock(cls):
Job._lock_storage.append(multiprocessing.Lock())
return Job._lock_storage[Job._lock_index]

def __new__(cls, *args, **kwargs):
def __new__(cls: Type[T], *args, **kwargs) -> T:
# Make sure unpickled jobs stay singletons
assert len(args) == 1 and len(kwargs) == 0 and isinstance(args[0], str)
sis_cache_key = args[0]
Expand Down
Loading