Skip to content

Commit 10c9066

Browse files
Copilotshaahji
andcommitted
Migrate from pydantic v1 to v2 native API
- Update pydantic_v1.py to import from pydantic v2 - Migrate ConfigBase to use ConfigDict - Migrate ConfigListBase and ConfigDictBase to use RootModel - Update NestedConfig validator to use model_validator - Update all method calls: dict()->model_dump(), parse_obj()->model_validate() - Update field access from __fields__ to model_fields - Add compatibility wrappers for root_validator and validator - Update pyproject.toml classmethod-decorators for v2 - All validators migrated by sub-agent (20 files) - All method calls migrated by sub-agent (50+ files) Co-authored-by: shaahji <96227573+shaahji@users.noreply.github.com>
1 parent 3f8fa0d commit 10c9066

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

olive/common/config_utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class NestedConfig(ConfigBase):
213213
@model_validator(mode="before")
214214
@classmethod
215215
def gather_nested_field(cls, values):
216-
all_fields = {name for name in cls.model_fields.keys()}
216+
all_fields = set(cls.model_fields.keys())
217217
for field in cls.model_fields.values():
218218
if field.alias:
219219
all_fields.add(field.alias)
@@ -324,15 +324,15 @@ def create_config_class(
324324
config = {}
325325
field_validators_dict = {}
326326
validators = validators.copy() if validators else {}
327-
327+
328328
for param, param_config in default_config.items():
329329
if param_config.category == ParamCategory.OBJECT:
330330
validator_name = f"validate_{param}_object"
331331
count = 0
332332
while validator_name in validators:
333333
validator_name = f"{validator_name}_{count}"
334334
count += 1
335-
335+
336336
def make_obj_validator(field_name):
337337
@field_validator(field_name)
338338
@classmethod
@@ -342,8 +342,9 @@ def check_obj(cls, v, info):
342342
if isinstance(v, str) and info.data.get("user_script") is None:
343343
raise ValueError(f"user_script must be provided if {field_name} is a name string")
344344
return v
345+
345346
return check_obj
346-
347+
347348
field_validators_dict[validator_name] = make_obj_validator(param)
348349

349350
type_ = param_config.type_

olive/common/pydantic_v1.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88
This module now uses pydantic v2 native APIs directly.
99
"""
1010

11-
# pylint: disable=redefined-builtin, wildcard-import, unused-wildcard-import
11+
# pylint: disable=redefined-builtin, wildcard-import, unused-wildcard-import, function-redefined
1212

13-
from pydantic import * # noqa: F403, F401
13+
from pydantic import * # noqa: F403
1414
from pydantic import ConfigDict, RootModel, field_validator, model_validator # noqa: F401
1515

1616

1717
# Compatibility wrapper for root_validator -> model_validator
1818
def root_validator(*_, pre=False, skip_on_failure=False, allow_reuse=False, **__):
1919
"""Compatibility wrapper for pydantic v1's root_validator using v2's model_validator."""
20-
mode = "before" if pre else "after"
21-
return model_validator(mode=mode)
20+
mode_val = "before" if pre else "after"
21+
return model_validator(mode=mode_val)
2222

2323

2424
# Compatibility wrapper for validator -> field_validator
2525
def validator(*field_names, pre=False, always=False, allow_reuse=False, **kwargs):
2626
"""Compatibility wrapper for pydantic v1's validator using v2's field_validator."""
27-
mode = "before" if pre else "after"
28-
return field_validator(*field_names, mode=mode)
27+
mode_val = "before" if pre else "after"
28+
return field_validator(*field_names, mode=mode_val)

pyproject.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,14 @@ ignore = [
180180
ban-relative-imports = "all"
181181

182182
[tool.ruff.lint.pep8-naming]
183-
# Allow Pydantic's `@validator` decorator to trigger class method treatment.
184-
classmethod-decorators = ["classmethod", "olive.common.pydantic_v1.validator", "olive.common.pydantic_v1.root_validator"]
183+
# Allow Pydantic's validators to trigger class method treatment.
184+
classmethod-decorators = [
185+
"classmethod",
186+
"olive.common.pydantic_v1.field_validator",
187+
"olive.common.pydantic_v1.model_validator",
188+
"pydantic.field_validator",
189+
"pydantic.model_validator"
190+
]
185191

186192
[tool.ruff.lint.per-file-ignores]
187193
".azure_pipelines/**" = ["INP001"]

0 commit comments

Comments
 (0)