Skip to content

Commit dad3c3d

Browse files
authored
feat: Upgrade pydantic to v2 (#750)
1. Upgrade pydantic to 2.x 2. Remove results/ from .gitignore, those files need to track 3. fix the coding styles in the results Signed-off-by: yangxuan <xuan.yang@zilliz.com>
1 parent 51c5158 commit dad3c3d

File tree

31 files changed

+213
-185
lines changed

31 files changed

+213
-185
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ build/
1010
venv/
1111
.venv/
1212
.idea/
13-
results/
1413
logs/
1514

1615
# Worktrees

install/requirements_py3.11.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ psutil
2020
polars
2121
plotly
2222
environs
23-
pydantic<v2
23+
pydantic>=2.0,<3
2424
scikit-learn
2525
pymilvus
2626
clickhouse_connect

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dependencies = [
3737
"polars",
3838
"plotly",
3939
"environs",
40-
"pydantic<v2",
40+
"pydantic>=2.0,<3",
4141
"scikit-learn",
4242
"pymilvus", # with pandas, numpy
4343
"hdrhistogram>=0.10.1",

vectordb_bench/backend/clients/alisql/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ def parse_metric(self) -> str:
4949

5050

5151
class AliSQLHNSWConfig(AliSQLIndexConfig, DBCaseConfig):
52-
M: int | None
53-
ef_search: int | None
52+
M: int | None = None
53+
ef_search: int | None = None
5454
index: IndexType = IndexType.HNSW
5555

5656
def index_param(self) -> dict:

vectordb_bench/backend/clients/alloydb/config.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class AlloyDBIndexParam(TypedDict):
4343
metric: str
4444
index_type: str
4545
index_creation_with_options: Sequence[dict[str, Any]]
46-
maintenance_work_mem: str | None
47-
max_parallel_workers: int | None
46+
maintenance_work_mem: str | None = None
47+
max_parallel_workers: int | None = None
4848

4949

5050
class AlloyDBSearchParam(TypedDict):
@@ -120,15 +120,15 @@ def _optionally_build_set_options(
120120

121121
class AlloyDBScaNNConfig(AlloyDBIndexConfig):
122122
index: IndexType = IndexType.SCANN
123-
num_leaves: int | None
124-
quantizer: str | None
125-
enable_pca: str | None
126-
max_num_levels: int | None
127-
num_leaves_to_search: int | None
128-
max_top_neighbors_buffer_size: int | None
129-
pre_reordering_num_neighbors: int | None
130-
num_search_threads: int | None
131-
max_num_prefetch_datasets: int | None
123+
num_leaves: int | None = None
124+
quantizer: str | None = None
125+
enable_pca: str | None = None
126+
max_num_levels: int | None = None
127+
num_leaves_to_search: int | None = None
128+
max_top_neighbors_buffer_size: int | None = None
129+
pre_reordering_num_neighbors: int | None = None
130+
num_search_threads: int | None = None
131+
max_num_prefetch_datasets: int | None = None
132132
maintenance_work_mem: str | None = None
133133
max_parallel_workers: int | None = None
134134

vectordb_bench/backend/clients/api.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from contextlib import contextmanager
33
from enum import StrEnum
44

5-
from pydantic import BaseModel, SecretStr, validator
5+
from pydantic import BaseModel, model_validator
66

77
from vectordb_bench.backend.filter import Filter, FilterOp
88

@@ -90,13 +90,18 @@ def common_long_configs() -> list[str]:
9090
def to_dict(self) -> dict:
9191
raise NotImplementedError
9292

93-
@validator("*")
94-
def not_empty_field(cls, v: any, field: any):
95-
if field.name in cls.common_short_configs() or field.name in cls.common_long_configs():
96-
return v
97-
if not v and isinstance(v, str | SecretStr):
98-
raise ValueError("Empty string!")
99-
return v
93+
@model_validator(mode="before")
94+
@classmethod
95+
def not_empty_field(cls, data: any) -> any:
96+
if not isinstance(data, dict):
97+
return data
98+
skip = set(cls.common_short_configs()) | set(cls.common_long_configs())
99+
for field_name, v in data.items():
100+
if field_name in skip:
101+
continue
102+
if isinstance(v, str) and not v:
103+
raise ValueError("Empty string!")
104+
return data
100105

101106

102107
class DBCaseConfig(ABC):

vectordb_bench/backend/clients/aws_opensearch/config.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from enum import Enum
33

4-
from pydantic import BaseModel, SecretStr, validator
4+
from pydantic import BaseModel, SecretStr, model_validator
55

66
from ..api import DBCaseConfig, DBConfig, MetricType
77

@@ -32,17 +32,18 @@ def to_dict(self) -> dict:
3232
"timeout": 600,
3333
}
3434

35-
@validator("*")
36-
def not_empty_field(cls, v: any, field: any):
37-
if (
38-
field.name in cls.common_short_configs()
39-
or field.name in cls.common_long_configs()
40-
or field.name in ["user", "password", "host"]
41-
):
42-
return v
43-
if isinstance(v, str | SecretStr) and len(v) == 0:
44-
raise ValueError("Empty string!")
45-
return v
35+
@model_validator(mode="before")
36+
@classmethod
37+
def not_empty_field(cls, data: any) -> any:
38+
if not isinstance(data, dict):
39+
return data
40+
skip = set(cls.common_short_configs()) | set(cls.common_long_configs()) | {"user", "password", "host"}
41+
for field_name, v in data.items():
42+
if field_name in skip:
43+
continue
44+
if isinstance(v, str) and not v:
45+
raise ValueError("Empty string!")
46+
return data
4647

4748

4849
class AWSOS_Engine(Enum):

vectordb_bench/backend/clients/chroma/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class ChromaConfig(DBConfig):
88
user: str | None = None
9-
password: SecretStr | None
9+
password: SecretStr | None = None
1010
host: SecretStr = "localhost"
1111
port: int = 8000
1212

vectordb_bench/backend/clients/cockroachdb/config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ class CockroachDBIndexParam(TypedDict):
7575

7676
metric: str
7777
index_creation_with_options: Sequence[dict[str, Any]]
78-
min_partition_size: int | None
79-
max_partition_size: int | None
80-
build_beam_size: int | None
78+
min_partition_size: int | None = None
79+
max_partition_size: int | None = None
80+
build_beam_size: int | None = None
8181

8282

8383
class CockroachDBSearchParam(TypedDict):
8484
"""Search parameters for CockroachDB vector queries."""
8585

8686
metric_fun_op: LiteralString
87-
vector_search_beam_size: int | None
87+
vector_search_beam_size: int | None = None
8888

8989

9090
class CockroachDBSessionCommands(TypedDict):

vectordb_bench/backend/clients/doris/config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22

3-
from pydantic import BaseModel, SecretStr, validator
3+
from pydantic import BaseModel, SecretStr, model_validator
44

55
from ..api import DBCaseConfig, DBConfig, MetricType
66

@@ -17,9 +17,10 @@ class DorisConfig(DBConfig):
1717
db_name: str = "test"
1818
ssl: bool = False
1919

20-
@validator("*")
21-
def not_empty_field(cls, v: any, field: any):
22-
return v
20+
@model_validator(mode="before")
21+
@classmethod
22+
def not_empty_field(cls, data: any) -> any:
23+
return data
2324

2425
def to_dict(self) -> dict:
2526
pwd_str = self.password.get_secret_value()

0 commit comments

Comments
 (0)