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
@@ -116,7 +116,8 @@ def test_prompt_embeds_keys_registered():
|
||||
|
||||
def test_ensure_placeholder_token_is_single_token_and_idempotent(tokenizer):
|
||||
"""Ensure the placeholder token is a single token and that multiple calls to
|
||||
"ensure" are idempotent, across all tokenizer families."""
|
||||
"ensure" are idempotent, across all tokenizer families.
|
||||
"""
|
||||
tid1 = _ensure_prompt_embeds_placeholder_token(tokenizer)
|
||||
tid2 = _ensure_prompt_embeds_placeholder_token(tokenizer)
|
||||
assert tid1 == tid2
|
||||
@@ -469,7 +470,8 @@ def test_build_mixed_prompt_embeds(stream):
|
||||
@pytest.mark.parametrize("role", ["user", "system"])
|
||||
async def test_end_to_end_expand_and_build(tokenizer, parse_fn, role):
|
||||
"""Full renderer pipeline: parse -> chat template -> expand -> locate
|
||||
-> build mixed prompt, across tokenizers, roles, and sync/async."""
|
||||
-> build mixed prompt, across tokenizers, roles, and sync/async.
|
||||
"""
|
||||
tokenizer.chat_template = _SIMPLE_CHAT_TEMPLATE
|
||||
tid = _ensure_prompt_embeds_placeholder_token(tokenizer)
|
||||
|
||||
@@ -524,7 +526,8 @@ async def test_end_to_end_expand_and_build(tokenizer, parse_fn, role):
|
||||
@pytest.mark.asyncio
|
||||
async def test_end_to_end_multi_message_conversation(tokenizer, parse_fn):
|
||||
"""Full pipeline with prompt_embeds spread across system + user messages,
|
||||
verifying ordering and positioning in the final token stream."""
|
||||
verifying ordering and positioning in the final token stream.
|
||||
"""
|
||||
tokenizer.chat_template = _SIMPLE_CHAT_TEMPLATE
|
||||
tid = _ensure_prompt_embeds_placeholder_token(tokenizer)
|
||||
|
||||
|
||||
@@ -506,7 +506,7 @@ class TestRenderPrompt:
|
||||
|
||||
class TestRenderEmbedPrompt:
|
||||
def _create_test_embed_bytes(self, tensor: torch.Tensor) -> bytes:
|
||||
"""Helper to create base64-encoded tensor bytes"""
|
||||
"""Helper to create base64-encoded tensor bytes."""
|
||||
buffer = io.BytesIO()
|
||||
torch.save(tensor, buffer)
|
||||
buffer.seek(0)
|
||||
|
||||
@@ -33,7 +33,8 @@ def _render(template, messages, **kwargs):
|
||||
class TestGemma4ChatTemplate:
|
||||
def test_basic_multiturn_thinking_disabled(self, gemma4_template):
|
||||
"""With enable_thinking=False (default), generation prompt ends with
|
||||
an empty thought channel to suppress thinking."""
|
||||
an empty thought channel to suppress thinking.
|
||||
"""
|
||||
messages = [
|
||||
{"role": "user", "content": "Hello"},
|
||||
{"role": "assistant", "content": "Hi there!"},
|
||||
@@ -49,7 +50,8 @@ class TestGemma4ChatTemplate:
|
||||
|
||||
def test_basic_multiturn_thinking_enabled(self, gemma4_template):
|
||||
"""With enable_thinking=True, generation prompt ends with model
|
||||
turn opener (no thought suppression)."""
|
||||
turn opener (no thought suppression).
|
||||
"""
|
||||
messages = [
|
||||
{"role": "user", "content": "Hello"},
|
||||
{"role": "assistant", "content": "Hi there!"},
|
||||
@@ -144,7 +146,8 @@ class TestGemma4ChatTemplate:
|
||||
|
||||
def test_tool_responses_openai_style(self, gemma4_template):
|
||||
"""role='tool' messages are formatted as <|tool_response> blocks
|
||||
with content dumped as-is."""
|
||||
with content dumped as-is.
|
||||
"""
|
||||
messages = [
|
||||
{"role": "user", "content": "Weather?"},
|
||||
{
|
||||
@@ -202,7 +205,8 @@ class TestGemma4ChatTemplate:
|
||||
|
||||
def test_generation_prompt_not_after_tool_response(self, gemma4_template):
|
||||
"""add_generation_prompt=True should NOT add <|turn>model when the
|
||||
last message type was tool_response (the model turn continues)."""
|
||||
last message type was tool_response (the model turn continues).
|
||||
"""
|
||||
messages = [
|
||||
{"role": "user", "content": "Weather?"},
|
||||
{
|
||||
@@ -228,8 +232,9 @@ class TestGemma4ChatTemplate:
|
||||
assert not result.strip().endswith("<|turn>model\n")
|
||||
|
||||
def test_reasoning_in_tool_chains(self, gemma4_template):
|
||||
"""reasoning field on assistant with tool_calls after last user
|
||||
message emits <|channel>thought\\n...<channel|>."""
|
||||
"""Reasoning field on assistant with tool_calls after last user
|
||||
message emits <|channel>thought\\n...<channel|>.
|
||||
"""
|
||||
messages = [
|
||||
{"role": "user", "content": "Calculate something"},
|
||||
{
|
||||
@@ -252,7 +257,7 @@ class TestGemma4ChatTemplate:
|
||||
assert "<channel|>" in result
|
||||
|
||||
def test_reasoning_not_before_last_user(self, gemma4_template):
|
||||
"""reasoning on assistant BEFORE the last user message is dropped."""
|
||||
"""Reasoning on assistant BEFORE the last user message is dropped."""
|
||||
messages = [
|
||||
{"role": "user", "content": "First"},
|
||||
{
|
||||
@@ -275,7 +280,8 @@ class TestGemma4ChatTemplate:
|
||||
|
||||
def test_strip_thinking_in_model_content(self, gemma4_template):
|
||||
"""<|channel>...<channel|> in model content is stripped by the
|
||||
strip_thinking macro."""
|
||||
strip_thinking macro.
|
||||
"""
|
||||
messages = [
|
||||
{"role": "user", "content": "Hi"},
|
||||
{
|
||||
@@ -289,7 +295,8 @@ class TestGemma4ChatTemplate:
|
||||
|
||||
def test_multi_turn_tool_chain(self, gemma4_template):
|
||||
"""assistant->tool->assistant->tool produces exactly one
|
||||
<|turn>model (later assistants continue the same turn)."""
|
||||
<|turn>model (later assistants continue the same turn).
|
||||
"""
|
||||
messages = [
|
||||
{"role": "user", "content": "Do two things"},
|
||||
{
|
||||
@@ -346,7 +353,8 @@ class TestGemma4ChatTemplate:
|
||||
|
||||
def test_tool_response_with_multimodal_content(self, gemma4_template):
|
||||
"""Multimodal placeholders in tool messages are emitted after the
|
||||
tool_response block."""
|
||||
tool_response block.
|
||||
"""
|
||||
messages = [
|
||||
{"role": "user", "content": "Download the image and describe it."},
|
||||
{
|
||||
@@ -380,7 +388,8 @@ class TestGemma4ChatTemplate:
|
||||
|
||||
def test_tool_response_with_all_modalities(self, gemma4_template):
|
||||
"""All multimodal types (image, audio, video) in a single tool
|
||||
response are rendered."""
|
||||
response are rendered.
|
||||
"""
|
||||
messages = [
|
||||
{"role": "user", "content": "Process media"},
|
||||
{
|
||||
|
||||
@@ -123,7 +123,7 @@ def test_no_load_chat_template_literallike():
|
||||
)
|
||||
@pytest.mark.parametrize("use_tools", [True, False])
|
||||
def test_resolve_chat_template(sample_json_schema, model, use_tools):
|
||||
"""checks that chat_template is a dict type for HF models."""
|
||||
"""Checks that chat_template is a dict type for HF models."""
|
||||
model_info = HF_EXAMPLE_MODELS.find_hf_info(model)
|
||||
model_info.check_available_online(on_fail="skip")
|
||||
|
||||
@@ -196,7 +196,7 @@ def test_resolve_chat_template(sample_json_schema, model, use_tools):
|
||||
],
|
||||
)
|
||||
def test_resolve_chat_template_kwargs(sample_json_schema, model, expected_kwargs):
|
||||
"""checks that chat_template is a dict type for HF models."""
|
||||
"""Checks that chat_template is a dict type for HF models."""
|
||||
model_info = HF_EXAMPLE_MODELS.find_hf_info(model)
|
||||
model_info.check_available_online(on_fail="skip")
|
||||
|
||||
@@ -308,7 +308,8 @@ def test_resolve_chat_template_kwargs(sample_json_schema, model, expected_kwargs
|
||||
|
||||
def test_resolve_chat_template_resolves_name():
|
||||
"""When chat_template is a name, resolve_chat_template should return
|
||||
the actual Jinja content so that kwargs detection works correctly."""
|
||||
the actual Jinja content so that kwargs detection works correctly.
|
||||
"""
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
jinja_content = "{{ messages }}{% if tools %}{{ tools }}{% endif %}"
|
||||
@@ -330,7 +331,8 @@ def test_resolve_chat_template_resolves_name():
|
||||
|
||||
def test_resolve_chat_template_kwargs_with_template_name():
|
||||
"""Ensures template kwargs are not silently dropped when chat_template
|
||||
was originally a template name that has been resolved to Jinja content."""
|
||||
was originally a template name that has been resolved to Jinja content.
|
||||
"""
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
jinja_content = (
|
||||
@@ -932,7 +934,8 @@ EFFORT_BREAKING_TEMPLATE = (
|
||||
|
||||
class TestApplyChatTemplateEffortTolerant:
|
||||
"""Chat templates that reject unsupported reasoning_effort values should
|
||||
surface a 400-style client error instead of crashing the request."""
|
||||
surface a 400-style client error instead of crashing the request.
|
||||
"""
|
||||
|
||||
@pytest.fixture
|
||||
def model_config(self):
|
||||
|
||||
@@ -49,7 +49,8 @@ def _build_text_only_renderer() -> HfRenderer:
|
||||
|
||||
def test_text_only_model_mm_data_maps_to_bad_request():
|
||||
"""Sending multimodal data to a text-only model is a client mistake, so it
|
||||
must surface as a ValueError and reach the client as HTTP 400, not 500."""
|
||||
must surface as a ValueError and reach the client as HTTP 400, not 500.
|
||||
"""
|
||||
renderer = _build_text_only_renderer()
|
||||
|
||||
with pytest.raises(ValueError, match="text-only") as exc_info:
|
||||
|
||||
@@ -89,7 +89,7 @@ def test_unreadable_prompt_embeds_payload_is_a_client_error(model_config, payloa
|
||||
|
||||
@pytest.mark.parametrize("payload", UNPARSABLE_PAYLOADS)
|
||||
def test_the_error_body_stays_bounded(model_config, payload):
|
||||
"""torch's reason is built from the caller's bytes, so it must be truncated.
|
||||
"""Torch's reason is built from the caller's bytes, so it must be truncated.
|
||||
|
||||
An unpickler that reports the offending global, for instance, quotes a name
|
||||
the caller chose; without a bound a large one would be reflected whole.
|
||||
@@ -127,7 +127,8 @@ def test_a_long_reason_is_truncated_and_says_how_much_was_left_out(model_config)
|
||||
|
||||
def test_a_short_reason_is_passed_through_whole(model_config):
|
||||
"""A reason that fits is not annotated -- the count only appears when it is
|
||||
telling the reader something."""
|
||||
telling the reader something.
|
||||
"""
|
||||
reason = "invalid magic number; corrupt file?"
|
||||
assert len(reason) <= _MAX_EMBED_ERROR_REASON_CHARS
|
||||
|
||||
|
||||
@@ -218,7 +218,8 @@ class TestCrossLoaderLockSharing:
|
||||
|
||||
def test_prompt_and_image_share_lock(self, model_config):
|
||||
"""Prompt and image loaders cannot run their sparse guards
|
||||
concurrently."""
|
||||
concurrently.
|
||||
"""
|
||||
valid_prompt = _encode_tensor(_create_valid_dense_tensor())
|
||||
valid_image = _encode_tensor(_create_valid_dense_tensor(hidden_size=10))
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
"""
|
||||
Tests verify that malicious sparse tensors are rejected before they can trigger
|
||||
"""Tests verify that malicious sparse tensors are rejected before they can trigger
|
||||
out-of-bounds memory writes, or an unbounded allocation, during to_dense()
|
||||
operations.
|
||||
|
||||
@@ -51,8 +50,7 @@ def _encode_tensor(tensor: torch.Tensor) -> bytes:
|
||||
|
||||
|
||||
def _create_malicious_sparse_tensor() -> torch.Tensor:
|
||||
"""
|
||||
Create a malicious sparse COO tensor with out-of-bounds indices.
|
||||
"""Create a malicious sparse COO tensor with out-of-bounds indices.
|
||||
|
||||
This tensor has indices that point beyond the declared shape, which would
|
||||
cause an out-of-bounds write when converted to dense format without
|
||||
@@ -159,7 +157,8 @@ class TestPromptEmbedsValidation:
|
||||
|
||||
def test_hidden_size_mismatch_rejected(self, model_config):
|
||||
"""Tensors whose trailing dim doesn't match the model's hidden_size
|
||||
must be rejected at parse time."""
|
||||
must be rejected at parse time.
|
||||
"""
|
||||
# opt-125m has hidden_size=768, passing 512 triggers the check.
|
||||
wrong_hidden = torch.randn(10, 512, dtype=torch.float32)
|
||||
encoded = _encode_tensor(wrong_hidden)
|
||||
@@ -170,7 +169,8 @@ class TestPromptEmbedsValidation:
|
||||
def test_float_dtype_mismatch_cast_to_model_dtype(self, model_config):
|
||||
"""Tensors whose dtype doesn't match the model's dtype but are still
|
||||
floating-point are cast, since API clients generally can't know the
|
||||
server's `--dtype` setting ahead of time."""
|
||||
server's `--dtype` setting ahead of time.
|
||||
"""
|
||||
# Fixture pins model dtype to float32, upload a bfloat16 tensor.
|
||||
mismatched_float = torch.randn(10, 768, dtype=torch.bfloat16)
|
||||
encoded = _encode_tensor(mismatched_float)
|
||||
@@ -183,7 +183,8 @@ class TestPromptEmbedsValidation:
|
||||
def test_non_float_dtype_rejected(self, model_config):
|
||||
"""Non-floating-point dtypes cannot be safely cast for embeddings
|
||||
(e.g. integer tensors almost certainly indicate caller confusion),
|
||||
so they are rejected at parse time."""
|
||||
so they are rejected at parse time.
|
||||
"""
|
||||
non_float = torch.randint(0, 100, (10, 768), dtype=torch.int32)
|
||||
encoded = _encode_tensor(non_float)
|
||||
|
||||
@@ -192,7 +193,8 @@ class TestPromptEmbedsValidation:
|
||||
|
||||
def test_non_2d_tensor_rejected(self, model_config):
|
||||
"""Tensors that aren't 2D (even after squeezing a leading dim)
|
||||
must be rejected with a clear error."""
|
||||
must be rejected with a clear error.
|
||||
"""
|
||||
# A 1D tensor cannot be interpreted as (num_tokens, hidden_size).
|
||||
bad = torch.randn(768, dtype=torch.float32)
|
||||
encoded = _encode_tensor(bad)
|
||||
@@ -202,7 +204,8 @@ class TestPromptEmbedsValidation:
|
||||
|
||||
def test_non_tensor_payload_rejected(self, model_config):
|
||||
"""Deserializing to a non-Tensor object must raise a clear error
|
||||
instead of propagating an AssertionError."""
|
||||
instead of propagating an AssertionError.
|
||||
"""
|
||||
# `torch.save` will serialize a plain dict; `weights_only=True` allows
|
||||
# loading built-in containers, so this exercises the isinstance check.
|
||||
buffer = io.BytesIO()
|
||||
@@ -266,7 +269,7 @@ class TestImageEmbedsValidation:
|
||||
io_handler.load_bytes(buffer.read())
|
||||
|
||||
def test_valid_numpy_tensor_accepted(self):
|
||||
"""numpy .npy format should load and return correct tensor."""
|
||||
"""Numpy .npy format should load and return correct tensor."""
|
||||
io_handler = ImageEmbeddingMediaIO()
|
||||
|
||||
arr = np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]], dtype=np.float32)
|
||||
@@ -281,8 +284,7 @@ class TestImageEmbedsValidation:
|
||||
assert torch.allclose(result, torch.from_numpy(arr))
|
||||
|
||||
def test_numpy_int32_tensor_accepted(self):
|
||||
"""numpy int32 arrays should round-trip correctly."""
|
||||
|
||||
"""Numpy int32 arrays should round-trip correctly."""
|
||||
io_handler = ImageEmbeddingMediaIO()
|
||||
|
||||
arr = np.arange(280, dtype=np.int32)
|
||||
@@ -296,8 +298,7 @@ class TestImageEmbedsValidation:
|
||||
assert (result == torch.from_numpy(arr)).all()
|
||||
|
||||
def test_load_file_numpy_tensor_accepted(self, tmp_path):
|
||||
"""numpy .npy files should load correctly via load_file."""
|
||||
|
||||
"""Numpy .npy files should load correctly via load_file."""
|
||||
io_handler = ImageEmbeddingMediaIO()
|
||||
|
||||
arr = np.array([[1.5, 2.5], [3.5, 4.5]], dtype=np.float32)
|
||||
@@ -364,13 +365,10 @@ class TestAudioEmbedsValidation:
|
||||
|
||||
|
||||
class TestSparseTensorValidationIntegration:
|
||||
"""
|
||||
These tests verify the complete attack chain is blocked at all entry points.
|
||||
"""
|
||||
"""These tests verify the complete attack chain is blocked at all entry points."""
|
||||
|
||||
def test_attack_scenario_completions_api(self, model_config):
|
||||
"""
|
||||
Simulate a complete attack through the Completions API.
|
||||
"""Simulate a complete attack through the Completions API.
|
||||
|
||||
Attack scenario:
|
||||
1. Attacker crafts malicious sparse tensor
|
||||
@@ -386,8 +384,7 @@ class TestSparseTensorValidationIntegration:
|
||||
safe_load_prompt_embeds(model_config, attack_payload)
|
||||
|
||||
def test_attack_scenario_chat_api_image(self):
|
||||
"""
|
||||
Simulate attack through Chat API with image_embeds.
|
||||
"""Simulate attack through Chat API with image_embeds.
|
||||
|
||||
Verifies the image embeddings path is protected.
|
||||
"""
|
||||
@@ -398,8 +395,7 @@ class TestSparseTensorValidationIntegration:
|
||||
io_handler.load_base64("", attack_payload.decode("utf-8"))
|
||||
|
||||
def test_attack_scenario_chat_api_audio(self):
|
||||
"""
|
||||
Simulate attack through Chat API with audio_embeds.
|
||||
"""Simulate attack through Chat API with audio_embeds.
|
||||
|
||||
Verifies the audio embeddings path is protected.
|
||||
"""
|
||||
@@ -467,7 +463,8 @@ class TestEmbeddingDecodeSizeLimit:
|
||||
def test_default_limit_would_reject_the_real_payload(self):
|
||||
"""The shipped default has to bound the actual attack, not just the
|
||||
shrunken shape used above. Checked arithmetically so no test ever asks
|
||||
the allocator for 4 TiB."""
|
||||
the allocator for 4 TiB.
|
||||
"""
|
||||
dense_bytes = BOMB_SHAPE[0] * BOMB_SHAPE[1] * 4
|
||||
assert dense_bytes > envs.VLLM_MAX_EMBED_DECODE_BYTES > 0
|
||||
|
||||
@@ -525,7 +522,8 @@ class TestEmbeddingDecodeSizeLimit:
|
||||
@pytest.mark.parametrize("io_cls,parameter", EMBEDDING_MEDIA_IO)
|
||||
def test_media_io_rejects_non_tensor_payload(self, io_cls, parameter):
|
||||
"""These three had no `isinstance` check, so a non-tensor payload
|
||||
surfaced as an `AttributeError` (500) rather than a 4xx."""
|
||||
surfaced as an `AttributeError` (500) rather than a 4xx.
|
||||
"""
|
||||
buffer = io.BytesIO()
|
||||
torch.save({"not": "a tensor"}, buffer)
|
||||
encoded = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
||||
|
||||
@@ -26,7 +26,8 @@ def fast_tokenizer():
|
||||
def _make_base_renderer_with(tokenizer):
|
||||
"""Build a minimal BaseRenderer subclass that exposes the tokenizer so we
|
||||
can call ``_tokenize_prompt`` directly. BaseRenderer is abstract because of
|
||||
``render_messages``; we just need a stub."""
|
||||
``render_messages``; we just need a stub.
|
||||
"""
|
||||
from vllm.renderers.base import BaseRenderer
|
||||
|
||||
class _StubRenderer(BaseRenderer):
|
||||
@@ -91,7 +92,8 @@ class TestTokenizePromptOffsets:
|
||||
def test_base_renderer_without_override_yields_no_offsets(self, fast_tokenizer):
|
||||
"""A renderer that does not override ``_can_produce_offsets`` never
|
||||
emits offsets, even with a fast tokenizer and the flag set. This locks
|
||||
in the base-default-False / subclass-override design."""
|
||||
in the base-default-False / subclass-override design.
|
||||
"""
|
||||
from vllm.renderers.base import BaseRenderer
|
||||
|
||||
class _BareRenderer(BaseRenderer):
|
||||
@@ -125,7 +127,8 @@ class TestTokenizePromptOffsets:
|
||||
|
||||
def test_slow_tokenizer_with_flag_no_offsets(self, fast_tokenizer):
|
||||
"""Force is_fast=False to simulate a Slow tokenizer: the flag is set
|
||||
but offsets must not be returned because it cannot produce them."""
|
||||
but offsets must not be returned because it cannot produce them.
|
||||
"""
|
||||
from unittest.mock import PropertyMock, patch
|
||||
|
||||
renderer = _make_base_renderer_with(fast_tokenizer)
|
||||
@@ -144,7 +147,8 @@ class TestTokenizePromptOffsets:
|
||||
@pytest.mark.parametrize("mm_key", ["multi_modal_data", "multi_modal_uuids"])
|
||||
def test_multimodal_with_flag_no_offsets(self, fast_tokenizer, mm_key):
|
||||
"""Offsets index the text prompt, which is meaningless once multimodal
|
||||
data is interleaved, so they are suppressed when MM inputs are present."""
|
||||
data is interleaved, so they are suppressed when MM inputs are present.
|
||||
"""
|
||||
renderer = _make_base_renderer_with(fast_tokenizer)
|
||||
params = TokenizeParams(max_total_tokens=None, return_token_offsets=True)
|
||||
prompt = {"prompt": "Hello.", mm_key: {"image": ["x"]}}
|
||||
@@ -156,7 +160,8 @@ class TestTokenizePromptOffsets:
|
||||
@pytest.mark.asyncio
|
||||
async def test_tokenize_prompt_async_returns_offsets(self, fast_tokenizer):
|
||||
"""The async path offloads the sync tokenizer; it must yield the same
|
||||
offsets as the sync path."""
|
||||
offsets as the sync path.
|
||||
"""
|
||||
renderer = _make_base_renderer_with(fast_tokenizer)
|
||||
params = TokenizeParams(max_total_tokens=None, return_token_offsets=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user