Skip to content

Commit

Permalink
fix: Fix type hints for Python 3.9 as the | operator is not supported
Browse files Browse the repository at this point in the history
  • Loading branch information
fgmacedo committed Dec 5, 2024
1 parent 13ab6e0 commit 9c62c8d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 46 deletions.
25 changes: 10 additions & 15 deletions statemachine/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,37 @@
from ..statemachine import StateMachine
from ..transition_list import TransitionList

CallbacksType = str | Callable | List[str] | List[Callable]


class TransitionDict(TypedDict, total=False):
target: str
event: str | None
event: "str | None"
internal: bool
validators: bool
cond: CallbacksType
unless: CallbacksType
on: CallbacksType
before: CallbacksType
after: CallbacksType
cond: "str | Callable | List[str] | List[Callable]"
unless: "str | Callable | List[str] | List[Callable]"
on: "str | Callable | List[str] | List[Callable]"
before: "str | Callable | List[str] | List[Callable]"
after: "str | Callable | List[str] | List[Callable]"


TransitionsDict = Dict[str | None, List[TransitionDict]]
TransitionsDict = Dict["str | None", List[TransitionDict]]


class StateDict(TypedDict, total=False):
name: str
value: Any
initial: bool
final: bool
enter: CallbacksType
exit: CallbacksType
enter: "str | Callable | List[str] | List[Callable]"
exit: "str | Callable | List[str] | List[Callable]"


class StateWithTransitionsDict(StateDict, total=False):
on: TransitionsDict


StateOptions = StateDict | StateWithTransitionsDict


def create_machine_class_from_definition(
name: str, states: Mapping[str, StateOptions], **definition
name: str, states: Mapping[str, "StateDict | StateWithTransitionsDict"], **definition
) -> StateMachine: # noqa: C901
"""
Creates a StateMachine class from a dictionary definition, using the StateMachineMetaclass.
Expand Down
4 changes: 2 additions & 2 deletions statemachine/io/scxml/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ParseTime:
pattern = re.compile(r"(\d+)?(\.\d+)?(s|ms)")

@classmethod
def parse_delay(cls, delay: str | None, delayexpr: str | None, **kwargs):
def parse_delay(cls, delay: "str | None", delayexpr: "str | None", **kwargs):
if delay:
return cls.time_in_ms(delay)
elif delayexpr:
Expand Down Expand Up @@ -377,7 +377,7 @@ def data_initializer(machine: StateMachine, **kwargs):
return data_initializer


def create_datamodel_action_callable(action: DataModel) -> Callable | None:
def create_datamodel_action_callable(action: DataModel) -> "Callable | None":
data_elements = [_create_dataitem_callable(item) for item in action.data]
data_elements.extend([create_script_action_callable(script) for script in action.scripts])

Expand Down
4 changes: 2 additions & 2 deletions statemachine/io/scxml/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def parse_scxml(scxml_content: str) -> StateMachineDefinition:
return definition


def parse_datamodel(root: ET.Element) -> DataModel | None:
def parse_datamodel(root: ET.Element) -> "DataModel | None":
data_model = DataModel()

for datamodel_elem in root.findall(".//datamodel"):
Expand All @@ -95,7 +95,7 @@ def parse_datamodel(root: ET.Element) -> DataModel | None:

def parse_state(
state_elem: ET.Element,
initial_state: str | None,
initial_state: "str | None",
is_final: bool = False,
is_parallel: bool = False,
) -> State:
Expand Down
3 changes: 1 addition & 2 deletions statemachine/io/scxml/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Dict
from typing import List

from .. import StateOptions
from .. import StateWithTransitionsDict
from .. import TransitionDict
from .. import TransitionsDict
Expand Down Expand Up @@ -33,7 +32,7 @@ def parse_scxml(self, sm_name: str, scxml_content: str):
self.process_definition(definition, location=sm_name)

def process_definition(self, definition, location: str):
states_dict: Dict[str, StateOptions] = {}
states_dict: Dict[str, StateWithTransitionsDict] = {}
for state_id, state in definition.states.items():
state_dict = StateWithTransitionsDict()
if state.initial:
Expand Down
50 changes: 25 additions & 25 deletions statemachine/io/scxml/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class AssignAction(Action):

@dataclass
class LogAction(Action):
label: str | None
label: "str | None"
expr: str


@dataclass
class IfBranch(Action):
cond: str | None
cond: "str | None"
actions: List[Action] = field(default_factory=list)

def append(self, action: Action):
Expand All @@ -49,36 +49,36 @@ class IfAction(Action):
class ForeachAction(Action):
array: str
item: str
index: str | None
index: "str | None"
content: ExecutableContent


@dataclass
class Param:
name: str
expr: str | None
location: str | None = None
expr: "str | None"
location: "str | None" = None


@dataclass
class SendAction(Action):
event: str | None = None
eventexpr: str | None = None
target: str | None = None
type: str | None = None
id: str | None = None
idlocation: str | None = None
delay: str | None = None
delayexpr: str | None = None
namelist: str | None = None
event: "str | None" = None
eventexpr: "str | None" = None
target: "str | None" = None
type: "str | None" = None
id: "str | None" = None
idlocation: "str | None" = None
delay: "str | None" = None
delayexpr: "str | None" = None
namelist: "str | None" = None
params: List[Param] = field(default_factory=list)
content: str | None = None
content: "str | None" = None


@dataclass
class CancelAction(Action):
sendid: str | None = None
sendidexpr: str | None = None
sendid: "str | None" = None
sendidexpr: "str | None" = None


@dataclass
Expand All @@ -89,9 +89,9 @@ class ScriptAction(Action):
@dataclass
class Transition:
target: str
event: str | None = None
cond: str | None = None
on: ExecutableContent | None = None
event: "str | None" = None
cond: "str | None" = None
on: "ExecutableContent | None" = None


@dataclass
Expand All @@ -109,9 +109,9 @@ class State:
@dataclass
class DataItem:
id: str
src: str | None
expr: str | None
content: str | None
src: "str | None"
expr: "str | None"
content: "str | None"


@dataclass
Expand All @@ -123,5 +123,5 @@ class DataModel:
@dataclass
class StateMachineDefinition:
states: Dict[str, State] = field(default_factory=dict)
initial_state: str | None = None
datamodel: DataModel | None = None
initial_state: "str | None" = None
datamodel: "DataModel | None" = None

0 comments on commit 9c62c8d

Please sign in to comment.