Skip to content

Commit 705ca0d

Browse files
authored
chore: clean up ruff lint ignores (#310)
1 parent dae9ec1 commit 705ca0d

437 files changed

Lines changed: 5163 additions & 3582 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/__init__.py

Whitespace-only changes.

examples/agent/__init__.py

Whitespace-only changes.

examples/agent/output_parser/__init__.py

Whitespace-only changes.

examples/agent/output_parser/cot_output_parser.py

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
import json
22
import re
33
from collections.abc import Generator
4-
from typing import Union
54

65
from dify_plugin.entities.model.llm import LLMResultChunk
76
from dify_plugin.interfaces.agent import AgentScratchpadUnit
87

8+
CODE_BLOCK_DELIMITER_COUNT = 3
9+
TRIM_BOUNDARY_CHARACTERS = frozenset(("\n", " ", ""))
10+
911

1012
class CotAgentOutputParser:
1113
@classmethod
1214
def handle_react_stream_output(
13-
cls, llm_response: Generator[LLMResultChunk, None, None], usage_dict: dict
14-
) -> Generator[Union[str, AgentScratchpadUnit.Action], None, None]:
15-
def parse_action(json_str):
15+
cls,
16+
llm_response: Generator[LLMResultChunk, None, None],
17+
usage_dict: dict,
18+
) -> Generator[str | AgentScratchpadUnit.Action, None, None]:
19+
def parse_action(json_str: str) -> str | AgentScratchpadUnit.Action:
1620
try:
1721
action = json.loads(json_str, strict=False)
1822
action_name = None
@@ -29,24 +33,31 @@ def parse_action(json_str):
2933
action_name = value
3034

3135
if action_name is not None and action_input is not None:
32-
return AgentScratchpadUnit.Action(
33-
action_name=action_name,
34-
action_input=action_input,
36+
parsed_action: str | AgentScratchpadUnit.Action = (
37+
AgentScratchpadUnit.Action(
38+
action_name=action_name,
39+
action_input=action_input,
40+
)
3541
)
3642
else:
37-
return json_str or ""
43+
parsed_action = json_str or ""
3844
except Exception:
3945
return json_str or ""
46+
else:
47+
return parsed_action
4048

4149
def extra_json_from_code_block(
42-
code_block,
43-
) -> Generator[Union[str, AgentScratchpadUnit.Action], None, None]:
50+
code_block: str,
51+
) -> Generator[str | AgentScratchpadUnit.Action, None, None]:
4452
code_blocks = re.findall(r"```(.*?)```", code_block, re.DOTALL)
4553
if not code_blocks:
4654
return
4755
for block in code_blocks:
4856
json_text = re.sub(
49-
r"^[a-zA-Z]+\n", "", block.strip(), flags=re.MULTILINE
57+
r"^[a-zA-Z]+\n",
58+
"",
59+
block.strip(),
60+
flags=re.MULTILINE,
5061
)
5162
yield parse_action(json_text)
5263

@@ -99,7 +110,7 @@ def extra_json_from_code_block(
99110

100111
if not in_code_block and not in_json:
101112
if delta.lower() == action_str[action_idx] and action_idx == 0:
102-
if last_character not in {"\n", " ", ""}:
113+
if last_character not in TRIM_BOUNDARY_CHARACTERS:
103114
yield_delta = True
104115
else:
105116
last_character = delta
@@ -119,15 +130,14 @@ def extra_json_from_code_block(
119130
action_idx = 0
120131
index += steps
121132
continue
122-
else:
123-
if action_cache:
124-
last_character = delta
125-
yield action_cache
126-
action_cache = ""
127-
action_idx = 0
133+
elif action_cache:
134+
last_character = delta
135+
yield action_cache
136+
action_cache = ""
137+
action_idx = 0
128138

129139
if delta.lower() == thought_str[thought_idx] and thought_idx == 0:
130-
if last_character not in {"\n", " ", ""}:
140+
if last_character not in TRIM_BOUNDARY_CHARACTERS:
131141
yield_delta = True
132142
else:
133143
last_character = delta
@@ -147,20 +157,19 @@ def extra_json_from_code_block(
147157
thought_idx = 0
148158
index += steps
149159
continue
150-
else:
151-
if thought_cache:
152-
last_character = delta
153-
yield thought_cache
154-
thought_cache = ""
155-
thought_idx = 0
160+
elif thought_cache:
161+
last_character = delta
162+
yield thought_cache
163+
thought_cache = ""
164+
thought_idx = 0
156165

157166
if yield_delta:
158167
index += steps
159168
last_character = delta
160169
yield delta
161170
continue
162171

163-
if code_block_delimiter_count == 3:
172+
if code_block_delimiter_count == CODE_BLOCK_DELIMITER_COUNT:
164173
if in_code_block:
165174
last_character = delta
166175
yield from extra_json_from_code_block(code_block_cache)
@@ -186,10 +195,9 @@ def extra_json_from_code_block(
186195
got_json = True
187196
index += steps
188197
continue
189-
else:
190-
if in_json:
191-
last_character = delta
192-
json_cache += delta
198+
elif in_json:
199+
last_character = delta
200+
json_cache += delta
193201

194202
if got_json:
195203
got_json = False

examples/agent/prompt/__init__.py

Whitespace-only changes.

examples/agent/provider/__init__.py

Whitespace-only changes.

examples/agent/strategies/ReAct.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ parameters:
5757
max: 30
5858
extra:
5959
python:
60-
source: strategies/ReAct.py
60+
source: strategies/react.py

examples/agent/strategies/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)