Skip to content

Commit 9b7a954

Browse files
committed
Improve typing
1 parent 002f4e5 commit 9b7a954

File tree

4 files changed

+36
-22
lines changed

4 files changed

+36
-22
lines changed

dynamic_stack_decider/dynamic_stack_decider/abstract_action_element.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from abc import ABCMeta
2+
from typing import TYPE_CHECKING
23

34
from dynamic_stack_decider.abstract_stack_element import AbstractStackElement
45

6+
if TYPE_CHECKING:
7+
from dynamic_stack_decider.dsd import DSD
8+
59

610
class AbstractActionElement(AbstractStackElement, metaclass=ABCMeta):
711
"""
@@ -14,7 +18,7 @@ class AbstractActionElement(AbstractStackElement, metaclass=ABCMeta):
1418
If the action is complete, it can remove itself from the stack by performing a pop command.
1519
"""
1620

17-
def __init__(self, blackboard, dsd, parameters=None):
21+
def __init__(self, blackboard, dsd: "DSD", parameters: dict[str, bool | int | float | str]):
1822
"""
1923
Constructor of the action element
2024
:param blackboard: Shared blackboard for data exchange between elements
@@ -23,10 +27,7 @@ def __init__(self, blackboard, dsd, parameters=None):
2327
"""
2428
super().__init__(blackboard, dsd, parameters)
2529
# Reevaluation can be disabled by setting 'r' or 'reevaluate' to False
26-
if parameters is not None:
27-
self.never_reevaluate = not parameters.get("r", True) or not parameters.get("reevaluate", True)
28-
else:
29-
self.never_reevaluate = False
30+
self.never_reevaluate = not parameters.get("r", True) or not parameters.get("reevaluate", True)
3031

3132
def do_not_reevaluate(self):
3233
"""
@@ -41,6 +42,6 @@ def repr_dict(self) -> dict:
4142
"""
4243
return {
4344
"type": "action",
44-
"name": self.__class__.__name__,
45+
"name": self.name,
4546
"debug_data": self._debug_data,
4647
}

dynamic_stack_decider/dynamic_stack_decider/abstract_decision_element.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def repr_dict(self) -> dict:
2626
"""
2727
return {
2828
"type": "decision",
29-
"name": self.__class__.__name__,
29+
"name": self.name,
3030
"debug_data": self._debug_data,
3131
}
3232

dynamic_stack_decider/dynamic_stack_decider/abstract_stack_element.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from abc import ABCMeta, abstractmethod
2-
from typing import Union
2+
from typing import TYPE_CHECKING, Union
33

44
from dynamic_stack_decider.logger import get_logger
55

6+
if TYPE_CHECKING:
7+
from dynamic_stack_decider.dsd import DSD
8+
69

710
class AbstractStackElement(metaclass=ABCMeta):
811
"""
@@ -12,14 +15,14 @@ class AbstractStackElement(metaclass=ABCMeta):
1215
Each element which inherits from the AbstractStackElement can be used as a root element on the stack.
1316
"""
1417

15-
_dsd = None
16-
_init_data = None
18+
_dsd: "DSD"
19+
parameters: dict[str, bool | int | float | str]
1720

18-
def __init__(self, blackboard, dsd, parameters=None):
21+
def __init__(self, blackboard, dsd: "DSD", parameters: dict[str, bool | int | float | str]):
1922
"""
2023
:param blackboard: Shared blackboard for data exchange between elements
2124
:param dsd: The stack decider which has this element on its stack.
22-
:param parameters: Optional parameters which serve as arguments to this element
25+
:param parameters: Parameters which serve as arguments to this element
2326
"""
2427
self._debug_data = {}
2528
"""
@@ -28,8 +31,16 @@ def __init__(self, blackboard, dsd, parameters=None):
2831
"""
2932

3033
self._dsd = dsd
34+
self.parameters = parameters
3135
self.blackboard = blackboard
3236

37+
@property
38+
def name(self) -> str:
39+
"""
40+
Returns the name of the action
41+
"""
42+
return self.__class__.__name__
43+
3344
def pop(self):
3445
"""
3546
Help method which pops the element of the stack.
@@ -90,6 +101,6 @@ def repr_dict(self) -> dict:
90101
"""Represent this stack element as dictionary which is JSON encodable"""
91102
return {
92103
"type": "abstract",
93-
"name": self.__class__.__name__,
104+
"name": self.name,
94105
"debug_data": self._debug_data,
95106
}

dynamic_stack_decider/dynamic_stack_decider/sequence_element.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
from typing import TYPE_CHECKING, Optional
2+
3+
from dynamic_stack_decider.abstract_action_element import AbstractActionElement
14
from dynamic_stack_decider.abstract_stack_element import AbstractStackElement
25

6+
if TYPE_CHECKING:
7+
from dynamic_stack_decider.dsd import DSD
8+
39

410
class SequenceElement(AbstractStackElement):
511
"""
@@ -10,11 +16,11 @@ class SequenceElement(AbstractStackElement):
1016
This is not an abstract class to inherit from.
1117
"""
1218

13-
def __init__(self, blackboard, dsd, actions=()):
19+
def __init__(self, blackboard, dsd: "DSD", actions: list[AbstractActionElement]):
1420
"""
1521
:param actions: list of initialized action elements
1622
"""
17-
super().__init__(blackboard, dsd)
23+
super().__init__(blackboard, dsd, dict())
1824
self.actions = actions
1925
self.current_action_index = 0
2026

@@ -40,21 +46,17 @@ def in_last_element(self):
4046
return self.current_action_index == len(self.actions) - 1
4147

4248
@property
43-
def current_action(self):
49+
def current_action(self) -> AbstractActionElement:
4450
"""
4551
Returns the currently executed action of the sequence element
46-
47-
:rtype: AbstractActionElement
4852
"""
4953
return self.actions[self.current_action_index]
5054

51-
def repr_dict(self):
55+
def repr_dict(self) -> dict:
5256
"""
5357
Represent this stack element as dictionary which is JSON encodable
54-
55-
:rtype: dict
5658
"""
57-
self.publish_debug_data("Active Element", self.current_action.__class__.__name__)
59+
self.publish_debug_data("Active Element", self.current_action.name)
5860
if self.current_action._debug_data:
5961
self.publish_debug_data("Corresponding debug data", self.current_action._debug_data)
6062
data = {

0 commit comments

Comments
 (0)