-
Notifications
You must be signed in to change notification settings - Fork 3.3k
chore(python): remove pydantic V1 deprecation warnings #15057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
6d0d821
e48ee8a
02d206f
2152966
aa3dbe7
0a114f9
7738015
3495079
97fedd7
6efc087
8e7ed0a
cf60287
6ed6b66
4a9e62b
4cad1c1
b50751e
61fb2f1
b21864c
c7f1928
833b040
a64c238
74bd189
c23294d
6129203
5cd59c6
80065bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| from pathlib import Path | ||
| from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Union | ||
|
|
||
| import pydantic | ||
| from pydantic import field_validator, model_validator | ||
| from ruamel.yaml import YAML | ||
| from typing_extensions import assert_never | ||
|
|
||
|
|
@@ -71,7 +71,8 @@ class Ownership(ConfigModel): | |
| id: str | ||
| type: str | ||
|
|
||
| @pydantic.validator("type") | ||
| @field_validator("type") | ||
| @classmethod | ||
| def ownership_type_must_be_mappable_or_custom(cls, v: str) -> str: | ||
| _, _ = builder.validate_ownership_type(v) | ||
| return v | ||
|
|
@@ -116,30 +117,49 @@ class DataProduct(ConfigModel): | |
| output_ports: Optional[List[str]] = None | ||
| _original_yaml_dict: Optional[dict] = None | ||
|
|
||
| @pydantic.validator("assets", each_item=True) | ||
| def assets_must_be_urns(cls, v: str) -> str: | ||
| try: | ||
| Urn.from_string(v) | ||
| except Exception as e: | ||
| raise ValueError(f"asset {v} is not an urn: {e}") from e | ||
|
|
||
| return v | ||
|
|
||
| @pydantic.validator("output_ports", each_item=True) | ||
| def output_ports_must_be_urns(cls, v: str) -> str: | ||
| try: | ||
| Urn.create_from_string(v) | ||
| except Exception as e: | ||
| raise ValueError(f"Output port {v} is not an urn: {e}") from e | ||
| @field_validator("assets") | ||
| @classmethod | ||
| def assets_must_be_urns(cls, v): | ||
|
||
| if isinstance(v, list): | ||
| for item in v: | ||
| try: | ||
| Urn.from_string(item) | ||
| except Exception as e: | ||
| raise ValueError(f"asset {item} is not an urn: {e}") from e | ||
| return v | ||
| else: | ||
| try: | ||
| Urn.from_string(v) | ||
| except Exception as e: | ||
| raise ValueError(f"asset {v} is not an urn: {e}") from e | ||
| return v | ||
|
|
||
| @field_validator("output_ports") | ||
|
||
| @classmethod | ||
| def output_ports_must_be_urns(cls, v): | ||
| if v is not None: | ||
| if isinstance(v, list): | ||
| for item in v: | ||
| try: | ||
| Urn.create_from_string(item) | ||
| except Exception as e: | ||
| raise ValueError( | ||
| f"Output port {item} is not an urn: {e}" | ||
| ) from e | ||
| else: | ||
| try: | ||
| Urn.create_from_string(v) | ||
| except Exception as e: | ||
| raise ValueError(f"Output port {v} is not an urn: {e}") from e | ||
| return v | ||
|
|
||
| @pydantic.validator("output_ports", each_item=True) | ||
| def output_ports_must_be_from_asset_list(cls, v: str, values: dict) -> str: | ||
| assets = values.get("assets", []) | ||
| if v not in assets: | ||
| raise ValueError(f"Output port {v} is not in asset list") | ||
| return v | ||
| @model_validator(mode="after") | ||
| def output_ports_must_be_from_asset_list(self): | ||
askumar27 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if self.output_ports and self.assets: | ||
| for port in self.output_ports: | ||
| if port not in self.assets: | ||
| raise ValueError(f"Output port {port} is not in asset list") | ||
| return self | ||
|
|
||
| @property | ||
| def urn(self) -> str: | ||
|
|
@@ -454,7 +474,7 @@ def _patch_ownership( | |
| patches_add.append(new_owner) | ||
| else: | ||
| patches_add.append( | ||
| Ownership(id=new_owner, type=new_owner_type).dict() | ||
| Ownership(id=new_owner, type=new_owner_type).model_dump() | ||
| ) | ||
|
|
||
| mutation_needed = bool(patches_replace or patches_drop or patches_add) | ||
|
|
@@ -485,8 +505,8 @@ def patch_yaml( | |
| raise Exception("Original Data Product was not loaded from yaml") | ||
|
|
||
| orig_dictionary = original_dataproduct._original_yaml_dict | ||
| original_dataproduct_dict = original_dataproduct.dict() | ||
| this_dataproduct_dict = self.dict() | ||
| original_dataproduct_dict = original_dataproduct.model_dump() | ||
| this_dataproduct_dict = self.model_dump() | ||
| for simple_field in ["display_name", "description", "external_url"]: | ||
| if original_dataproduct_dict.get(simple_field) != this_dataproduct_dict.get( | ||
| simple_field | ||
|
|
@@ -566,7 +586,7 @@ def to_yaml( | |
| yaml = YAML(typ="rt") # default, if not specfied, is 'rt' (round-trip) | ||
| yaml.indent(mapping=2, sequence=4, offset=2) | ||
| yaml.default_flow_style = False | ||
| yaml.dump(self.dict(), fp) | ||
| yaml.dump(self.model_dump(), fp) | ||
|
|
||
| @staticmethod | ||
| def get_patch_builder( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
always=True->Field(validate_default=True)as per the migration doc https://docs.pydantic.dev/latest/migration/#changes-to-validatorsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Model validators (mode="after") inherently work with defaults