Improve literal dataclass field conversion to argparse argument (#17391)
Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
@@ -11,7 +11,8 @@ import pytest
|
||||
from vllm.config import PoolerConfig, config
|
||||
from vllm.engine.arg_utils import (EngineArgs, contains_type, get_kwargs,
|
||||
get_type, is_not_builtin, is_type,
|
||||
nullable_kvs, optional_type)
|
||||
literal_to_kwargs, nullable_kvs,
|
||||
optional_type)
|
||||
from vllm.utils import FlexibleArgumentParser
|
||||
|
||||
|
||||
@@ -71,6 +72,21 @@ def test_get_type(type_hints, type, expected):
|
||||
assert get_type(type_hints, type) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("type_hints", "expected"), [
|
||||
({Literal[1, 2]}, {
|
||||
"type": int,
|
||||
"choices": [1, 2]
|
||||
}),
|
||||
({Literal[1, "a"]}, Exception),
|
||||
])
|
||||
def test_literal_to_kwargs(type_hints, expected):
|
||||
context = nullcontext()
|
||||
if expected is Exception:
|
||||
context = pytest.raises(expected)
|
||||
with context:
|
||||
assert literal_to_kwargs(type_hints) == expected
|
||||
|
||||
|
||||
@config
|
||||
@dataclass
|
||||
class DummyConfigClass:
|
||||
@@ -81,11 +97,15 @@ class DummyConfigClass:
|
||||
optional_literal: Optional[Literal["x", "y"]] = None
|
||||
"""Optional literal with default None"""
|
||||
tuple_n: tuple[int, ...] = field(default_factory=lambda: (1, 2, 3))
|
||||
"""Tuple with default (1, 2, 3)"""
|
||||
"""Tuple with variable length"""
|
||||
tuple_2: tuple[int, int] = field(default_factory=lambda: (1, 2))
|
||||
"""Tuple with default (1, 2)"""
|
||||
"""Tuple with fixed length"""
|
||||
list_n: list[int] = field(default_factory=lambda: [1, 2, 3])
|
||||
"""List with default [1, 2, 3]"""
|
||||
"""List with variable length"""
|
||||
list_literal: list[Literal[1, 2]] = field(default_factory=list)
|
||||
"""List with literal choices"""
|
||||
literal_literal: Literal[Literal[1], Literal[2]] = 1
|
||||
"""Literal of literals with default 1"""
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("type_hint", "expected"), [
|
||||
@@ -111,6 +131,12 @@ def test_get_kwargs():
|
||||
# lists should work
|
||||
assert kwargs["list_n"]["type"] is int
|
||||
assert kwargs["list_n"]["nargs"] == "+"
|
||||
# lists with literals should have the correct choices
|
||||
assert kwargs["list_literal"]["type"] is int
|
||||
assert kwargs["list_literal"]["nargs"] == "+"
|
||||
assert kwargs["list_literal"]["choices"] == [1, 2]
|
||||
# literals of literals should have merged choices
|
||||
assert kwargs["literal_literal"]["choices"] == [1, 2]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("arg", "expected"), [
|
||||
|
||||
+34
-1
@@ -1,14 +1,47 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from dataclasses import MISSING, Field, asdict, dataclass, field
|
||||
from typing import Literal, Union
|
||||
|
||||
import pytest
|
||||
|
||||
from vllm.config import ModelConfig, PoolerConfig, get_field
|
||||
from vllm.config import ModelConfig, PoolerConfig, config, get_field
|
||||
from vllm.model_executor.layers.pooler import PoolingType
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
|
||||
class TestConfig1:
|
||||
pass
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestConfig2:
|
||||
a: int
|
||||
"""docstring"""
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestConfig3:
|
||||
a: int = 1
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestConfig4:
|
||||
a: Union[Literal[1], Literal[2]] = 1
|
||||
"""docstring"""
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("test_config", "expected_error"), [
|
||||
(TestConfig1, "must be a dataclass"),
|
||||
(TestConfig2, "must have a default"),
|
||||
(TestConfig3, "must have a docstring"),
|
||||
(TestConfig4, "must use a single Literal"),
|
||||
])
|
||||
def test_config(test_config, expected_error):
|
||||
with pytest.raises(Exception, match=expected_error):
|
||||
config(test_config)
|
||||
|
||||
|
||||
def test_get_field():
|
||||
|
||||
@dataclass
|
||||
|
||||
Reference in New Issue
Block a user