Skip to content

Commit

Permalink
Temp commit trying out fixes to provide autocomplete on the job_func …
Browse files Browse the repository at this point in the history
…callable
  • Loading branch information
sveinugu committed Mar 31, 2023
1 parent 604c03c commit 69f7603
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 86 deletions.
67 changes: 35 additions & 32 deletions src/omnipy/api/protocols/private/compute/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from datetime import datetime
from types import MappingProxyType
from typing import Any, Callable, Dict, Mapping, Optional, Protocol, Tuple, Type
from typing import Any, Callable, Dict, Mapping, Optional, Protocol, Tuple, Type, TypeVar

from omnipy.api.enums import PersistOutputsOptions, RestoreOutputsOptions
from omnipy.api.protocols.private.compute.job_creator import IsJobCreator
Expand All @@ -17,8 +17,10 @@
TaskTemplateCovT,
TaskTemplateT)

C = TypeVar('C', bound=Callable)

class IsJobBase(CanLog, IsUniquelyNamedJob, Protocol):

class IsJobBase(CanLog, IsUniquelyNamedJob, Protocol[C]):
""""""
@property
def _job_creator(self) -> IsJobCreator:
Expand All @@ -40,57 +42,54 @@ def __eq__(self, other: object):
...

@classmethod
def _create_job_template(cls, *args: object, **kwargs: object) -> IsJobTemplate:
def _create_job_template(cls, *args: object, **kwargs: object) -> IsJobTemplate[C]:
...

@classmethod
def _create_job(cls, *args: object, **kwargs: object) -> IsJob:
def _create_job(cls, *args: object, **kwargs: object) -> IsJob[C]:
...

def _apply(self) -> IsJob:
def _apply(self) -> IsJob[C]:
...

def _refine(self, *args: Any, update: bool = True, **kwargs: object) -> IsJobTemplate:
def _refine(self, *args: Any, update: bool = True, **kwargs: object) -> IsJobTemplate[C]:
...

def _revise(self) -> IsJobTemplate:
def _revise(self) -> IsJobTemplate[C]:
...

def _call_job_template(self, *args: object, **kwargs: object) -> object:
...
_call_job_template: C
_call_job: C

def _call_job(self, *args: object, **kwargs: object) -> object:
...

class IsJobBaseCallable(IsJobBase[C], Protocol[C]):
__call__: C

class IsJob(IsJobBase, Protocol):

class IsJob(IsJobBaseCallable[C], Protocol[C]):
""""""
@property
def time_of_cur_toplevel_flow_run(self) -> Optional[datetime]:
...

@classmethod
def create_job(cls, *args: object, **kwargs: object) -> IsJob:
...

def __call__(self, *args: object, **kwargs: object) -> object:
def create_job(cls, *args: object, **kwargs: object) -> IsJob[C]:
...

def _apply_engine_decorator(self, engine: IsEngine) -> None:
...


class IsJobTemplate(IsJobBase, Protocol):
class IsJobTemplate(IsJobBaseCallable[C], Protocol[C]):
""""""
@classmethod
def create_job_template(cls, *args: object, **kwargs: object) -> IsJobTemplate:
def create_job_template(cls, *args: object, **kwargs: object) -> IsJobTemplate[C]:
...

def run(self, *args: object, **kwargs: object) -> object:
...
run: C


class IsFuncArgJobBase(IsJob, Protocol):
class IsFuncArgJobBase(IsJob[C], Protocol[C]):
""""""
@property
def param_signatures(self) -> MappingProxyType:
Expand Down Expand Up @@ -139,6 +138,7 @@ def get_call_args(self, *args: object, **kwargs: object) -> Dict[str, object]:
...


## Change?
class IsPlainFuncArgJobBase(Protocol):
""""""
_job_func: Callable
Expand All @@ -147,13 +147,16 @@ def _accept_call_func_decorator(self, call_func_decorator: GeneralDecorator) ->
...


class IsFuncArgJob(IsFuncArgJobBase, Protocol[JobT]):
class IsFuncArgJob(IsFuncArgJobBase[C], Protocol[JobT, C]):
""""""
def revise(self) -> JobT:
...


class IsFuncArgJobTemplateCallable(Protocol[JobTemplateT]):
CC = TypeVar('CC', bound=Callable, contravariant=True)


class IsFuncArgJobTemplateCallable(Protocol[JobTemplateT, CC]):
""""""
def __call__(
self,
Expand All @@ -165,11 +168,11 @@ def __call__(
fixed_params: Optional[Mapping[str, object]] = None,
param_key_map: Optional[Mapping[str, str]] = None,
**kwargs: object,
) -> Callable[[Callable], JobTemplateT]:
) -> Callable[[CC], JobTemplateT]:
...


class IsFuncArgJobTemplate(IsJobTemplate, IsFuncArgJobBase, Protocol[JobTemplateT, JobT]):
class IsFuncArgJobTemplate(IsJobTemplate[C], IsFuncArgJobBase[C], Protocol[JobTemplateT, JobT, C]):
""""""
def refine(self,
*args: Any,
Expand All @@ -188,16 +191,16 @@ def apply(self) -> JobT:
...


class IsTaskTemplateArgsJobBase(IsFuncArgJobBase, Protocol[TaskTemplateCovT]):
class IsTaskTemplateArgsJobBase(IsFuncArgJobBase[C], Protocol[TaskTemplateCovT, C]):
""""""
@property
def task_templates(self) -> Tuple[TaskTemplateCovT, ...]:
...


class IsTaskTemplateArgsJob(IsTaskTemplateArgsJobBase[TaskTemplateCovT],
IsFuncArgJob[JobT],
Protocol[TaskTemplateCovT, JobT]):
class IsTaskTemplateArgsJob(IsTaskTemplateArgsJobBase[TaskTemplateCovT, C],
IsFuncArgJob[JobT, C],
Protocol[TaskTemplateCovT, JobT, C]):
""""""


Expand All @@ -218,9 +221,9 @@ def __call__(
...


class IsTaskTemplateArgsJobTemplate(IsFuncArgJobTemplate[JobTemplateT, JobT],
IsTaskTemplateArgsJobBase[TaskTemplateT],
Protocol[TaskTemplateT, JobTemplateT, JobT]):
class IsTaskTemplateArgsJobTemplate(IsFuncArgJobTemplate[JobTemplateT, JobT, C],
IsTaskTemplateArgsJobBase[TaskTemplateT, C],
Protocol[TaskTemplateT, JobTemplateT, JobT, C]):
""""""
def refine(self,
*task_templates: TaskTemplateT,
Expand Down
9 changes: 6 additions & 3 deletions src/omnipy/api/protocols/private/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Callable, Protocol, runtime_checkable
from typing import Callable, Protocol, runtime_checkable, TypeVar

from omnipy.api.types import DecoratorClassT

Expand All @@ -12,8 +12,11 @@ def __call__(self, callable_arg: Callable, /, *args: object, **kwargs: object) -
...


CC = TypeVar('CC', bound=Callable, contravariant=True)


@runtime_checkable
class IsCallableClass(Protocol[DecoratorClassT]):
class IsCallableClass(Protocol[DecoratorClassT, CC]):
""""""
def __call__(self, *args: object, **kwargs: object) -> Callable[[Callable], DecoratorClassT]:
def __call__(self, *args: object, **kwargs: object) -> Callable[[CC], DecoratorClassT]:
...
30 changes: 18 additions & 12 deletions src/omnipy/api/protocols/public/compute.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from __future__ import annotations

from datetime import datetime
from typing import Optional, Protocol
from typing import Callable, Optional, Protocol, TypeVar

from omnipy.api.protocols.private.compute.job import (IsFuncArgJob,
IsFuncArgJobTemplate,
IsTaskTemplateArgsJob,
IsTaskTemplateArgsJobTemplate)
from omnipy.api.protocols.private.compute.mixins import IsNestedContext

C = TypeVar('C', bound=Callable)

class IsTaskTemplate(IsFuncArgJobTemplate['IsTaskTemplate', 'IsTask'], Protocol):

class IsTaskTemplate(IsFuncArgJobTemplate['IsTaskTemplate', 'IsTask', C], Protocol[C]):
""""""
...


class IsTask(IsFuncArgJob[IsTaskTemplate], Protocol):
class IsTask(IsFuncArgJob[IsTaskTemplate, C], Protocol[C]):
""""""
...

Expand All @@ -38,39 +40,43 @@ def time_of_last_run(self) -> Optional[datetime]:

class IsLinearFlowTemplate(IsTaskTemplateArgsJobTemplate[IsTaskTemplate,
'IsLinearFlowTemplate',
'IsLinearFlow'],
'IsLinearFlow',
C],
IsFlowTemplate,
Protocol):
Protocol[C]):
""""""
...


class IsLinearFlow(IsTaskTemplateArgsJob[IsTaskTemplate, IsLinearFlowTemplate], IsFlow, Protocol):
class IsLinearFlow(IsTaskTemplateArgsJob[IsTaskTemplate, IsLinearFlowTemplate, C],
IsFlow,
Protocol[C]):
""""""
...


class IsDagFlowTemplate(IsTaskTemplateArgsJobTemplate[IsTaskTemplate,
'IsDagFlowTemplate',
'IsDagFlow'],
'IsDagFlow',
C],
IsFlowTemplate,
Protocol):
Protocol[C]):
""""""
...


class IsDagFlow(IsTaskTemplateArgsJob[IsTaskTemplate, IsDagFlowTemplate], IsFlow, Protocol):
class IsDagFlow(IsTaskTemplateArgsJob[IsTaskTemplate, IsDagFlowTemplate, C], IsFlow, Protocol[C]):
""""""
...


class IsFuncFlowTemplate(IsFuncArgJobTemplate['IsFuncFlowTemplate', 'IsFuncFlow'],
class IsFuncFlowTemplate(IsFuncArgJobTemplate['IsFuncFlowTemplate', 'IsFuncFlow', C],
IsFlowTemplate,
Protocol):
Protocol[C]):
""""""
...


class IsFuncFlow(IsFuncArgJob[IsFuncFlowTemplate], Protocol):
class IsFuncFlow(IsFuncArgJob[IsFuncFlowTemplate, C], Protocol[C]):
""""""
...
12 changes: 7 additions & 5 deletions src/omnipy/compute/func_job.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asyncio
from typing import Callable, Tuple
from typing import Callable, Generic, Tuple, TypeVar

from omnipy.api.types import GeneralDecorator
from omnipy.compute.job import JobBase
Expand All @@ -9,12 +9,14 @@
from omnipy.compute.mixins.result_key import ResultKeyFuncJobBaseMixin
from omnipy.compute.mixins.serialize import SerializerFuncJobBaseMixin

C = TypeVar('C', bound=Callable)

class PlainFuncArgJobBase(JobBase):
def __init__(self, job_func: Callable, *args: object, **kwargs: object) -> None:

class PlainFuncArgJobBase(JobBase, Generic[C]):
def __init__(self, job_func: C, *args: object, **kwargs: object) -> None:
self._job_func = job_func

def _get_init_args(self) -> Tuple[object, ...]:
def _get_init_args(self) -> Tuple[C]:
return self._job_func,

def has_coroutine_func(self) -> bool:
Expand All @@ -37,7 +39,7 @@ def _accept_call_func_decorator(self, call_func_decorator: GeneralDecorator) ->


# Extra level needed for mixins to be able to overload _call_job (and possibly other methods)
class FuncArgJobBase(PlainFuncArgJobBase):
class FuncArgJobBase(PlainFuncArgJobBase[C], Generic[C]):
...


Expand Down
Loading

0 comments on commit 69f7603

Please sign in to comment.