Skip to content

Commit

Permalink
chore: Ruff and mypy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ramedina86 committed Nov 21, 2024
1 parent 100566d commit 87ac476
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/writer/blocks/httprequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def run(self):
else:
self.outcome = "responseError"
raise RuntimeError("HTTP response with code " + str(req.status_code))
except json.JSONDecodeError:
except json.JSONDecodeError as e:
self.outcome = "responseError"
raise e
except BaseException as e:
Expand Down
14 changes: 9 additions & 5 deletions src/writer/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ def _get_branch_nodes(self, base_component_id: str, base_outcome: Optional[str]=
raise RuntimeError(f'Cannot obtain branch. Could not find component "{base_component_id}".')
if not root_node:
return []
branch_nodes = []
branch_nodes: List[writer.core_ui.Component] = []
if not root_node.outs:
return branch_nodes
for out in root_node.outs:
if base_outcome is not None and base_outcome != out.get("outId"):
continue
branch_root_node = self.session.session_component_tree.get_component(out.get("toNodeId"))
if not branch_root_node:
continue
branch_nodes.append(branch_root_node)
branch_nodes += self._get_branch_nodes(out.get("toNodeId"), base_outcome=None)
return branch_nodes
Expand All @@ -58,7 +60,7 @@ def run_nodes(self, nodes: List[writer.core_ui.Component], execution_environment
for tool in execution.values():
if tool and tool.return_value is not None:
return_value = tool.return_value
except WriterConfigurationError as e:
except WriterConfigurationError:
self._generate_run_log(execution, title, "error")
# No need to re-raise, it's a configuration error under control and shown as message in the relevant tool
except BaseException as e:
Expand Down Expand Up @@ -153,11 +155,13 @@ def run_node(self, target_node: writer.core_ui.Component, nodes: List[writer.cor
tool.run()
tool.execution_time_in_seconds = time.time() - start_time
except BaseException as e:
if tool and not tool.outcome:
if not tool:
raise e
if not tool.outcome:
tool.outcome = "error"
if tool and isinstance(e, WriterConfigurationError):
if isinstance(e, WriterConfigurationError):
tool.message = str(e)
if not self._is_outcome_managed(target_node, tool.outcome):
if not tool.outcome or not self._is_outcome_managed(target_node, tool.outcome):
raise e
finally:
execution[target_node.id] = tool
Expand Down
1 change: 1 addition & 0 deletions tests/backend/blocks/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict

import pytest
from writer.core import WriterSession, WriterState
from writer.core_ui import Branch, Component, ComponentTree, ComponentTreeBranch
Expand Down
4 changes: 2 additions & 2 deletions tests/backend/blocks/test_base_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_get_field(session, runner):
assert {} == block._get_field("ghost_field", as_json=True)
assert "ok" == block._get_field("ghost_field_json", as_json=True, default_field_value='{ "ok": "ok" }').get("ok")
block.run()
assert block.outcome == None
assert block.outcome is None


def test_set_state(session, runner):
Expand All @@ -41,4 +41,4 @@ def test_set_state(session, runner):
assert session.session_state["my_list"] == [1, 2]
assert session.session_state["my_dict"]["animal"] == "cat"
assert session.session_state["unchanged"] == "unchanged"
assert block.outcome == None
assert block.outcome is None
2 changes: 1 addition & 1 deletion tests/backend/blocks/test_calleventhandler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from writer.blocks.calleventhandler import CallEventHandler
import writer.core
from writer.blocks.calleventhandler import CallEventHandler


def valid_handler(state):
Expand Down
1 change: 1 addition & 0 deletions tests/backend/blocks/test_foreach.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from writer.blocks.foreach import ForEach


def test_basic_list(session, runner):
session.add_fake_component({
"workflowKey": "workflow1",
Expand Down
1 change: 1 addition & 0 deletions tests/backend/blocks/test_httprequest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json

import pytest
import requests
from writer.blocks.httprequest import HTTPRequest
Expand Down
4 changes: 2 additions & 2 deletions tests/backend/blocks/test_logmessage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
import writer.core
from writer.blocks.logmessage import LogMessage
from writer.core import WriterState
import writer.core
import pytest


def test_log_message(session, runner):
Expand Down
1 change: 1 addition & 0 deletions tests/backend/blocks/test_parsejson.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json

import pytest
from writer.blocks.parsejson import ParseJSON
from writer.workflows import WorkflowRunner
Expand Down
1 change: 1 addition & 0 deletions tests/backend/blocks/test_returnvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from writer.blocks.returnvalue import ReturnValue
from writer.core import WriterState


def test_basic_return(session, runner):
session.session_state = WriterState({
"animal": "marmot",
Expand Down
1 change: 1 addition & 0 deletions tests/backend/blocks/test_runworkflow.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from writer.blocks.runworkflow import RunWorkflow


def test_workflow_that_does_not_exist(session, runner):
session.add_fake_component({
"workflowKey": "workflowThatDoesNotExist",
Expand Down
2 changes: 1 addition & 1 deletion tests/backend/blocks/test_writeraddchatmessage.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from writer.blocks.writeraddchatmessage import WriterAddChatMessage
import writer.ai
from writer.blocks.writeraddchatmessage import WriterAddChatMessage


def test_add_chat_message(session, runner):
Expand Down
3 changes: 2 additions & 1 deletion tests/backend/blocks/test_writerchat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json

import pytest
from writer.blocks.writerchat import WriterChat
from writer.ai import Conversation
from writer.blocks.writerchat import WriterChat


class MockConversation(Conversation):
Expand Down
4 changes: 3 additions & 1 deletion tests/backend/blocks/test_writerclassification.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json

import pytest
from writer.blocks.writerclassification import WriterClassification
import writer.ai
from writer.blocks.writerclassification import WriterClassification


def fake_complete(prompt, config):
additional_context = "It's about animal classification."
Expand Down
2 changes: 1 addition & 1 deletion tests/backend/blocks/test_writercompletion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from writer.blocks.writercompletion import WriterCompletion
import writer.ai
from writer.blocks.writercompletion import WriterCompletion


def test_complete(session, runner):
Expand Down
2 changes: 1 addition & 1 deletion tests/backend/blocks/test_writerinitchat.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from writer.blocks.writerinitchat import WriterInitChat
from writer.ai import Conversation
from writer.blocks.writerinitchat import WriterInitChat


def test_init_chat_already_initialized(session, runner):
Expand Down
4 changes: 3 additions & 1 deletion tests/backend/blocks/test_writernocodeapp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import json

import pytest
from writer.blocks.writernocodeapp import WriterNoCodeApp
import writer.ai
from writer.blocks.writernocodeapp import WriterNoCodeApp


def fake_generate_content(application_id, app_inputs):
assert application_id == "123"
Expand Down

0 comments on commit 87ac476

Please sign in to comment.