Skip to content

Commit

Permalink
Add TypeVar for Job creation (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
michelwi authored Jan 15, 2024
1 parent 5556b5b commit 399b8d9
Showing 1 changed file with 7 additions and 3 deletions.
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

0 comments on commit 399b8d9

Please sign in to comment.