Update Optional[x] -> x | None and Union[x, y] to x | y (#26633)

Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
Harry Mellor
2025-10-12 17:51:31 +01:00
committed by GitHub
parent 9bb38130cb
commit 8fcaaf6a16
944 changed files with 9490 additions and 10121 deletions

View File

@ -3,7 +3,6 @@
import asyncio
from contextlib import ExitStack
from typing import Optional
from unittest.mock import MagicMock
import pytest
@ -53,8 +52,8 @@ async def generate(
output_kind: RequestOutputKind,
max_tokens: int,
n: int = 1,
prompt_logprobs: Optional[int] = None,
cancel_after: Optional[int] = None,
prompt_logprobs: int | None = None,
cancel_after: int | None = None,
) -> tuple[int, str]:
# Ensure generate doesn't complete too fast for cancellation test.
await asyncio.sleep(0.2)
@ -545,9 +544,9 @@ async def collect_outputs(
prompt: PromptType,
sampling_params: SamplingParams,
outputs_list: list[RequestOutput],
) -> Optional[RequestOutput]:
) -> RequestOutput | None:
"""Helper to collect outputs and return the final one."""
final_output: Optional[RequestOutput] = None
final_output: RequestOutput | None = None
async for output in engine.generate(
request_id=request_id, prompt=prompt, sampling_params=sampling_params
):

View File

@ -8,7 +8,7 @@ import time
import uuid
from dataclasses import dataclass
from threading import Thread
from typing import Any, Optional, Union
from typing import Any
from unittest.mock import MagicMock
import pytest
@ -41,7 +41,7 @@ PROMPT_TOKENS = TOKENIZER(PROMPT).input_ids
def make_request(
params: SamplingParams, prompt_tokens_ids: Optional[list[int]] = None
params: SamplingParams, prompt_tokens_ids: list[int] | None = None
) -> EngineCoreRequest:
if not prompt_tokens_ids:
prompt_tokens_ids = PROMPT_TOKENS
@ -113,9 +113,7 @@ async def loop_until_fully_done_async(client: EngineCoreClient, outputs: dict):
# Dummy utility function to monkey-patch into engine core.
def echo(
self, msg: str, err_msg: Optional[str] = None, sleep: Optional[float] = None
) -> str:
def echo(self, msg: str, err_msg: str | None = None, sleep: float | None = None) -> str:
print(f"echo util function called: {msg}, {err_msg}")
if sleep is not None:
time.sleep(sleep)
@ -317,7 +315,7 @@ def echo_dc(
self,
msg: str,
return_list: bool = False,
) -> Union[MyDataclass, list[MyDataclass]]:
) -> MyDataclass | list[MyDataclass]:
print(f"echo dc util function called: {msg}")
val = None if msg is None else MyDataclass(msg)
# Return dataclass to verify support for returning custom types
@ -330,7 +328,7 @@ def echo_dc_dict(
self,
msg: str,
return_dict: bool = False,
) -> Union[MyDataclass, dict[str, MyDataclass]]:
) -> MyDataclass | dict[str, MyDataclass]:
print(f"echo dc dict util function called: {msg}")
val = None if msg is None else MyDataclass(msg)
# Return dict of dataclasses to verify support for returning dicts

View File

@ -1,7 +1,5 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from __future__ import annotations
import random
from typing import TYPE_CHECKING
@ -13,6 +11,8 @@ from vllm.v1.metrics.reader import Counter, Gauge, Histogram, Metric, Vector
if TYPE_CHECKING:
from tests.conftest import VllmRunner
else:
VllmRunner = object
MODEL = "facebook/opt-125m"
DTYPE = "half"

View File

@ -3,7 +3,6 @@
import math
import time
from typing import Optional
import pytest
@ -118,13 +117,13 @@ def test_incremental_detokenization(
def _validate_logprobs(
gen_tokens: dict[str, list[int]],
gen_logprobs: dict[str, Optional[SampleLogprobs]],
gen_prompt_logprobs: dict[str, Optional[PromptLogprobs]],
gen_logprobs: dict[str, SampleLogprobs | None],
gen_prompt_logprobs: dict[str, PromptLogprobs | None],
gen_cumulative_logprob: dict[str, float],
dtv: DummyOutputProcessorTestVectors,
request_id_list: list[str],
num_sample_logprobs: Optional[int],
num_prompt_logprobs: Optional[int],
num_sample_logprobs: int | None,
num_prompt_logprobs: int | None,
) -> None:
for req_idx, req_id in enumerate(request_id_list):
new_tokens = gen_tokens[req_id]
@ -413,8 +412,8 @@ def _validate_logprobs(
@pytest.mark.parametrize("num_prompt_logprobs", [None, NUM_PROMPT_LOGPROBS_UNDER_TEST])
def test_logprobs_processor(
request_output_kind: RequestOutputKind,
num_sample_logprobs: Optional[int],
num_prompt_logprobs: Optional[int],
num_sample_logprobs: int | None,
num_prompt_logprobs: int | None,
dummy_test_vectors,
):
output_processor = OutputProcessor(dummy_test_vectors.tokenizer, log_stats=False)
@ -530,7 +529,7 @@ def test_logprobs_processor(
)
def test_stop_token(
include_stop_str_in_output: bool,
num_sample_logprobs: Optional[int],
num_sample_logprobs: int | None,
stop_token_type: str,
ignore_eos: bool,
dummy_test_vectors,
@ -696,7 +695,7 @@ def test_stop_token(
@pytest.mark.parametrize("num_sample_logprobs", [None, NUM_SAMPLE_LOGPROBS_UNDER_TEST])
def test_stop_string(
include_stop_str_in_output: bool,
num_sample_logprobs: Optional[int],
num_sample_logprobs: int | None,
dummy_test_vectors,
):
output_processor = OutputProcessor(dummy_test_vectors.tokenizer, log_stats=False)

View File

@ -3,7 +3,7 @@
import random
from dataclasses import dataclass
from typing import Optional, Union
from typing import TypeAlias
import torch
from transformers import PreTrainedTokenizer, PreTrainedTokenizerFast
@ -12,7 +12,7 @@ from vllm.engine.arg_utils import EngineArgs
from vllm.v1.engine import EngineCoreOutput, FinishReason
from vllm.v1.outputs import LogprobsLists, LogprobsTensors
GeneralTokenizerType = Union[PreTrainedTokenizer, PreTrainedTokenizerFast]
GeneralTokenizerType: TypeAlias = PreTrainedTokenizer | PreTrainedTokenizerFast
# Number of sample logprobs to request when testing sample logprobs
NUM_SAMPLE_LOGPROBS_UNDER_TEST = 5
@ -332,16 +332,15 @@ class MockEngineCore:
# For each request, for each sampled token offset,
# a tuple of
# (list of topk token ids, list of sample logprob vals, rank)
generated_logprobs_raw: Optional[
list[list[tuple[list[int], list[float], int]]]
] = None,
generated_logprobs_raw: list[list[tuple[list[int], list[float], int]]]
| None = None,
# For each request, a tuple of
# (prompt logprob val matrix, prompt logprob tok id matrix);
# each matrix has dimensions
# (num prompt toks) x (num prompt logprobs+1)
prompt_logprobs_raw: Optional[list[LogprobsTensors]] = None,
eos_token_id: Optional[int] = None,
stop_token_ids: Optional[list[int]] = None,
prompt_logprobs_raw: list[LogprobsTensors] | None = None,
eos_token_id: int | None = None,
stop_token_ids: list[int] | None = None,
ignore_eos: bool = False,
) -> None:
self.num_requests = len(tokens_list)