Skip to content

Commit 8796170

Browse files
authored
Merge pull request #1129 from writer/chore-expected-type-cast
chore: prevent mypy 1.18.1 fail
2 parents 5733d6b + 7fc64fd commit 8796170

File tree

1 file changed

+86
-47
lines changed

1 file changed

+86
-47
lines changed

src/writer/core.py

Lines changed: 86 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ def _set_state_item(self, key: str, value: Any):
803803
If there is a StateProxy, it is a fault in the code.
804804
"""
805805
annotations = get_annotations(self)
806-
expected_type = annotations.get(key, None)
806+
expected_type = cast(Type, annotations.get(key, None))
807807
expect_dict = _type_match_dict(expected_type)
808808
if isinstance(value, dict) and not expect_dict:
809809
"""
@@ -1224,34 +1224,58 @@ class HandlerEntry(TypedDict):
12241224

12251225
# === BLUEPRINT HANLDERS ===
12261226
@staticmethod
1227-
def stop_blueprint_run(payload: dict, blueprint_runner: 'BlueprintRunner'):
1227+
def stop_blueprint_run(payload: dict, blueprint_runner: "BlueprintRunner"):
12281228
run_id = payload.pop("run_id", None)
12291229
if not run_id:
12301230
raise ValueError("Missing run_id in payload")
12311231
return blueprint_runner.cancel_blueprint_execution(run_id=run_id)
12321232

12331233
@staticmethod
1234-
def run_blueprint_by_id(payload: dict, context: dict, session: dict, blueprint_runner: 'BlueprintRunner', vault: Dict):
1234+
def run_blueprint_by_id(
1235+
payload: dict,
1236+
context: dict,
1237+
session: dict,
1238+
blueprint_runner: "BlueprintRunner",
1239+
vault: Dict,
1240+
):
12351241
blueprint_id = payload.pop("blueprint_id", None)
12361242
if not blueprint_id:
12371243
raise ValueError("Missing blueprint_id in payload")
12381244
execution_environment = EventHandler._get_blueprint_execution_environment(
12391245
payload, context, session, vault
12401246
)
1241-
return blueprint_runner.run_blueprint(component_id=blueprint_id, execution_environment=execution_environment, title="Blueprint execution triggered on demand")
1247+
return blueprint_runner.run_blueprint(
1248+
component_id=blueprint_id,
1249+
execution_environment=execution_environment,
1250+
title="Blueprint execution triggered on demand",
1251+
)
12421252

12431253
@staticmethod
1244-
def run_blueprint_by_key(payload: dict, context: dict, session: dict, blueprint_runner: 'BlueprintRunner', vault: Dict):
1254+
def run_blueprint_by_key(
1255+
payload: dict,
1256+
context: dict,
1257+
session: dict,
1258+
blueprint_runner: "BlueprintRunner",
1259+
vault: Dict,
1260+
):
12451261
blueprint_key = payload.pop("blueprint_key", None)
12461262
if not blueprint_key:
12471263
raise ValueError("Missing blueprint_key in payload")
12481264
execution_environment = EventHandler._get_blueprint_execution_environment(
12491265
payload, context, session, vault
12501266
)
1251-
return blueprint_runner.run_blueprint_by_key(blueprint_key=blueprint_key, execution_environment=execution_environment)
1267+
return blueprint_runner.run_blueprint_by_key(
1268+
blueprint_key=blueprint_key, execution_environment=execution_environment
1269+
)
12521270

12531271
@staticmethod
1254-
def run_blueprint_via_api(payload: dict, context: dict, session: dict, blueprint_runner: 'BlueprintRunner', vault: Dict):
1272+
def run_blueprint_via_api(
1273+
payload: dict,
1274+
context: dict,
1275+
session: dict,
1276+
blueprint_runner: "BlueprintRunner",
1277+
vault: Dict,
1278+
):
12551279
"""
12561280
This handler is used to run a blueprint via the API.
12571281
It is used by the frontend to run a blueprint when the user clicks on a button.
@@ -1262,56 +1286,66 @@ def run_blueprint_via_api(payload: dict, context: dict, session: dict, blueprint
12621286
execution_environment = EventHandler._get_blueprint_execution_environment(
12631287
payload, context, session, vault
12641288
)
1265-
return blueprint_runner.run_blueprint_via_api(blueprint_id=blueprint_id, execution_environment=execution_environment)
1289+
return blueprint_runner.run_blueprint_via_api(
1290+
blueprint_id=blueprint_id, execution_environment=execution_environment
1291+
)
12661292

12671293
@staticmethod
1268-
def run_blueprint_branch(payload: dict, context: dict, session: dict, blueprint_runner: 'BlueprintRunner', vault: Dict):
1294+
def run_blueprint_branch(
1295+
payload: dict,
1296+
context: dict,
1297+
session: dict,
1298+
blueprint_runner: "BlueprintRunner",
1299+
vault: Dict,
1300+
):
12691301
branch_id = payload.pop("branch_id", None)
12701302
if not branch_id:
12711303
raise ValueError("Missing branch_id in payload")
12721304
execution_environment = EventHandler._get_blueprint_execution_environment(
12731305
payload, context, session, vault
12741306
)
1275-
return blueprint_runner.run_branch(start_node_id=branch_id, branch_out_id=None, execution_environment=execution_environment, title="Branch execution triggered by demand")
1307+
return blueprint_runner.run_branch(
1308+
start_node_id=branch_id,
1309+
branch_out_id=None,
1310+
execution_environment=execution_environment,
1311+
title="Branch execution triggered by demand",
1312+
)
12761313

12771314
def __init__(self):
12781315
self.handler_map: Dict[str, "EventHandlerRegistry.HandlerEntry"] = {
1279-
"stop_blueprint_run": {
1280-
"callable": self.stop_blueprint_run,
1281-
"meta": {
1282-
"name": "stop_blueprint_run",
1283-
"args": ["payload", "blueprint_runner"]
1284-
}
1316+
"stop_blueprint_run": {
1317+
"callable": self.stop_blueprint_run,
1318+
"meta": {"name": "stop_blueprint_run", "args": ["payload", "blueprint_runner"]},
1319+
},
1320+
"run_blueprint_by_key": {
1321+
"callable": self.run_blueprint_by_key,
1322+
"meta": {
1323+
"name": "run_blueprint_by_key",
1324+
"args": ["payload", "context", "session", "blueprint_runner", "vault"],
12851325
},
1286-
"run_blueprint_by_key": {
1287-
"callable": self.run_blueprint_by_key,
1288-
"meta": {
1289-
"name": "run_blueprint_by_key",
1290-
"args": ["payload", "context", "session", "blueprint_runner", "vault"]
1291-
}
1326+
},
1327+
"run_blueprint_by_id": {
1328+
"callable": self.run_blueprint_by_id,
1329+
"meta": {
1330+
"name": "run_blueprint_by_id",
1331+
"args": ["payload", "context", "session", "blueprint_runner", "vault"],
12921332
},
1293-
"run_blueprint_by_id": {
1294-
"callable": self.run_blueprint_by_id,
1295-
"meta": {
1296-
"name": "run_blueprint_by_id",
1297-
"args": ["payload", "context", "session", "blueprint_runner", "vault"]
1298-
}
1333+
},
1334+
"run_blueprint_via_api": {
1335+
"callable": self.run_blueprint_via_api,
1336+
"meta": {
1337+
"name": "run_blueprint_via_api",
1338+
"args": ["payload", "context", "session", "blueprint_runner", "vault"],
12991339
},
1300-
"run_blueprint_via_api": {
1301-
"callable": self.run_blueprint_via_api,
1302-
"meta": {
1303-
"name": "run_blueprint_via_api",
1304-
"args": ["payload", "context", "session", "blueprint_runner", "vault"]
1305-
}
1340+
},
1341+
"run_blueprint_branch": {
1342+
"callable": self.run_blueprint_branch,
1343+
"meta": {
1344+
"name": "run_blueprint_branch",
1345+
"args": ["payload", "context", "session", "blueprint_runner", "vault"],
13061346
},
1307-
"run_blueprint_branch": {
1308-
"callable": self.run_blueprint_branch,
1309-
"meta": {
1310-
"name": "run_blueprint_branch",
1311-
"args": ["payload", "context", "session", "blueprint_runner", "vault"]
1312-
}
1313-
}
1314-
}
1347+
},
1348+
}
13151349

13161350
def __iter__(self):
13171351
return iter(self.handler_map.keys())
@@ -1764,7 +1798,9 @@ def _get_blueprint_callable(
17641798
branch_id: Optional[str] = None,
17651799
):
17661800
def fn(payload, context, session, vault):
1767-
execution_environment = self._get_blueprint_execution_environment(payload, context, session, vault)
1801+
execution_environment = self._get_blueprint_execution_environment(
1802+
payload, context, session, vault
1803+
)
17681804
if blueprint_key:
17691805
return self.blueprint_runner.run_blueprint_by_key(
17701806
blueprint_key, execution_environment
@@ -1799,15 +1835,17 @@ def _get_calling_arguments(self, ev: WriterEvent, instance_path: Optional[Instan
17991835
"session": _event_handler_session_info(),
18001836
"ui": _event_handler_ui_manager(),
18011837
"blueprint_runner": self.blueprint_runner,
1802-
"vault": writer_vault.get_secrets()
1838+
"vault": writer_vault.get_secrets(),
18031839
}
18041840

18051841
def _call_handler_callable(self, handler_callable: Callable, calling_arguments: Dict) -> Any:
18061842
current_app_process = get_app_process()
18071843
result = None
18081844
with (
18091845
core_ui.use_component_tree(self.session.session_component_tree),
1810-
use_stdout_redirect(lambda entry: self.session_state.add_log_entry("info", "Stdout message", entry)),
1846+
use_stdout_redirect(
1847+
lambda entry: self.session_state.add_log_entry("info", "Stdout message", entry)
1848+
),
18111849
):
18121850
middlewares_executors = current_app_process.middleware_registry.executors()
18131851
result = EventHandlerExecutor.invoke_with_middlewares(
@@ -1875,7 +1913,8 @@ def _handle_component_event(self, ev: WriterEvent):
18751913
calling_arguments.get("payload"),
18761914
calling_arguments.get("context"),
18771915
calling_arguments.get("session"),
1878-
calling_arguments.get("vault"))
1916+
calling_arguments.get("vault"),
1917+
)
18791918
self.blueprint_runner.execute_ui_trigger(target_id, ev.type, execution_environment)
18801919
if not target_component.handlers:
18811920
return None
@@ -1968,7 +2007,7 @@ def invoke(callable_handler: Callable, writer_args: dict) -> Any:
19682007
def invoke_with_middlewares(
19692008
middlewares_executors: List[MiddlewareExecutor],
19702009
callable_handler: Callable,
1971-
writer_args: dict
2010+
writer_args: dict,
19722011
) -> Any:
19732012
"""
19742013
Runs the middlewares then the handler. This function allows you to manage exceptions that are triggered in middleware

0 commit comments

Comments
 (0)