Skip to content

Commit ee71c7c

Browse files
committed
fix: address SonarCloud code smells
- S1186: add comments to empty method bodies in tests - S125: remove commented-out code in conftest.py and test_microwave.py - S1192: extract _ERROR_EXECUTION constant in engines/base.py - S108: invert condition to eliminate empty pass block in base engine - S5713: remove redundant IndentationError (subclass of SyntaxError) - S1940: use != instead of not == in processor.py - S2772: remove unneeded pass in schema.py Action dataclass - S1854: remove dead assignment to all_initial_states in parser.py - S5806: rename id -> func_id to avoid shadowing builtin in spec_parser.py - S1172: remove unused is_root parameter from diagram._graph_states - S5655: fix Events.match type hint to accept str | None - S5886: fix return type of create_machine_class_from_definition
1 parent 5250989 commit ee71c7c

15 files changed

Lines changed: 31 additions & 62 deletions

conftest.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,3 @@ def has_dot_installed():
4444
def requires_dot_installed(request, has_dot_installed):
4545
if not has_dot_installed:
4646
pytest.skip(f"Test {request.node.nodeid} requires 'dot' that is not installed.")
47-
48-
49-
# @pytest.fixture(autouse=True, scope="module")
50-
# def mock_dot_write(request):
51-
# """
52-
# This fixture avoids updating files while executing tests
53-
# """
54-
55-
# def open_effect(
56-
# filename,
57-
# mode="r",
58-
# *args,
59-
# **kwargs,
60-
# ):
61-
# if mode in ("r", "rt", "rb"):
62-
# return open(filename, mode, *args, **kwargs)
63-
# elif filename.startswith("/tmp/"):
64-
# return open(filename, mode, *args, **kwargs)
65-
# elif "b" in mode:
66-
# return io.BytesIO()
67-
# else:
68-
# return io.StringIO()
69-
70-
# # using global mock instead of the fixture mocker due to the ScopeMismatch
71-
# # this fixture is module scoped and mocker is function scoped
72-
# with mock.patch("pydot.core.io.open", spec=True) as m:
73-
# m.side_effect = open_effect
74-
# yield m

statemachine/contrib/diagram.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def _transition_as_edges(self, transition):
202202

203203
def get_graph(self):
204204
graph = self._get_graph(self.machine)
205-
self._graph_states(self.machine, graph, is_root=True)
205+
self._graph_states(self.machine, graph)
206206
return graph
207207

208208
def _add_transitions(self, graph, state):
@@ -212,7 +212,7 @@ def _add_transitions(self, graph, state):
212212
for edge in self._transition_as_edges(transition):
213213
graph.add_edge(edge)
214214

215-
def _graph_states(self, state, graph, is_root=False):
215+
def _graph_states(self, state, graph):
216216
initial_node = self._initial_node(state)
217217
initial_subgraph = pydot.Subgraph(
218218
graph_name=f"{initial_node.get_name()}_initial",

statemachine/engines/base.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def remove(self, send_id: str):
7070
]
7171

7272

73+
_ERROR_EXECUTION = "error.execution"
74+
75+
7376
class BaseEngine:
7477
def __init__(self, sm: "StateChart"):
7578
self._sm: ReferenceType["StateChart"] = ref(sm)
@@ -138,7 +141,7 @@ def handler(error: Exception) -> None:
138141
# new error.execution is a separate event that may trigger a different
139142
# transition (see W3C test 152). The infinite-loop guard lives at the
140143
# *microstep* level (in ``_send_error_execution``), not here.
141-
self.sm.send("error.execution", error=error, internal=True)
144+
self.sm.send(_ERROR_EXECUTION, error=error, internal=True)
142145

143146
return handler
144147

@@ -159,10 +162,10 @@ def _send_error_execution(self, error: Exception, trigger_data: TriggerData):
159162
If already processing an error.execution event, ignore to avoid infinite loops.
160163
"""
161164
logger.debug("Error %s captured while executing event=%s", error, trigger_data.event)
162-
if trigger_data.event and str(trigger_data.event) == "error.execution":
165+
if trigger_data.event and str(trigger_data.event) == _ERROR_EXECUTION:
163166
logger.warning("Error while processing error.execution, ignoring: %s", error)
164167
return
165-
self.sm.send("error.execution", error=error, internal=True)
168+
self.sm.send(_ERROR_EXECUTION, error=error, internal=True)
166169

167170
def start(self):
168171
if self.sm.current_state_value is not None:
@@ -755,18 +758,16 @@ def add_descendant_states_to_enter( # noqa: C901
755758

756759
# Add the state to the entry set
757760
if (
758-
not self.sm.enable_self_transition_entries
759-
and info.transition.internal
760-
and (
761+
self.sm.enable_self_transition_entries
762+
or not info.transition.internal
763+
or not (
761764
info.transition.is_self
762765
or (
763766
info.transition.target
764767
and info.transition.target.is_descendant(info.transition.source)
765768
)
766769
)
767770
):
768-
pass
769-
else:
770771
states_to_enter.add(info)
771772
state = info.state
772773

statemachine/events.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def add(self, events):
3434

3535
return self
3636

37-
def match(self, event: str):
37+
def match(self, event: "str | None"):
3838
if event is None and self.is_empty:
3939
return True
4040
return any(e.match(event) for e in self)

statemachine/io/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def _parse_states(
145145

146146
def create_machine_class_from_definition(
147147
name: str, states: Mapping[str, "StateKwargs | StateDefinition"], **definition
148-
) -> StateChart: # noqa: C901
148+
) -> "type[StateChart]": # noqa: C901
149149
"""Create a StateChart class dynamically from a dictionary definition.
150150
151151
Args:

statemachine/io/scxml/actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def create_send_action_callable(action: SendAction) -> Callable: # noqa: C901
368368
if action.content:
369369
try:
370370
content = (eval(action.content, {}, {}),)
371-
except (NameError, IndentationError, SyntaxError, TypeError):
371+
except (NameError, SyntaxError, TypeError):
372372
content = (action.content,)
373373

374374
def send_action(*args, **kwargs):

statemachine/io/scxml/parser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def strip_namespaces(tree: ET.Element):
3131
if "}" in el.tag:
3232
el.tag = el.tag.split("}", 1)[1]
3333
attrib = el.attrib
34-
for name in list(attrib.keys()):
34+
for name in list(attrib.keys()): # list() needed: loop mutates attrib
3535
if "}" in name:
3636
new_name = name.split("}", 1)[1]
3737
attrib[new_name] = attrib.pop(name)
@@ -78,7 +78,6 @@ def parse_scxml(scxml_content: str) -> StateMachineDefinition: # noqa: C901
7878
# If no initial state was specified, pick the first state
7979
if not all_initial_states and definition.states:
8080
first_state = next(iter(definition.states.keys()))
81-
all_initial_states = {first_state}
8281
definition.initial_states = [first_state]
8382
definition.states[first_state].initial = True
8483

statemachine/io/scxml/processor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def _prepare_event(self, *args, event: Event, **kwargs):
111111
machine = kwargs["machine"]
112112
session_data = self._get_session(machine)
113113

114-
if not session_data.first_event_raised and event and not event == "__initial__":
114+
if not session_data.first_event_raised and event and event != "__initial__":
115115
session_data.first_event_raised = True
116116

117117
_event: "EventDataWrapper | None" = None

statemachine/io/scxml/schema.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
@dataclass
99
class Action:
10-
pass
11-
1210
def __str__(self):
1311
return f"{self.__class__.__name__}"
1412

statemachine/spec_parser.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ def register(func):
126126
return register
127127

128128
@classmethod
129-
def get(cls, id):
130-
id = id.lower()
131-
if id not in cls.registry:
132-
raise ValueError(f"Unsupported function: {id}")
133-
return cls.registry[id]
129+
def get(cls, func_id):
130+
func_id = func_id.lower()
131+
if func_id not in cls.registry:
132+
raise ValueError(f"Unsupported function: {func_id}")
133+
return cls.registry[func_id]
134134

135135

136136
class InState:

0 commit comments

Comments
 (0)