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 09:51:31 -07:00
committed by GitHub
parent 9bb38130cb
commit 8fcaaf6a16
944 changed files with 9490 additions and 10121 deletions
+33 -31
View File
@@ -1,8 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from __future__ import annotations
import asyncio
import concurrent
import contextlib
@@ -46,6 +44,7 @@ from collections import UserDict, defaultdict
from collections.abc import (
AsyncGenerator,
Awaitable,
Callable,
Collection,
Generator,
Hashable,
@@ -62,12 +61,10 @@ from pathlib import Path
from typing import (
TYPE_CHECKING,
Any,
Callable,
Generic,
Literal,
TextIO,
TypeVar,
Union,
)
from urllib.parse import urlparse
from uuid import uuid4
@@ -99,6 +96,12 @@ if TYPE_CHECKING:
from vllm.config import ModelConfig, VllmConfig
from vllm.sequence import IntermediateTensors
else:
Namespace = object
ModelConfig = object
VllmConfig = object
IntermediateTensors = object
logger = init_logger(__name__)
@@ -245,9 +248,7 @@ class AsyncMicrobatchTokenizer:
self._queues: dict[
tuple,
asyncio.Queue[
Union[
tuple[str, dict, asyncio.Future], tuple[list[int], asyncio.Future]
]
tuple[str, dict, asyncio.Future] | tuple[list[int], asyncio.Future]
],
] = {}
self._batcher_tasks: list[asyncio.Task] = []
@@ -274,7 +275,7 @@ class AsyncMicrobatchTokenizer:
def _get_queue(
self, loop: asyncio.AbstractEventLoop, key: tuple
) -> asyncio.Queue[
Union[tuple[str, dict, asyncio.Future], tuple[list[int], asyncio.Future]]
tuple[str, dict, asyncio.Future] | tuple[list[int], asyncio.Future]
]:
"""Get the request queue for the given operation key, creating a new
queue and batcher task if needed."""
@@ -431,7 +432,7 @@ def cancel_task_threadsafe(task: Task):
run_in_loop(task.get_loop(), task.cancel)
def close_sockets(sockets: Sequence[Union[zmq.Socket, zmq.asyncio.Socket]]):
def close_sockets(sockets: Sequence[zmq.Socket | zmq.asyncio.Socket]):
for sock in sockets:
if sock is not None:
sock.close(linger=0)
@@ -767,8 +768,8 @@ def _generate_random_fp8(
def get_kv_cache_torch_dtype(
cache_dtype: Union[str, torch.dtype] | None,
model_dtype: Union[str, torch.dtype] | None = None,
cache_dtype: str | torch.dtype | None,
model_dtype: str | torch.dtype | None = None,
) -> torch.dtype:
if isinstance(cache_dtype, str):
if cache_dtype == "auto":
@@ -795,8 +796,8 @@ def create_kv_caches_with_random_flash(
num_layers: int,
num_heads: int,
head_size: int,
cache_dtype: Union[str, torch.dtype] | None,
model_dtype: Union[str, torch.dtype] | None = None,
cache_dtype: str | torch.dtype | None,
model_dtype: str | torch.dtype | None = None,
seed: int | None = None,
device: str | None = "cuda",
cache_layout: str | None = "NHD",
@@ -837,8 +838,8 @@ def create_kv_caches_with_random(
num_layers: int,
num_heads: int,
head_size: int,
cache_dtype: Union[str, torch.dtype] | None,
model_dtype: Union[str, torch.dtype] | None = None,
cache_dtype: str | torch.dtype | None,
model_dtype: str | torch.dtype | None = None,
seed: int | None = None,
device: str | None = "cuda",
) -> tuple[list[torch.Tensor], list[torch.Tensor]]:
@@ -952,7 +953,7 @@ def make_tensor_with_pad(
dtype: torch.dtype,
*,
max_len: int | None = None,
device: Union[str, torch.device] | None = None,
device: str | torch.device | None = None,
pin_memory: bool = False,
) -> torch.Tensor:
"""
@@ -974,7 +975,7 @@ def make_tensor_with_pad(
def async_tensor_h2d(
data: list,
dtype: torch.dtype,
target_device: Union[str, torch.device],
target_device: str | torch.device,
pin_memory: bool,
) -> torch.Tensor:
"""Asynchronously create a tensor and copy it from host to device."""
@@ -1041,7 +1042,7 @@ def as_list(maybe_list: Iterable[T]) -> list[T]:
return maybe_list if isinstance(maybe_list, list) else list(maybe_list)
def as_iter(obj: Union[T, Iterable[T]]) -> Iterable[T]:
def as_iter(obj: T | Iterable[T]) -> Iterable[T]:
if isinstance(obj, str) or not isinstance(obj, Iterable):
return [obj] # type: ignore[list-item]
return obj
@@ -1050,7 +1051,7 @@ def as_iter(obj: Union[T, Iterable[T]]) -> Iterable[T]:
# `collections` helpers
def is_list_of(
value: object,
typ: Union[type[T], tuple[type[T], ...]],
typ: type[T] | tuple[type[T], ...],
*,
check: Literal["first", "all"] = "first",
) -> TypeIs[list[T]]:
@@ -1266,7 +1267,7 @@ F = TypeVar("F", bound=Callable[..., Any])
def deprecate_args(
start_index: int,
is_deprecated: Union[bool, Callable[[], bool]] = True,
is_deprecated: bool | Callable[[], bool] = True,
additional_message: str | None = None,
) -> Callable[[F], F]:
if not callable(is_deprecated):
@@ -1306,7 +1307,7 @@ def deprecate_args(
def deprecate_kwargs(
*kws: str,
is_deprecated: Union[bool, Callable[[], bool]] = True,
is_deprecated: bool | Callable[[], bool] = True,
additional_message: str | None = None,
) -> Callable[[F], F]:
deprecated_kws = set(kws)
@@ -1892,7 +1893,7 @@ class FlexibleArgumentParser(ArgumentParser):
# only expecting a flat dictionary of atomic types
processed_args: list[str] = []
config: dict[str, Union[int, str]] = {}
config: dict[str, int | str] = {}
try:
with open(file_path) as config_file:
config = yaml.safe_load(config_file)
@@ -2149,10 +2150,11 @@ def weak_ref_tensor(tensor: Any) -> Any:
def weak_ref_tensors(
tensors: Union[
torch.Tensor, list[torch.Tensor], tuple[torch.Tensor], IntermediateTensors
],
) -> Union[torch.Tensor, list[Any], tuple[Any], Any]:
tensors: torch.Tensor
| list[torch.Tensor]
| tuple[torch.Tensor]
| IntermediateTensors,
) -> torch.Tensor | list[Any] | tuple[Any] | Any:
"""
Convenience function to create weak references to tensors,
for single tensor, list of tensors or tuple of tensors.
@@ -2183,7 +2185,7 @@ def get_cuda_view_from_cpu_tensor(cpu_tensor: torch.Tensor) -> torch.Tensor:
return torch.ops._C.get_cuda_view_from_cpu_tensor(cpu_tensor)
def import_from_path(module_name: str, file_path: Union[str, os.PathLike]):
def import_from_path(module_name: str, file_path: str | os.PathLike):
"""
Import a Python file according to its file path.
@@ -2584,7 +2586,7 @@ class MemorySnapshot:
self.non_torch_memory = self.cuda_memory - self.torch_memory
self.timestamp = time.time()
def __sub__(self, other: MemorySnapshot) -> MemorySnapshot:
def __sub__(self, other: "MemorySnapshot") -> "MemorySnapshot":
return MemorySnapshot(
torch_peak=self.torch_peak - other.torch_peak,
free_memory=self.free_memory - other.free_memory,
@@ -2778,13 +2780,13 @@ def make_zmq_path(scheme: str, host: str, port: int | None = None) -> str:
# Adapted from: https://github.com/sgl-project/sglang/blob/v0.4.1/python/sglang/srt/utils.py#L783 # noqa: E501
def make_zmq_socket(
ctx: Union[zmq.asyncio.Context, zmq.Context], # type: ignore[name-defined]
ctx: zmq.asyncio.Context | zmq.Context, # type: ignore[name-defined]
path: str,
socket_type: Any,
bind: bool | None = None,
identity: bytes | None = None,
linger: int | None = None,
) -> Union[zmq.Socket, zmq.asyncio.Socket]: # type: ignore[name-defined]
) -> zmq.Socket | zmq.asyncio.Socket: # type: ignore[name-defined]
"""Make a ZMQ socket with the proper bind/connect semantics."""
mem = psutil.virtual_memory()
@@ -2950,7 +2952,7 @@ def bind_kv_cache(
def run_method(
obj: Any,
method: Union[str, bytes, Callable],
method: str | bytes | Callable,
args: tuple[Any],
kwargs: dict[str, Any],
) -> Any:
+9 -15
View File
@@ -1,11 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from __future__ import annotations
from collections import UserDict
from collections.abc import Hashable, Iterator, KeysView, Mapping
from collections.abc import Callable, Hashable, Iterator, KeysView, Mapping
from types import MappingProxyType
from typing import Callable, Generic, NamedTuple, TypeVar, Union, cast, overload
from typing import Generic, NamedTuple, TypeVar, cast, overload
import cachetools
@@ -43,7 +41,7 @@ class CacheInfo(NamedTuple):
return self.hits / self.total
def __sub__(self, other: CacheInfo):
def __sub__(self, other: "CacheInfo"):
return CacheInfo(
hits=self.hits - other.hits,
total=self.total - other.total,
@@ -129,12 +127,10 @@ class LRUCache(cachetools.LRUCache[_K, _V], Generic[_K, _V]):
def get(self, key: _K, /) -> _V | None: ...
@overload
def get(self, key: _K, /, default: Union[_V, _T]) -> Union[_V, _T]: ...
def get(self, key: _K, /, default: _V | _T) -> _V | _T: ...
def get(
self, key: _K, /, default: Union[_V, _T] | None = None
) -> Union[_V, _T] | None:
value: Union[_V, _T] | None
def get(self, key: _K, /, default: _V | _T | None = None) -> _V | _T | None:
value: _V | _T | None
if key in self:
value = self.__getitem__(key, update_info=False) # type: ignore[call-arg]
@@ -149,12 +145,10 @@ class LRUCache(cachetools.LRUCache[_K, _V], Generic[_K, _V]):
def pop(self, key: _K) -> _V: ...
@overload
def pop(self, key: _K, default: Union[_V, _T]) -> Union[_V, _T]: ...
def pop(self, key: _K, default: _V | _T) -> _V | _T: ...
def pop(
self, key: _K, default: Union[_V, _T] | None = None
) -> Union[_V, _T] | None:
value: Union[_V, _T] | None
def pop(self, key: _K, default: _V | _T | None = None) -> _V | _T | None:
value: _V | _T | None
if key not in self:
return default
+2 -3
View File
@@ -5,12 +5,11 @@
Users of vLLM should always import **only** these wrappers.
"""
from __future__ import annotations
import functools
import importlib
import os
from typing import Any, Callable, NoReturn
from collections.abc import Callable
from typing import Any, NoReturn
import torch
+2 -3
View File
@@ -5,15 +5,14 @@
Users of vLLM should always import **only** these wrappers.
"""
from __future__ import annotations
import contextlib
import functools
import importlib
import importlib.util
import os
import shutil
from typing import Any, Callable, NoReturn
from collections.abc import Callable
from typing import Any, NoReturn
import requests
import torch
+2 -2
View File
@@ -5,7 +5,7 @@ import json
import time
from collections import Counter
from contextlib import suppress
from typing import Any, Optional
from typing import Any
from vllm.envs import VLLM_GC_DEBUG
from vllm.logger import init_logger
@@ -21,7 +21,7 @@ class GCDebugConfig:
- '{"top_objects":5}': enable GC debugger with top 5 collected objects
"""
def __init__(self, gc_debug_conf: Optional[str] = None) -> None:
def __init__(self, gc_debug_conf: str | None = None) -> None:
self.enabled: bool = False
self.top_objects: int = -1
+27 -30
View File
@@ -2,9 +2,9 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Helper functions to work with nested JSON structures."""
from collections.abc import Iterable
from collections.abc import Callable, Iterable
from functools import reduce
from typing import TYPE_CHECKING, Callable, TypeVar, Union, cast, overload
from typing import TYPE_CHECKING, TypeAlias, TypeVar, cast, overload
if TYPE_CHECKING:
import torch
@@ -14,23 +14,20 @@ if TYPE_CHECKING:
_T = TypeVar("_T")
_U = TypeVar("_U")
JSONTree = Union[
dict[str, "JSONTree[_T]"],
list["JSONTree[_T]"],
tuple["JSONTree[_T]", ...],
_T,
]
JSONTree: TypeAlias = (
dict[str, "JSONTree[_T]"] | list["JSONTree[_T]"] | tuple["JSONTree[_T]", ...] | _T
)
"""A nested JSON structure where the leaves need not be JSON-serializable."""
_JSONTree = Union[
dict[str, "JSONTree[_T]"],
list["JSONTree[_T]"],
tuple["JSONTree[_T]", ...],
dict[str, _T],
list[_T],
tuple[_T, ...],
_T,
]
_JSONTree: TypeAlias = (
dict[str, "JSONTree[_T]"]
| list["JSONTree[_T]"]
| tuple["JSONTree[_T]", ...]
| dict[str, _T]
| list[_T]
| tuple[_T, ...]
| _T
)
"""
Same as `JSONTree` but with additional `Union` members to satisfy overloads.
"""
@@ -58,22 +55,22 @@ def json_map_leaves(
@overload
def json_map_leaves(
func: Callable[[_T], _U],
value: Union[_T, dict[str, _T]],
) -> Union[_U, dict[str, _U]]: ...
value: _T | dict[str, _T],
) -> _U | dict[str, _U]: ...
@overload
def json_map_leaves(
func: Callable[[_T], _U],
value: Union[_T, list[_T]],
) -> Union[_U, list[_U]]: ...
value: _T | list[_T],
) -> _U | list[_U]: ...
@overload
def json_map_leaves(
func: Callable[[_T], _U],
value: Union[_T, tuple[_T, ...]],
) -> Union[_U, tuple[_U, ...]]: ...
value: _T | tuple[_T, ...],
) -> _U | tuple[_U, ...]: ...
@overload
@@ -85,8 +82,8 @@ def json_map_leaves(
def json_map_leaves(
func: Callable[[_T], _U],
value: Union["BatchedTensorInputs", _JSONTree[_T]],
) -> Union["BatchedTensorInputs", _JSONTree[_U]]:
value: "BatchedTensorInputs" | _JSONTree[_T],
) -> "BatchedTensorInputs" | _JSONTree[_U]:
"""Apply a function to each leaf in a nested JSON structure."""
if isinstance(value, dict):
return {
@@ -104,7 +101,7 @@ def json_map_leaves(
@overload
def json_reduce_leaves(
func: Callable[[_T, _T], _T],
value: Union[_T, dict[str, _T]],
value: _T | dict[str, _T],
/,
) -> _T: ...
@@ -112,7 +109,7 @@ def json_reduce_leaves(
@overload
def json_reduce_leaves(
func: Callable[[_T, _T], _T],
value: Union[_T, list[_T]],
value: _T | list[_T],
/,
) -> _T: ...
@@ -120,7 +117,7 @@ def json_reduce_leaves(
@overload
def json_reduce_leaves(
func: Callable[[_T, _T], _T],
value: Union[_T, tuple[_T, ...]],
value: _T | tuple[_T, ...],
/,
) -> _T: ...
@@ -143,11 +140,11 @@ def json_reduce_leaves(
def json_reduce_leaves(
func: Callable[..., Union[_T, _U]],
func: Callable[..., _T | _U],
value: _JSONTree[_T],
initial: _U = cast(_U, ...), # noqa: B008
/,
) -> Union[_T, _U]:
) -> _T | _U:
"""
Apply a function of two arguments cumulatively to each leaf in a
nested JSON structure, from left to right, so as to reduce the
+12 -10
View File
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from typing import Annotated, Any, Optional, Union, get_args, get_origin, get_type_hints
from types import UnionType
from typing import Annotated, Any, Union, get_args, get_origin, get_type_hints
import torch
@@ -12,16 +13,16 @@ logger = init_logger(__name__)
class TensorShape:
def __init__(
self,
*dims: Union[int, str],
dynamic_dims: Optional[set[str]] = None,
*dims: int | str,
dynamic_dims: set[str] | None = None,
) -> None:
super().__init__()
self.dims = dims
self.dynamic_dims = dynamic_dims if dynamic_dims else set()
def resolve(self, **bindings: int) -> tuple[Union[int, str], ...]:
resolved = list[Union[int, str]]()
def resolve(self, **bindings: int) -> tuple[int | str, ...]:
resolved = list[int | str]()
for dim in self.dims:
if isinstance(dim, str) and dim in bindings:
resolved.append(bindings[dim])
@@ -48,7 +49,7 @@ class TensorSchema:
self,
*,
validate: bool = True,
resolve_bindings: Optional[dict[str, int]] = None,
resolve_bindings: dict[str, int] | None = None,
**kwargs: Any,
) -> None:
super().__init__()
@@ -71,7 +72,7 @@ class TensorSchema:
self,
actual: tuple[int, ...],
reference: tuple[int, ...],
expected_shape: tuple[Union[int, str], ...],
expected_shape: tuple[int | str, ...],
dynamic_dims: set[str],
) -> bool:
if len(actual) != len(reference) or len(actual) > len(expected_shape):
@@ -100,7 +101,7 @@ class TensorSchema:
self,
value: object,
field_name: str,
expected_shape: tuple[Union[int, str], ...],
expected_shape: tuple[int | str, ...],
dynamic_dims: set[str],
leading_idxs: tuple[int, ...] = (),
) -> tuple[int, ...]:
@@ -154,7 +155,7 @@ class TensorSchema:
def _validate_tensor_shape_expected(
self,
actual_shape: tuple[int, ...],
expected_shape: tuple[Union[int, str], ...],
expected_shape: tuple[int | str, ...],
field_name: str,
shape_env: dict[str, int],
dynamic_dims: set[str],
@@ -209,7 +210,8 @@ class TensorSchema:
actual_type = args[0]
# Check arg was provided as Union
if get_origin(actual_type) is Union:
if get_origin(actual_type) in {Union, UnionType}:
# Union for Union[X, Y] and UnionType for X | Y
args = get_args(actual_type)
# Skip validation when Union contains None
if type(None) in args: