Skip to content

Commit c1e6830

Browse files
DarkLight1337lulmer
authored andcommitted
[Misc] Upgrade to Python 3.9 typing for additional directories (vllm-project#14492)
Signed-off-by: DarkLight1337 <tlleungac@connect.ust.hk> Signed-off-by: Louis Ulmer <ulmerlouis@gmail.com>
1 parent 8a94c6c commit c1e6830

File tree

12 files changed

+78
-78
lines changed

12 files changed

+78
-78
lines changed

pyproject.toml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,16 @@ exclude = [
7575
"vllm/distributed/**/*.py" = ["UP006", "UP035"]
7676
"vllm/engine/**/*.py" = ["UP006", "UP035"]
7777
"vllm/executor/**/*.py" = ["UP006", "UP035"]
78-
"vllm/inputs/**/*.py" = ["UP006", "UP035"]
79-
"vllm/logging_utils/**/*.py" = ["UP006", "UP035"]
8078
"vllm/lora/**/*.py" = ["UP006", "UP035"]
8179
"vllm/model_executor/**/*.py" = ["UP006", "UP035"]
82-
"vllm/multimodal/**/*.py" = ["UP006", "UP035"]
8380
"vllm/platforms/**/*.py" = ["UP006", "UP035"]
8481
"vllm/plugins/**/*.py" = ["UP006", "UP035"]
8582
"vllm/profiler/**/*.py" = ["UP006", "UP035"]
8683
"vllm/prompt_adapter/**/*.py" = ["UP006", "UP035"]
8784
"vllm/spec_decode/**/*.py" = ["UP006", "UP035"]
8885
"vllm/transformers_utils/**/*.py" = ["UP006", "UP035"]
8986
"vllm/triton_utils/**/*.py" = ["UP006", "UP035"]
90-
"vllm/usage/**/*.py" = ["UP006", "UP035"]
9187
"vllm/vllm_flash_attn/**/*.py" = ["UP006", "UP035"]
92-
"vllm/assets/**/*.py" = ["UP006", "UP035"]
9388
"vllm/worker/**/*.py" = ["UP006", "UP035"]
9489

9590
[tool.ruff.lint]

vllm/assets/video.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from dataclasses import dataclass
44
from functools import lru_cache
5-
from typing import List, Literal
5+
from typing import Literal
66

77
import cv2
88
import numpy as np
@@ -58,7 +58,7 @@ def video_to_ndarrays(path: str, num_frames: int = -1) -> npt.NDArray:
5858

5959

6060
def video_to_pil_images_list(path: str,
61-
num_frames: int = -1) -> List[Image.Image]:
61+
num_frames: int = -1) -> list[Image.Image]:
6262
frames = video_to_ndarrays(path, num_frames)
6363
return [
6464
Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
@@ -72,7 +72,7 @@ class VideoAsset:
7272
num_frames: int = -1
7373

7474
@property
75-
def pil_images(self) -> List[Image.Image]:
75+
def pil_images(self) -> list[Image.Image]:
7676
video_path = download_video_asset(self.name)
7777
ret = video_to_pil_images_list(video_path, self.num_frames)
7878
return ret

vllm/inputs/data.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3+
from collections.abc import Iterable
34
from dataclasses import dataclass
45
from functools import cached_property
5-
from typing import (TYPE_CHECKING, Any, Dict, Generic, Iterable, List, Literal,
6-
Optional, Tuple, Union, cast)
6+
from typing import TYPE_CHECKING, Any, Generic, Literal, Optional, Union, cast
77

88
import torch
99
from typing_extensions import NotRequired, TypedDict, TypeVar, assert_never
@@ -26,7 +26,7 @@ class TextPrompt(TypedDict):
2626
if the model supports it.
2727
"""
2828

29-
mm_processor_kwargs: NotRequired[Dict[str, Any]]
29+
mm_processor_kwargs: NotRequired[dict[str, Any]]
3030
"""
3131
Optional multi-modal processor kwargs to be forwarded to the
3232
multimodal input mapper & processor. Note that if multiple modalities
@@ -38,10 +38,10 @@ class TextPrompt(TypedDict):
3838
class TokensPrompt(TypedDict):
3939
"""Schema for a tokenized prompt."""
4040

41-
prompt_token_ids: List[int]
41+
prompt_token_ids: list[int]
4242
"""A list of token IDs to pass to the model."""
4343

44-
token_type_ids: NotRequired[List[int]]
44+
token_type_ids: NotRequired[list[int]]
4545
"""A list of token type IDs to pass to the cross encoder model."""
4646

4747
multi_modal_data: NotRequired["MultiModalDataDict"]
@@ -50,7 +50,7 @@ class TokensPrompt(TypedDict):
5050
if the model supports it.
5151
"""
5252

53-
mm_processor_kwargs: NotRequired[Dict[str, Any]]
53+
mm_processor_kwargs: NotRequired[dict[str, Any]]
5454
"""
5555
Optional multi-modal processor kwargs to be forwarded to the
5656
multimodal input mapper & processor. Note that if multiple modalities
@@ -115,7 +115,7 @@ class ExplicitEncoderDecoderPrompt(TypedDict, Generic[_T1_co, _T2_co]):
115115

116116
decoder_prompt: Optional[_T2_co]
117117

118-
mm_processor_kwargs: NotRequired[Dict[str, Any]]
118+
mm_processor_kwargs: NotRequired[dict[str, Any]]
119119

120120

121121
PromptType = Union[SingletonPrompt, ExplicitEncoderDecoderPrompt]
@@ -136,10 +136,10 @@ class TokenInputs(TypedDict):
136136
type: Literal["token"]
137137
"""The type of inputs."""
138138

139-
prompt_token_ids: List[int]
139+
prompt_token_ids: list[int]
140140
"""The token IDs of the prompt."""
141141

142-
token_type_ids: NotRequired[List[int]]
142+
token_type_ids: NotRequired[list[int]]
143143
"""The token type IDs of the prompt."""
144144

145145
prompt: NotRequired[str]
@@ -164,12 +164,12 @@ class TokenInputs(TypedDict):
164164
Placeholder ranges for the multi-modal data.
165165
"""
166166

167-
multi_modal_hashes: NotRequired[List[str]]
167+
multi_modal_hashes: NotRequired[list[str]]
168168
"""
169169
The hashes of the multi-modal data.
170170
"""
171171

172-
mm_processor_kwargs: NotRequired[Dict[str, Any]]
172+
mm_processor_kwargs: NotRequired[dict[str, Any]]
173173
"""
174174
Optional multi-modal processor kwargs to be forwarded to the
175175
multimodal input mapper & processor. Note that if multiple modalities
@@ -179,14 +179,14 @@ class TokenInputs(TypedDict):
179179

180180

181181
def token_inputs(
182-
prompt_token_ids: List[int],
183-
token_type_ids: Optional[List[int]] = None,
182+
prompt_token_ids: list[int],
183+
token_type_ids: Optional[list[int]] = None,
184184
prompt: Optional[str] = None,
185185
multi_modal_data: Optional["MultiModalDataDict"] = None,
186186
multi_modal_inputs: Optional["MultiModalKwargs"] = None,
187-
multi_modal_hashes: Optional[List[str]] = None,
187+
multi_modal_hashes: Optional[list[str]] = None,
188188
multi_modal_placeholders: Optional["MultiModalPlaceholderDict"] = None,
189-
mm_processor_kwargs: Optional[Dict[str, Any]] = None,
189+
mm_processor_kwargs: Optional[dict[str, Any]] = None,
190190
) -> TokenInputs:
191191
"""Construct :class:`TokenInputs` from optional values."""
192192
inputs = TokenInputs(type="token", prompt_token_ids=prompt_token_ids)
@@ -255,7 +255,7 @@ def prompt(self) -> Optional[str]:
255255
assert_never(inputs) # type: ignore[arg-type]
256256

257257
@cached_property
258-
def prompt_token_ids(self) -> List[int]:
258+
def prompt_token_ids(self) -> list[int]:
259259
inputs = self.inputs
260260

261261
if inputs["type"] == "token" or inputs["type"] == "multimodal":
@@ -264,7 +264,7 @@ def prompt_token_ids(self) -> List[int]:
264264
assert_never(inputs) # type: ignore[arg-type]
265265

266266
@cached_property
267-
def token_type_ids(self) -> List[int]:
267+
def token_type_ids(self) -> list[int]:
268268
inputs = self.inputs
269269

270270
if inputs["type"] == "token" or inputs["type"] == "multimodal":
@@ -294,7 +294,7 @@ def multi_modal_data(self) -> "MultiModalDataDict":
294294
assert_never(inputs) # type: ignore[arg-type]
295295

296296
@cached_property
297-
def multi_modal_inputs(self) -> Union[Dict, "MultiModalKwargs"]:
297+
def multi_modal_inputs(self) -> Union[dict, "MultiModalKwargs"]:
298298
inputs = self.inputs
299299

300300
if inputs["type"] == "token":
@@ -306,7 +306,7 @@ def multi_modal_inputs(self) -> Union[Dict, "MultiModalKwargs"]:
306306
assert_never(inputs) # type: ignore[arg-type]
307307

308308
@cached_property
309-
def multi_modal_hashes(self) -> List[str]:
309+
def multi_modal_hashes(self) -> list[str]:
310310
inputs = self.inputs
311311

312312
if inputs["type"] == "token":
@@ -331,7 +331,7 @@ def multi_modal_placeholders(self) -> "MultiModalPlaceholderDict":
331331
assert_never(inputs) # type: ignore[arg-type]
332332

333333
@cached_property
334-
def mm_processor_kwargs(self) -> Dict[str, Any]:
334+
def mm_processor_kwargs(self) -> dict[str, Any]:
335335
inputs = self.inputs
336336

337337
if inputs["type"] == "token":
@@ -355,7 +355,7 @@ def mm_processor_kwargs(self) -> Dict[str, Any]:
355355
def build_explicit_enc_dec_prompt(
356356
encoder_prompt: _T1,
357357
decoder_prompt: Optional[_T2],
358-
mm_processor_kwargs: Optional[Dict[str, Any]] = None,
358+
mm_processor_kwargs: Optional[dict[str, Any]] = None,
359359
) -> ExplicitEncoderDecoderPrompt[_T1, _T2]:
360360
if mm_processor_kwargs is None:
361361
mm_processor_kwargs = {}
@@ -368,9 +368,9 @@ def build_explicit_enc_dec_prompt(
368368
def zip_enc_dec_prompts(
369369
enc_prompts: Iterable[_T1],
370370
dec_prompts: Iterable[Optional[_T2]],
371-
mm_processor_kwargs: Optional[Union[Iterable[Dict[str, Any]],
372-
Dict[str, Any]]] = None,
373-
) -> List[ExplicitEncoderDecoderPrompt[_T1, _T2]]:
371+
mm_processor_kwargs: Optional[Union[Iterable[dict[str, Any]],
372+
dict[str, Any]]] = None,
373+
) -> list[ExplicitEncoderDecoderPrompt[_T1, _T2]]:
374374
"""
375375
Zip encoder and decoder prompts together into a list of
376376
:class:`ExplicitEncoderDecoderPrompt` instances.
@@ -380,12 +380,12 @@ def zip_enc_dec_prompts(
380380
provided, it will be zipped with the encoder/decoder prompts.
381381
"""
382382
if mm_processor_kwargs is None:
383-
mm_processor_kwargs = cast(Dict[str, Any], {})
383+
mm_processor_kwargs = cast(dict[str, Any], {})
384384
if isinstance(mm_processor_kwargs, dict):
385385
return [
386386
build_explicit_enc_dec_prompt(
387387
encoder_prompt, decoder_prompt,
388-
cast(Dict[str, Any], mm_processor_kwargs))
388+
cast(dict[str, Any], mm_processor_kwargs))
389389
for (encoder_prompt,
390390
decoder_prompt) in zip(enc_prompts, dec_prompts)
391391
]
@@ -399,7 +399,7 @@ def zip_enc_dec_prompts(
399399

400400
def to_enc_dec_tuple_list(
401401
enc_dec_prompts: Iterable[ExplicitEncoderDecoderPrompt[_T1, _T2]],
402-
) -> List[Tuple[_T1, Optional[_T2]]]:
402+
) -> list[tuple[_T1, Optional[_T2]]]:
403403
return [(enc_dec_prompt["encoder_prompt"],
404404
enc_dec_prompt["decoder_prompt"])
405405
for enc_dec_prompt in enc_dec_prompts]

vllm/inputs/parse.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3-
from typing import List, Literal, Sequence, TypedDict, Union, cast, overload
3+
from collections.abc import Sequence
4+
from typing import Literal, TypedDict, Union, cast, overload
45

56
from typing_extensions import TypeIs
67

@@ -17,24 +18,24 @@ class ParsedText(TypedDict):
1718

1819

1920
class ParsedTokens(TypedDict):
20-
content: List[int]
21+
content: list[int]
2122
is_tokens: Literal[True]
2223

2324

2425
@overload
2526
def parse_and_batch_prompt(
26-
prompt: Union[str, List[str]]) -> Sequence[ParsedText]:
27+
prompt: Union[str, list[str]]) -> Sequence[ParsedText]:
2728
...
2829

2930

3031
@overload
3132
def parse_and_batch_prompt(
32-
prompt: Union[List[int], List[List[int]]]) -> Sequence[ParsedTokens]:
33+
prompt: Union[list[int], list[list[int]]]) -> Sequence[ParsedTokens]:
3334
...
3435

3536

3637
def parse_and_batch_prompt(
37-
prompt: Union[str, List[str], List[int], List[List[int]]],
38+
prompt: Union[str, list[str], list[int], list[list[int]]],
3839
) -> Union[Sequence[ParsedText], Sequence[ParsedTokens]]:
3940
if isinstance(prompt, str):
4041
# case 1: a string
@@ -46,16 +47,16 @@ def parse_and_batch_prompt(
4647

4748
if is_list_of(prompt, str):
4849
# case 2: array of strings
49-
prompt = cast(List[str], prompt)
50+
prompt = cast(list[str], prompt)
5051
return [
5152
ParsedText(content=elem, is_tokens=False) for elem in prompt
5253
]
5354
if is_list_of(prompt, int):
5455
# case 3: array of tokens
55-
prompt = cast(List[int], prompt)
56+
prompt = cast(list[int], prompt)
5657
return [ParsedTokens(content=prompt, is_tokens=True)]
5758
if is_list_of(prompt, list):
58-
prompt = cast(List[List[int]], prompt)
59+
prompt = cast(list[list[int]], prompt)
5960
if len(prompt[0]) == 0:
6061
raise ValueError("please provide at least one prompt")
6162

vllm/inputs/preprocess.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# SPDX-License-Identifier: Apache-2.0
22

33
import asyncio
4-
from typing import List, Mapping, Optional, Tuple, Union, cast
4+
from collections.abc import Mapping
5+
from typing import Optional, Union, cast
56

67
from typing_extensions import assert_never
78

@@ -92,7 +93,7 @@ def get_decoder_start_token_id(self) -> Optional[int]:
9293

9394
return dec_start_token_id
9495

95-
def _get_default_enc_dec_decoder_prompt(self) -> List[int]:
96+
def _get_default_enc_dec_decoder_prompt(self) -> list[int]:
9697
'''
9798
Specifically for encoder/decoder models:
9899
generate a default decoder prompt for when
@@ -130,8 +131,8 @@ def _get_default_enc_dec_decoder_prompt(self) -> List[int]:
130131

131132
def _prepare_decoder_input_ids_for_generation(
132133
self,
133-
decoder_input_ids: Optional[List[int]],
134-
) -> List[int]:
134+
decoder_input_ids: Optional[list[int]],
135+
) -> list[int]:
135136
"""
136137
Prepares `decoder_input_ids` for generation with encoder-decoder models.
137138
@@ -168,9 +169,9 @@ def _prepare_decoder_input_ids_for_generation(
168169

169170
def _apply_prompt_adapter(
170171
self,
171-
prompt_token_ids: List[int],
172+
prompt_token_ids: list[int],
172173
prompt_adapter_request: Optional[PromptAdapterRequest],
173-
) -> List[int]:
174+
) -> list[int]:
174175
if prompt_adapter_request:
175176
prompt_token_ids = (
176177
[0] * prompt_adapter_request.prompt_adapter_num_virtual_tokens
@@ -183,7 +184,7 @@ def _tokenize_prompt(
183184
prompt: str,
184185
request_id: str,
185186
lora_request: Optional[LoRARequest],
186-
) -> List[int]:
187+
) -> list[int]:
187188
"""
188189
Apply the model's tokenizer to a text prompt, returning the
189190
corresponding token IDs.
@@ -211,7 +212,7 @@ async def _tokenize_prompt_async(
211212
prompt: str,
212213
request_id: str,
213214
lora_request: Optional[LoRARequest],
214-
) -> List[int]:
215+
) -> list[int]:
215216
"""Async version of :meth:`_tokenize_prompt`."""
216217
tokenizer = self.get_tokenizer_group()
217218
add_special_tokens = None
@@ -250,7 +251,7 @@ def _can_process_multimodal(self) -> bool:
250251

251252
def _process_multimodal(
252253
self,
253-
prompt: Union[str, List[int]],
254+
prompt: Union[str, list[int]],
254255
mm_data: MultiModalDataDict,
255256
mm_processor_kwargs: Optional[Mapping[str, object]],
256257
lora_request: Optional[LoRARequest],
@@ -280,7 +281,7 @@ def _process_multimodal(
280281

281282
async def _process_multimodal_async(
282283
self,
283-
prompt: Union[str, List[int]],
284+
prompt: Union[str, list[int]],
284285
mm_data: MultiModalDataDict,
285286
mm_processor_kwargs: Optional[Mapping[str, object]],
286287
lora_request: Optional[LoRARequest],
@@ -511,7 +512,7 @@ def _separate_enc_dec_inputs_from_mm_processor_outputs(
511512
self,
512513
inputs: SingletonInputs,
513514
decoder_inputs_to_override: Optional[SingletonInputs] = None,
514-
) -> Tuple[SingletonInputs, SingletonInputs]:
515+
) -> tuple[SingletonInputs, SingletonInputs]:
515516
"""
516517
For encoder/decoder models only:
517518
Separate Encoder/Decoder inputs from a MultiModalEncDecInputs

vllm/inputs/registry.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import functools
44
from collections import UserDict
5+
from collections.abc import Mapping
56
from dataclasses import dataclass
6-
from typing import (TYPE_CHECKING, Any, Callable, Mapping, NamedTuple,
7-
Optional, Protocol, Union)
7+
from typing import (TYPE_CHECKING, Any, Callable, NamedTuple, Optional,
8+
Protocol, Union)
89

910
from torch import nn
1011
from transformers import BatchFeature, PretrainedConfig, ProcessorMixin

0 commit comments

Comments
 (0)