Add pydocstyle to the ruff rules (#52136)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Signed-off-by: Harry Mellor <hej.mellor@gmail.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
804e5377aa
commit
8c1557a79c
@@ -28,8 +28,7 @@ logger = init_logger(__name__)
|
||||
|
||||
|
||||
class StreamingXMLToolCallParser:
|
||||
"""
|
||||
Simplified streaming XML tool call parser
|
||||
"""Simplified streaming XML tool call parser
|
||||
Supports streaming input, parsing, and output
|
||||
"""
|
||||
|
||||
@@ -46,8 +45,7 @@ class StreamingXMLToolCallParser:
|
||||
self.parameter_end_token: str = "</parameter>"
|
||||
|
||||
def reset_streaming_state(self):
|
||||
"""Reset streaming parsing state"""
|
||||
|
||||
"""Reset streaming parsing state."""
|
||||
self.deltas = []
|
||||
# state for streaming
|
||||
self.tool_call_index = 0
|
||||
@@ -80,8 +78,7 @@ class StreamingXMLToolCallParser:
|
||||
self.setup_parser()
|
||||
|
||||
def parse_single_streaming_chunks(self, xml_chunk: str) -> DeltaMessage:
|
||||
"""
|
||||
Parse single streaming XML chunk and return Delta response
|
||||
"""Parse single streaming XML chunk and return Delta response
|
||||
This is the actual streaming interface that receives chunks
|
||||
one by one and maintains internal state
|
||||
|
||||
@@ -90,6 +87,7 @@ class StreamingXMLToolCallParser:
|
||||
Returns:
|
||||
DeltaMessage: Contains delta information generated by this chunk,
|
||||
returns empty response if no complete elements
|
||||
|
||||
"""
|
||||
# Record delta count before processing
|
||||
initial_delta_count = len(self.deltas)
|
||||
@@ -218,8 +216,7 @@ class StreamingXMLToolCallParser:
|
||||
return DeltaMessage(content=None)
|
||||
|
||||
def _escape_xml_special_chars(self, text: str) -> str:
|
||||
"""
|
||||
Escape XML special characters
|
||||
"""Escape XML special characters
|
||||
Args:
|
||||
text: Original text
|
||||
Returns:
|
||||
@@ -239,11 +236,11 @@ class StreamingXMLToolCallParser:
|
||||
return text
|
||||
|
||||
def _process_complete_xml_elements(self) -> bool:
|
||||
"""
|
||||
Process complete XML elements in buffer
|
||||
"""Process complete XML elements in buffer.
|
||||
|
||||
Returns:
|
||||
bool: Whether complete elements were found and processed
|
||||
|
||||
"""
|
||||
found_any = False
|
||||
|
||||
@@ -320,8 +317,7 @@ class StreamingXMLToolCallParser:
|
||||
return found_any
|
||||
|
||||
def _fix_incomplete_tag_in_chunk(self, chunk: str) -> str:
|
||||
"""
|
||||
Fallback: fix incomplete <parameter=xxx or <function=xxx tags
|
||||
"""Fallback: fix incomplete <parameter=xxx or <function=xxx tags
|
||||
(missing >)
|
||||
Examples: <parameter=-C: -> <parameter=-C>, <parameter=parameter=-n:
|
||||
-> <parameter=-n>
|
||||
@@ -386,8 +382,7 @@ class StreamingXMLToolCallParser:
|
||||
return chunk
|
||||
|
||||
def _fix_missing_equals_in_function_tag(self, chunk: str) -> str:
|
||||
"""
|
||||
Fix missing = in function tags: <function xxx> or <functionxxx>
|
||||
"""Fix missing = in function tags: <function xxx> or <functionxxx>
|
||||
Examples:
|
||||
<function execute_bash> -> <function=execute_bash>
|
||||
<functionexecute_bash> -> <function=execute_bash>
|
||||
@@ -425,7 +420,7 @@ class StreamingXMLToolCallParser:
|
||||
return chunk
|
||||
|
||||
def _validate_function_name(self, func_name: str) -> bool:
|
||||
"""Check if function name exists in tool definitions"""
|
||||
"""Check if function name exists in tool definitions."""
|
||||
if not self.tools:
|
||||
return False
|
||||
|
||||
@@ -442,7 +437,7 @@ class StreamingXMLToolCallParser:
|
||||
return False
|
||||
|
||||
def _validate_parameter_name(self, param_name: str) -> bool:
|
||||
"""Check if parameter exists in current function's tool definition"""
|
||||
"""Check if parameter exists in current function's tool definition."""
|
||||
if not self.tools or not self.current_function_name:
|
||||
return True
|
||||
|
||||
@@ -465,16 +460,15 @@ class StreamingXMLToolCallParser:
|
||||
return True
|
||||
|
||||
def _should_skip_element(self, element: str) -> bool:
|
||||
"""
|
||||
Determine whether an element should be skipped
|
||||
"""Determine whether an element should be skipped.
|
||||
|
||||
Args:
|
||||
element: Element to evaluate
|
||||
|
||||
Returns:
|
||||
bool: True means should skip, False means should process
|
||||
"""
|
||||
|
||||
"""
|
||||
# If it's a tool_call XML tag, don't skip
|
||||
if (
|
||||
element.startswith(self.tool_call_start_token)
|
||||
@@ -501,8 +495,7 @@ class StreamingXMLToolCallParser:
|
||||
return not element
|
||||
|
||||
def _find_next_complete_element(self, start_pos: int) -> tuple[str | None, int]:
|
||||
"""
|
||||
Find next complete XML element from specified position
|
||||
"""Find next complete XML element from specified position.
|
||||
|
||||
Args:
|
||||
start_pos: Position to start searching
|
||||
@@ -510,6 +503,7 @@ class StreamingXMLToolCallParser:
|
||||
Returns:
|
||||
(Complete element string, element end position),
|
||||
returns (None, start_pos) if no complete element found
|
||||
|
||||
"""
|
||||
buffer = self.streaming_buffer[start_pos:]
|
||||
|
||||
@@ -588,8 +582,7 @@ class StreamingXMLToolCallParser:
|
||||
return remaining, start_pos + len(remaining)
|
||||
|
||||
def _merge_new_deltas_to_single_response(self, initial_count: int) -> DeltaMessage:
|
||||
"""
|
||||
Merge newly generated deltas from this processing
|
||||
"""Merge newly generated deltas from this processing
|
||||
into a single DeltaMessage
|
||||
|
||||
Args:
|
||||
@@ -597,6 +590,7 @@ class StreamingXMLToolCallParser:
|
||||
|
||||
Returns:
|
||||
Merged DeltaMessage containing all newly generated delta information
|
||||
|
||||
"""
|
||||
if len(self.deltas) <= initial_count:
|
||||
return DeltaMessage(content=None)
|
||||
@@ -652,8 +646,7 @@ class StreamingXMLToolCallParser:
|
||||
)
|
||||
|
||||
def _preprocess_xml_chunk(self, chunk: str) -> str:
|
||||
"""
|
||||
Preprocess XML chunk, handle non-standard formats,
|
||||
"""Preprocess XML chunk, handle non-standard formats,
|
||||
and escape special characters
|
||||
|
||||
Args:
|
||||
@@ -661,8 +654,8 @@ class StreamingXMLToolCallParser:
|
||||
|
||||
Returns:
|
||||
Processed XML chunk
|
||||
"""
|
||||
|
||||
"""
|
||||
# Check if this is a tool_call related element
|
||||
is_tool_call = False
|
||||
if chunk.startswith(self.tool_call_start_token) or chunk.startswith(
|
||||
@@ -785,7 +778,7 @@ class StreamingXMLToolCallParser:
|
||||
return processed
|
||||
|
||||
def _emit_delta(self, delta: DeltaMessage):
|
||||
"""Emit Delta response (streaming output)"""
|
||||
"""Emit Delta response (streaming output)."""
|
||||
self.deltas.append(delta)
|
||||
|
||||
def _auto_close_open_parameter_if_needed(self, incoming_tag: str | None = None):
|
||||
@@ -814,8 +807,7 @@ class StreamingXMLToolCallParser:
|
||||
self._end_element("tool_call")
|
||||
|
||||
def _start_element(self, name: str, attrs: dict[str, str]):
|
||||
"""Handle XML start element events"""
|
||||
|
||||
"""Handle XML start element events."""
|
||||
if name == "root":
|
||||
return
|
||||
|
||||
@@ -904,7 +896,7 @@ class StreamingXMLToolCallParser:
|
||||
self.current_param_is_first = False
|
||||
|
||||
def _char_data(self, data: str):
|
||||
"""Handle XML character data events"""
|
||||
"""Handle XML character data events."""
|
||||
if data and self.current_param_name:
|
||||
# If preprocessing stage determines deferred parsing is needed,
|
||||
# only cache character data, no streaming output
|
||||
@@ -979,8 +971,7 @@ class StreamingXMLToolCallParser:
|
||||
self._emit_delta(delta)
|
||||
|
||||
def _end_element(self, name: str):
|
||||
"""Handle XML end element events"""
|
||||
|
||||
"""Handle XML end element events."""
|
||||
if name == "root":
|
||||
return
|
||||
|
||||
@@ -1154,18 +1145,18 @@ class StreamingXMLToolCallParser:
|
||||
self._reset_xml_parser_after_tool_call()
|
||||
|
||||
def setup_parser(self):
|
||||
"""Set up XML parser event handlers"""
|
||||
"""Set up XML parser event handlers."""
|
||||
self.parser.buffer_text = True
|
||||
self.parser.StartElementHandler = self._start_element
|
||||
self.parser.EndElementHandler = self._end_element
|
||||
self.parser.CharacterDataHandler = self._char_data
|
||||
|
||||
def set_tools(self, tools: list[Tool] | None):
|
||||
"""Set tool configuration information"""
|
||||
"""Set tool configuration information."""
|
||||
self.tools = tools
|
||||
|
||||
def _extract_function_name(self, name: str, attrs: dict[str, str]) -> str | None:
|
||||
"""Extract function name from various formats"""
|
||||
"""Extract function name from various formats."""
|
||||
if attrs and "name" in attrs:
|
||||
return attrs["name"]
|
||||
|
||||
@@ -1177,7 +1168,7 @@ class StreamingXMLToolCallParser:
|
||||
return None
|
||||
|
||||
def _extract_parameter_name(self, name: str, attrs: dict[str, str]) -> str | None:
|
||||
"""Extract parameter name from various formats"""
|
||||
"""Extract parameter name from various formats."""
|
||||
if attrs and "name" in attrs:
|
||||
return attrs["name"]
|
||||
|
||||
@@ -1195,6 +1186,7 @@ class StreamingXMLToolCallParser:
|
||||
|
||||
Returns:
|
||||
Parameter type
|
||||
|
||||
"""
|
||||
if not self.tools or not self.current_function_name:
|
||||
return "string"
|
||||
@@ -1235,6 +1227,7 @@ class StreamingXMLToolCallParser:
|
||||
|
||||
Returns:
|
||||
Repaired parameter type
|
||||
|
||||
"""
|
||||
if (
|
||||
param_type in ["string", "str", "text", "varchar", "char", "enum"]
|
||||
@@ -1264,6 +1257,7 @@ class StreamingXMLToolCallParser:
|
||||
|
||||
Returns:
|
||||
Converted value
|
||||
|
||||
"""
|
||||
if param_value.lower() == "null":
|
||||
return None
|
||||
@@ -1315,6 +1309,7 @@ class StreamingXMLToolCallParser:
|
||||
|
||||
Returns:
|
||||
Converted string for streaming output
|
||||
|
||||
"""
|
||||
# Check if value is empty, but exclude numeric 0
|
||||
if converted_value is None or converted_value == "":
|
||||
@@ -1331,11 +1326,9 @@ class StreamingXMLToolCallParser:
|
||||
return converted_value
|
||||
|
||||
def _reset_xml_parser_after_tool_call(self):
|
||||
"""
|
||||
Each tool_call is treated as a separate XML document,
|
||||
"""Each tool_call is treated as a separate XML document,
|
||||
so we need to reset the parser after each tool_call.
|
||||
"""
|
||||
|
||||
# recreate XML parser
|
||||
self.parser = ParserCreate()
|
||||
self.setup_parser()
|
||||
@@ -1513,7 +1506,5 @@ class Step3p5ToolParser(ToolParser):
|
||||
return result
|
||||
|
||||
def parser_should_check_for_unstreamed_tool_arg_tokens(self) -> bool:
|
||||
"""
|
||||
Skip the remaining_call calculation in serving_chat
|
||||
"""
|
||||
"""Skip the remaining_call calculation in serving_chat."""
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user