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
@@ -84,8 +84,7 @@ class StreamState:
|
||||
|
||||
|
||||
class Parser:
|
||||
"""
|
||||
Abstract Parser class that unifies ReasoningParser and ToolParser into
|
||||
"""Abstract Parser class that unifies ReasoningParser and ToolParser into
|
||||
a single interface for parsing model output.
|
||||
|
||||
This class provides a unified way to handle both reasoning extraction
|
||||
@@ -191,8 +190,7 @@ class Parser:
|
||||
|
||||
@abstractmethod
|
||||
def is_reasoning_end(self, input_ids: list[int]) -> bool:
|
||||
"""
|
||||
Check if the reasoning content ends in the input_ids.
|
||||
"""Check if the reasoning content ends in the input_ids.
|
||||
|
||||
Used by structured engines like `xgrammar` to check if the
|
||||
reasoning content ends in the model output.
|
||||
@@ -204,13 +202,13 @@ class Parser:
|
||||
|
||||
Returns:
|
||||
True if the reasoning content ends in the input_ids.
|
||||
|
||||
"""
|
||||
|
||||
def is_reasoning_end_streaming(
|
||||
self, input_ids: list[int], delta_ids: list[int]
|
||||
) -> bool:
|
||||
"""
|
||||
Check if the reasoning content ends during a decode step.
|
||||
"""Check if the reasoning content ends during a decode step.
|
||||
|
||||
Must be a pure function of the input_ids and delta_ids.
|
||||
|
||||
@@ -220,12 +218,12 @@ class Parser:
|
||||
|
||||
Returns:
|
||||
True if the reasoning content ends in the delta_ids.
|
||||
|
||||
"""
|
||||
return self.is_reasoning_end(input_ids)
|
||||
|
||||
def find_reasoning_end_offset(self, token_ids: Sequence[int]) -> int | None:
|
||||
"""
|
||||
Locate the token that ends reasoning within one window of tokens.
|
||||
"""Locate the token that ends reasoning within one window of tokens.
|
||||
|
||||
Args:
|
||||
token_ids: The tokens to examine.
|
||||
@@ -235,13 +233,13 @@ class Parser:
|
||||
``len(token_ids)`` when reasoning does not end in this window. Parsers
|
||||
that cannot answer from a window alone always return ``None``; callers
|
||||
fall back to :meth:`is_reasoning_end_streaming`.
|
||||
|
||||
"""
|
||||
return None
|
||||
|
||||
@abstractmethod
|
||||
def extract_content_ids(self, input_ids: list[int]) -> list[int]:
|
||||
"""
|
||||
Extract content token IDs from the input_ids.
|
||||
"""Extract content token IDs from the input_ids.
|
||||
|
||||
This extracts the non-reasoning content (e.g., everything after
|
||||
the </think> tag).
|
||||
@@ -251,6 +249,7 @@ class Parser:
|
||||
|
||||
Returns:
|
||||
The extracted content token IDs.
|
||||
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
@@ -259,8 +258,7 @@ class Parser:
|
||||
model_output: str,
|
||||
request: ChatCompletionRequest | ResponsesRequest,
|
||||
) -> tuple[str | None, str | None]:
|
||||
"""
|
||||
Extract reasoning content from a complete model-generated string.
|
||||
"""Extract reasoning content from a complete model-generated string.
|
||||
|
||||
Used for non-streaming responses where we have the entire model
|
||||
response available before sending to the client.
|
||||
@@ -271,6 +269,7 @@ class Parser:
|
||||
|
||||
Returns:
|
||||
A tuple of (reasoning, response_content).
|
||||
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
@@ -283,8 +282,7 @@ class Parser:
|
||||
current_token_ids: Sequence[int],
|
||||
delta_token_ids: Sequence[int],
|
||||
) -> DeltaMessage | None:
|
||||
"""
|
||||
Extract reasoning content from a streaming delta message.
|
||||
"""Extract reasoning content from a streaming delta message.
|
||||
|
||||
Args:
|
||||
previous_text: Text from all previous tokens.
|
||||
@@ -296,6 +294,7 @@ class Parser:
|
||||
|
||||
Returns:
|
||||
A DeltaMessage with reasoning and/or content fields, or None.
|
||||
|
||||
"""
|
||||
|
||||
# ========== Tool Parser Methods ==========
|
||||
@@ -303,8 +302,7 @@ class Parser:
|
||||
def adjust_request(
|
||||
self, request: ChatCompletionRequest | ResponsesRequest
|
||||
) -> ChatCompletionRequest | ResponsesRequest:
|
||||
"""
|
||||
Adjust the request parameters for tool calling.
|
||||
"""Adjust the request parameters for tool calling.
|
||||
|
||||
Can be overridden by subclasses to modify request parameters
|
||||
(e.g., setting structured output schemas for tool calling).
|
||||
@@ -314,6 +312,7 @@ class Parser:
|
||||
|
||||
Returns:
|
||||
The adjusted request.
|
||||
|
||||
"""
|
||||
return request
|
||||
|
||||
@@ -323,8 +322,7 @@ class Parser:
|
||||
model_output: str,
|
||||
request: ChatCompletionRequest | ResponsesRequest,
|
||||
) -> ExtractedToolCallInformation:
|
||||
"""
|
||||
Extract tool calls from a complete model-generated string.
|
||||
"""Extract tool calls from a complete model-generated string.
|
||||
|
||||
Used for non-streaming responses.
|
||||
|
||||
@@ -334,6 +332,7 @@ class Parser:
|
||||
|
||||
Returns:
|
||||
ExtractedToolCallInformation containing the tool calls.
|
||||
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
@@ -347,8 +346,7 @@ class Parser:
|
||||
delta_token_ids: Sequence[int],
|
||||
request: ChatCompletionRequest | ResponsesRequest,
|
||||
) -> DeltaMessage | None:
|
||||
"""
|
||||
Extract tool calls from a streaming delta message.
|
||||
"""Extract tool calls from a streaming delta message.
|
||||
|
||||
Args:
|
||||
previous_text: Text from all previous tokens.
|
||||
@@ -361,6 +359,7 @@ class Parser:
|
||||
|
||||
Returns:
|
||||
A DeltaMessage with tool_calls field, or None.
|
||||
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
@@ -381,6 +380,7 @@ class Parser:
|
||||
|
||||
Returns:
|
||||
A tuple of (reasoning, content, tool_calls).
|
||||
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
@@ -403,8 +403,7 @@ class Parser:
|
||||
|
||||
|
||||
class DelegatingParser(Parser):
|
||||
"""
|
||||
A Parser implementation that delegates to separate ReasoningParser and
|
||||
"""A Parser implementation that delegates to separate ReasoningParser and
|
||||
ToolParser instances.
|
||||
|
||||
This is the recommended base class for creating model-specific parsers
|
||||
|
||||
@@ -169,8 +169,7 @@ def collect_tool_schema(tool_schema: list[CohereNormalizedTool]) -> str:
|
||||
def _tool_definitions_to_schema_list(
|
||||
tools: str | list[Any],
|
||||
) -> list[CohereNormalizedTool]:
|
||||
"""
|
||||
Build the list of ``CohereNormalizedTool`` dicts expected by
|
||||
"""Build the list of ``CohereNormalizedTool`` dicts expected by
|
||||
``collect_tool_schema``.
|
||||
|
||||
Accepts:
|
||||
@@ -214,8 +213,7 @@ def _tool_definitions_to_schema_list(
|
||||
def _has_effective_tools(
|
||||
tools: str | list[Any] | None,
|
||||
) -> TypeGuard[str | list[Any]]:
|
||||
"""
|
||||
True when ``tools`` contains at least one tool definition to convert.
|
||||
"""True when ``tools`` contains at least one tool definition to convert.
|
||||
|
||||
``ResponsesRequest`` defaults ``tools`` to ``[]``; ``ChatCompletionRequest``
|
||||
uses ``None``. Both mean "no tools" here. Strings (e.g. a JSON blob) are
|
||||
@@ -236,8 +234,7 @@ def convert_schema_to_structural_tags(
|
||||
tools: str | list[Any] | None = None,
|
||||
model_architecture: str | None = None,
|
||||
) -> str | None:
|
||||
"""
|
||||
Returns a response_format string accepted by xgrammar's structural tag format.
|
||||
"""Returns a response_format string accepted by xgrammar's structural tag format.
|
||||
Uses the canonical shape: {"type": "structural_tag", "format": {...}} with
|
||||
format.type "triggered_tags" and tag content type "json_schema" or "grammar".
|
||||
|
||||
@@ -325,8 +322,7 @@ def _unwrap_nested_schema(candidate: Any) -> dict | None:
|
||||
|
||||
|
||||
def _schema_from_json_schema_field(js_wr: Any) -> dict | None:
|
||||
"""
|
||||
Extract the JSON Schema object from Chat Completions ``json_schema`` payload.
|
||||
"""Extract the JSON Schema object from Chat Completions ``json_schema`` payload.
|
||||
|
||||
Accepts:
|
||||
- ``JsonSchemaResponseFormat`` (Pydantic) with ``schema`` / ``json_schema`` field
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""
|
||||
Streaming parser engine framework for tool call and reasoning extraction.
|
||||
"""Streaming parser engine framework for tool call and reasoning extraction.
|
||||
|
||||
Instead of hand-rolling a parser for every model's tool-call / reasoning
|
||||
format, each format is declared as a ParserEngineConfig (terminals,
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Incremental text lexer that converts text chunks into terminal
|
||||
tokens, with prefix-match buffering for ambiguous boundaries."""
|
||||
tokens, with prefix-match buffering for ambiguous boundaries.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Streaming parser engine that orchestrates token ID scanning,
|
||||
incremental lexing, and state-machine-driven semantic event emission."""
|
||||
incremental lexing, and state-machine-driven semantic event emission.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""Scan delta token IDs for special tokens and split the stream into
|
||||
pre-lexed terminals and plain text chunks."""
|
||||
pre-lexed terminals and plain text chunks.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
@@ -83,6 +83,7 @@ def _parse_gemma4_args(args_str: str, *, partial: bool = False) -> dict:
|
||||
(e.g. partial boolean parsed as bare string).
|
||||
|
||||
Returns a dict ready for ``json.dumps()``.
|
||||
|
||||
"""
|
||||
if not args_str or not args_str.strip():
|
||||
return {}
|
||||
|
||||
@@ -67,7 +67,8 @@ _WS = " \t\r\n"
|
||||
|
||||
def _scan_json_value(raw: str, start: int) -> int | None:
|
||||
"""Return the end index (exclusive) of the JSON object starting at
|
||||
``raw[start]``, or ``None`` when the object is still unterminated."""
|
||||
``raw[start]``, or ``None`` when the object is still unterminated.
|
||||
"""
|
||||
depth = 0
|
||||
in_string = False
|
||||
escape = False
|
||||
|
||||
@@ -136,6 +136,7 @@ def mistral_config(
|
||||
|
||||
Returns:
|
||||
A frozen :class:`ParserEngineConfig` with ``initial_state=CONTENT``.
|
||||
|
||||
"""
|
||||
if reasoning_encoding == "special_token":
|
||||
think_start = _THINK_START_SPECIAL
|
||||
@@ -467,6 +468,7 @@ class MistralParser(ParserEngine):
|
||||
|
||||
Returns:
|
||||
A JSON Schema dict, or ``None`` if the named tool is not found.
|
||||
|
||||
"""
|
||||
tool_choice = request.tool_choice
|
||||
tools = request.tools or []
|
||||
|
||||
@@ -16,9 +16,7 @@ logger = init_logger(__name__)
|
||||
|
||||
|
||||
class ParserManager:
|
||||
"""
|
||||
Provides a unified Parser by composing reasoning and tool parser adapters.
|
||||
"""
|
||||
"""Provides a unified Parser by composing reasoning and tool parser adapters."""
|
||||
|
||||
@classmethod
|
||||
def get_tool_parser(
|
||||
@@ -80,8 +78,7 @@ class ParserManager:
|
||||
model_name: str | None = None,
|
||||
is_harmony: bool = False,
|
||||
) -> type[Parser] | None:
|
||||
"""
|
||||
Get a Parser that handles both reasoning and tool parsing.
|
||||
"""Get a Parser that handles both reasoning and tool parsing.
|
||||
|
||||
Composes the individual parsers into a ``DelegatingParser`` subclass.
|
||||
|
||||
@@ -95,6 +92,7 @@ class ParserManager:
|
||||
|
||||
Returns:
|
||||
A Parser class, or None if neither parser is specified.
|
||||
|
||||
"""
|
||||
if not tool_parser_name and not reasoning_parser_name:
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user