Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
typing
Browse files Browse the repository at this point in the history
RenderingTask is abstract. mypy doesn't allow assigning it to Type
variable.

See also #4645
  • Loading branch information
etam committed Oct 11, 2019
1 parent b73b95f commit ad1bd1e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
7 changes: 3 additions & 4 deletions apps/core/task/coretask.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,8 @@ def get_tasks_left(self):
return (self.get_total_tasks() - self.last_task) \
+ self.num_failed_subtasks

# pylint:disable=unused-argument
@classmethod
def get_subtasks(cls, part):
# pylint:disable=unused-argument,no-self-use
def get_subtasks(self, part) -> Dict[str, dict]:
return dict()

def restart(self):
Expand Down Expand Up @@ -518,7 +517,7 @@ def copy_subtask_results(


class CoreTaskBuilder(TaskBuilder):
TASK_CLASS = CoreTask
TASK_CLASS: Type[CoreTask]
OUTPUT_DIR_TIME_FORMAT = '_%Y-%m-%d_%H-%M-%S'

def __init__(self,
Expand Down
22 changes: 15 additions & 7 deletions apps/rendering/task/framerenderingtask.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import logging
import math
import os
import typing
from typing import Callable, List, TYPE_CHECKING
from typing import (
Callable,
Dict,
List,
TYPE_CHECKING,
Type,
cast,
)
from bisect import insort
from collections import OrderedDict, defaultdict

Expand Down Expand Up @@ -174,7 +180,7 @@ def get_output_states(self):
return result
return []

def get_subtasks(self, part) -> typing.Dict[str, dict]:
def get_subtasks(self, part) -> Dict[str, dict]:
if self.task_definition.options.use_frames:
subtask_ids = self.frames_subtasks.get(to_unicode(part), [])
subtask_ids = filter(None, subtask_ids)
Expand Down Expand Up @@ -481,7 +487,7 @@ def get_frame_name(output_name, ext, frame_num):


class FrameRenderingTaskBuilder(RenderingTaskBuilder):
TASK_CLASS = FrameRenderingTask
TASK_CLASS: Type[FrameRenderingTask]

def __init__(self, owner, task_definition, dir_manager):
frames = task_definition.options.frames
Expand All @@ -504,7 +510,8 @@ def build_dictionary(cls, definition):
@classmethod
def build_minimal_definition(cls, task_type, dictionary) \
-> 'RenderingTaskDefinition':
parent = super(FrameRenderingTaskBuilder, cls)
parent = cast(Type[RenderingTaskBuilder],
super(FrameRenderingTaskBuilder, cls))
options = dictionary.get('options') or dict()

frames_string = to_unicode(options.get('frames', 1))
Expand All @@ -522,15 +529,16 @@ def build_minimal_definition(cls, task_type, dictionary) \
@classmethod
def build_full_definition(cls, task_type, dictionary) \
-> 'RenderingTaskDefinition':
definition = super().build_full_definition(task_type, dictionary)
parent = cast(Type[RenderingTaskBuilder],
super(FrameRenderingTaskBuilder, cls))

definition = parent.build_full_definition(task_type, dictionary)
definition.subtasks_count = _calculate_subtasks_count(
subtasks_count=int(dictionary['subtasks_count']),
use_frames=definition.options.use_frames,
frames=definition.options.frames,
resolution=definition.resolution,
)

return definition

@staticmethod
Expand Down
20 changes: 12 additions & 8 deletions apps/rendering/task/renderingtask.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import math
import os
from typing import Type, TYPE_CHECKING
from typing import cast, Type, TYPE_CHECKING

from pathlib import Path

Expand Down Expand Up @@ -246,7 +246,7 @@ class RenderingTaskBuilderError(Exception):


class RenderingTaskBuilder(CoreTaskBuilder):
TASK_CLASS = RenderingTask
TASK_CLASS: Type[RenderingTask]

@staticmethod
def _scene_file(type, resources):
Expand All @@ -262,7 +262,7 @@ def _scene_file(type, resources):

@classmethod
def build_dictionary(cls, definition):
parent = super(RenderingTaskBuilder, cls)
parent = cast(Type[CoreTaskBuilder], super(RenderingTaskBuilder, cls))

dictionary = parent.build_dictionary(definition)
dictionary['options']['format'] = definition.output_format
Expand All @@ -272,10 +272,12 @@ def build_dictionary(cls, definition):
@classmethod
def build_minimal_definition(cls, task_type, dictionary) \
-> 'RenderingTaskDefinition':
parent = super(RenderingTaskBuilder, cls)
parent = cast(Type[CoreTaskBuilder], super(RenderingTaskBuilder, cls))
resources = dictionary['resources']

definition = parent.build_minimal_definition(task_type, dictionary)
definition = cast(
'RenderingTaskDefinition',
parent.build_minimal_definition(task_type, dictionary))

if 'main_scene_file' in dictionary:
main_scene_file = dictionary['main_scene_file']
Expand All @@ -288,10 +290,12 @@ def build_minimal_definition(cls, task_type, dictionary) \
@classmethod
def build_full_definition(cls, task_type, dictionary) \
-> 'RenderingTaskDefinition':
parent = super(RenderingTaskBuilder, cls)
parent = cast(Type[CoreTaskBuilder], super(RenderingTaskBuilder, cls))
options = dictionary['options']

definition = parent.build_full_definition(task_type, dictionary)
definition = cast(
'RenderingTaskDefinition',
parent.build_full_definition(task_type, dictionary))
definition.output_format = options['format'].upper()

definition.resolution = \
Expand Down Expand Up @@ -319,7 +323,7 @@ def build_full_definition(cls, task_type, dictionary) \

@classmethod
def get_output_path(cls, dictionary, definition):
parent = super(RenderingTaskBuilder, cls)
parent = cast(Type[CoreTaskBuilder], super(RenderingTaskBuilder, cls))
path = parent.get_output_path(dictionary, definition)

return '{}.{}'.format(path, dictionary['options']['format'])
8 changes: 0 additions & 8 deletions tests/apps/core/task/test_coretask.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,6 @@ def _get_task_def_dict(

def test_init(self):
builder = self._get_core_task_builder()
assert builder.TASK_CLASS == CoreTaskBuilder.TASK_CLASS
assert builder.TASK_CLASS == CoreTask
assert builder.task_definition is not None
assert builder.owner is not None
assert isinstance(builder.dir_manager, MagicMock)
Expand All @@ -544,12 +542,6 @@ class C(object):
assert kwargs["owner"] is not None
assert isinstance(kwargs["task_definition"], MagicMock)

def test_build(self):
builder = self._get_core_task_builder()
# CoreTask is now abstract
with self.assertRaises(TypeError):
builder.build()

@freeze_time('2019-01-01 00:00:00')
def test_get_output_path_returns_correct_path(self):
builder = self._get_core_task_builder()
Expand Down

0 comments on commit ad1bd1e

Please # to comment.