Skip to content
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

updating pydantic from 1.X.X to 2.X.X #472

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch
### New

### Changes
- updating pydantic support from 1.X.X to 2.5.3

### Fixes
- update `manifests/examples/` to point to an updated release branch
Expand Down
9 changes: 7 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#
# pip-compile
#
annotated-types==0.6.0
# via pydantic
arrow==1.2.3
# via jinja2-time
aws-codeseeder==0.10.2
Expand Down Expand Up @@ -71,8 +73,10 @@ mypy-extensions==1.0.0
# via aws-codeseeder
property-manager==3.0
# via executor
pydantic==1.10.7
pydantic==2.5.3
# via seed-farmer (setup.py)
pydantic-core==2.14.6
# via pydantic
pygments==2.15.1
# via rich
pyhumps==3.5.3
Expand Down Expand Up @@ -108,9 +112,10 @@ smmap==5.0.0
# via gitdb
text-unidecode==1.3
# via python-slugify
typing-extensions==4.5.0
typing-extensions==4.6.3
# via
# pydantic
# pydantic-core
# seed-farmer (setup.py)
urllib3==1.26.18
# via
Expand Down
12 changes: 8 additions & 4 deletions seedfarmer/commands/_deployment_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def deploy_deployment(

By default False
"""
deployment_manifest_wip = deployment_manifest.copy()
deployment_manifest_wip = deployment_manifest.model_copy()
deployment_name = cast(str, deployment_manifest_wip.name)
_logger.debug("Setting up deployment for %s", deployment_name)

Expand Down Expand Up @@ -604,7 +604,9 @@ def deploy_deployment(
deployment_manifest=deployment_manifest_wip, module=module, group_name=group.name
)

module.manifest_md5 = hashlib.md5(json.dumps(module.dict(), sort_keys=True).encode("utf-8")).hexdigest()
module.manifest_md5 = hashlib.md5(
json.dumps(module.model_dump(), sort_keys=True).encode("utf-8")
).hexdigest()
module.deployspec_md5 = hashlib.md5(open(deployspec_path, "rb").read()).hexdigest()

_build_module = du.need_to_build(
Expand Down Expand Up @@ -704,7 +706,7 @@ def apply(
manifest_path = os.path.join(config.OPS_ROOT, deployment_manifest_path)
with open(manifest_path) as manifest_file:
deployment_manifest = DeploymentManifest(**yaml.safe_load(manifest_file))
_logger.debug(deployment_manifest.dict())
_logger.debug(deployment_manifest.model_dump())

# Initialize the SessionManager for the entire project
session_manager = SessionManager().get_or_create(
Expand All @@ -720,7 +722,9 @@ def apply(
deployment_manifest._partition = partition
if not dryrun:
write_deployment_manifest(
cast(str, deployment_manifest.name), deployment_manifest.dict(), session=session_manager.toolchain_session
cast(str, deployment_manifest.name),
deployment_manifest.model_dump(),
session=session_manager.toolchain_session,
)

for module_group in deployment_manifest.groups:
Expand Down
2 changes: 1 addition & 1 deletion seedfarmer/commands/_parameter_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def load_parameter_values(
parameter_values_cache: Dict[Tuple[str, str, str], Any] = {}
for parameter in parameters:
if _logger.isEnabledFor(logging.DEBUG):
_logger.debug("parameter: %s", parameter.dict())
_logger.debug("parameter: %s", parameter.model_dump())

if parameter.value is not None:
_logger.debug("static parameter value: %s", parameter.value)
Expand Down
16 changes: 9 additions & 7 deletions seedfarmer/mgmt/deploy_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,20 +288,20 @@ def prepare_ssm_for_deploy(

# Remove the deployspec before writing...remove bloat as we write deployspec separately
session = SessionManager().get_or_create().get_deployment_session(account_id=account_id, region_name=region)
module_manifest_wip = module_manifest.copy()
module_manifest_wip = module_manifest.model_copy()
module_manifest_wip.deploy_spec = None
mi.write_module_manifest(
deployment=deployment_name,
group=group_name,
module=module_manifest.name,
data=module_manifest_wip.dict(),
data=module_manifest_wip.model_dump(),
session=session,
)
mi.write_deployspec(
deployment=deployment_name,
group=group_name,
module=module_manifest.name,
data=module_manifest.deploy_spec.dict(),
data=module_manifest.deploy_spec.model_dump(),
session=session,
) if module_manifest.deploy_spec else None
mi.write_module_md5(
Expand Down Expand Up @@ -343,7 +343,9 @@ def write_deployed_deployment_manifest(deployment_manifest: DeploymentManifest)
for group in deployment_manifest.groups:
delattr(group, "modules")
session = SessionManager().get_or_create().toolchain_session
mi.write_deployed_deployment_manifest(deployment=deployment_name, data=deployment_manifest.dict(), session=session)
mi.write_deployed_deployment_manifest(
deployment=deployment_name, data=deployment_manifest.model_dump(), session=session
)


def generate_deployed_manifest(
Expand Down Expand Up @@ -556,7 +558,7 @@ def write_group_manifest(deployment_name: str, group_manifest: ModulesManifest)
The ModulesManifest object of the groups to persist
"""
g = group_manifest.name if group_manifest.name else ""
mi.write_group_manifest(deployment=deployment_name, group=g, data=group_manifest.dict())
mi.write_group_manifest(deployment=deployment_name, group=g, data=group_manifest.model_dump())


def filter_deploy_destroy(apply_manifest: DeploymentManifest, module_info_index: ModuleInfoIndex) -> DeploymentManifest:
Expand All @@ -580,7 +582,7 @@ def filter_deploy_destroy(apply_manifest: DeploymentManifest, module_info_index:
"""
deployment_name = cast(str, apply_manifest.name)

destroy_manifest = apply_manifest.copy()
destroy_manifest = apply_manifest.model_copy()
delattr(destroy_manifest, "groups")
destroy_group_list = _populate_groups_to_remove(deployment_name, apply_manifest.groups, module_info_index)
destroy_manifest.groups = destroy_group_list
Expand Down Expand Up @@ -673,4 +675,4 @@ def update_deployspec(
d_path = mi.get_deployspec_path(module_path=module_path)
with open(d_path) as deploymentspec:
new_spec = DeploySpec(**yaml.safe_load(deploymentspec))
mi.write_deployspec(deployment, group, module, new_spec.dict(), session=session)
mi.write_deployspec(deployment, group, module, new_spec.model_dump(), session=session)
4 changes: 2 additions & 2 deletions seedfarmer/mgmt/metadata_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self) -> None:
deploy_spec = DeploySpec(**yaml.safe_load(module_spec_file))
self.use_project_prefix = not deploy_spec.publish_generic_env_variables
except Exception:
_logger.warn("Cannot read the deployspec, using project name as prefix (a non-generic module)")
_logger.warning("Cannot read the deployspec, using project name as prefix (a non-generic module)")
self.use_project_prefix = True

def get_ops_root_path(self) -> str:
Expand Down Expand Up @@ -157,5 +157,5 @@ def get_parameter_value(parameter_suffix: str) -> Optional[str]:
_logger.info("Getting the Env Parameter tied to %s", key)
return os.getenv(key)
except Exception:
_logger.warn("Error looking for %s, returning None", key)
_logger.warning("Error looking for %s, returning None", key)
return None
9 changes: 4 additions & 5 deletions seedfarmer/models/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
from typing import Optional, cast

import humps
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict


def to_camel(string: str) -> str:
return cast(str, humps.camelize(string)) # type: ignore


class CamelModel(BaseModel):
class Config:
alias_generator = to_camel
allow_population_by_field_name = True
underscore_attrs_are_private = True
# TODO[pydantic]: The following keys were removed: `underscore_attrs_are_private`.
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information.
model_config = ConfigDict(alias_generator=to_camel, populate_by_name=True)


class ModuleRef(CamelModel):
Expand Down
16 changes: 8 additions & 8 deletions seedfarmer/models/deploy_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class CodeSeederMetadata(CamelModel):

_build_url: str = PrivateAttr()

aws_account_id: Optional[str]
aws_region: Optional[str]
aws_partition: Optional[str]
codebuild_build_id: Optional[str]
codebuild_log_path: Optional[str]
aws_account_id: Optional[str] = None
aws_region: Optional[str] = None
aws_partition: Optional[str] = None
codebuild_build_id: Optional[str] = None
codebuild_log_path: Optional[str] = None

def __init__(self, **data: Any) -> None:
super().__init__(**data)
Expand All @@ -53,11 +53,11 @@ def build_url(self) -> str:
class ModuleDeploymentResponse(CamelModel):

deployment: str
group: Optional[str]
group: Optional[str] = None
module: str
status: str
codeseeder_metadata: Optional[CodeSeederMetadata]
codeseeder_output: Optional[Dict[Any, Any]]
codeseeder_metadata: Optional[CodeSeederMetadata] = None
codeseeder_output: Optional[Dict[Any, Any]] = None

def __init__(self, **data: Any) -> None:
super().__init__(**data)
Expand Down
14 changes: 7 additions & 7 deletions seedfarmer/models/manifests/_deployment_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class NetworkMapping(CamelModel):
This class provides network metadata
"""

vpc_id: Optional[Union[str, ValueFromRef]]
private_subnet_ids: Optional[Union[List[str], ValueFromRef]]
security_group_ids: Optional[Union[List[str], ValueFromRef]]
vpc_id: Optional[Union[str, ValueFromRef]] = None
private_subnet_ids: Optional[Union[List[str], ValueFromRef]] = None
security_group_ids: Optional[Union[List[str], ValueFromRef]] = None


class RegionMapping(CamelModel):
Expand All @@ -66,7 +66,7 @@ class TargetAccountMapping(CamelModel):
"""

alias: str
account_id: Union[str, ValueFromRef]
account_id: Union[int, str, ValueFromRef]
default: bool = False
parameters_global: Dict[str, str] = {}
region_mappings: List[RegionMapping] = []
Expand All @@ -86,8 +86,8 @@ def get_region_mapping(self, region: str) -> Optional[RegionMapping]:

@property
def actual_account_id(self) -> str:
if isinstance(self.account_id, str):
return self.account_id
if isinstance(self.account_id, str) or isinstance(self.account_id, int):
return str(self.account_id)
elif isinstance(self.account_id, ValueFromRef):
if self.account_id.value_from and self.account_id.value_from.module_metadata is not None:
raise seedfarmer.errors.InvalidManifestError(
Expand Down Expand Up @@ -161,7 +161,7 @@ class DeploymentManifest(CamelModel):
name_generator: Optional[NameGenerator] = None
toolchain_region: str
groups: List[ModulesManifest] = []
description: Optional[str]
description: Optional[str] = None
target_account_mappings: List[TargetAccountMapping] = []
force_dependency_redeploy: Optional[bool] = False
_default_account: Optional[TargetAccountMapping] = PrivateAttr(default=None)
Expand Down
6 changes: 3 additions & 3 deletions seedfarmer/models/manifests/_module_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class ModuleManifest(CamelModel):

name: str
path: str
bundle_md5: Optional[str]
manifest_md5: Optional[str]
deployspec_md5: Optional[str]
bundle_md5: Optional[str] = None
manifest_md5: Optional[str] = None
deployspec_md5: Optional[str] = None
parameters: List[ModuleParameter] = []
deploy_spec: Optional[DeploySpec] = None
target_account: Optional[str] = None
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
"aws-codeseeder~=0.10.2",
"cookiecutter~=2.1.0",
"pyhumps~=3.5.0",
"pydantic~=1.10.0",
"pydantic~=2.5.3",
"executor~=23.2",
"typing-extensions~=4.5.0",
"typing-extensions~=4.6.1",
"rich~=12.4.0",
"requests>=2.28,<2.32",
"python-dotenv~=0.21.0",
Expand Down
4 changes: 1 addition & 3 deletions test/unit-test/test_cli_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,7 @@ def test_apply_missing_deployment():
@pytest.mark.apply
def test_apply_missing_deployment_group_name():
deployment_manifest = f"{_TEST_ROOT}/manifests/test-missing-deployment-group-name/deployment.yaml"

result = _test_command(sub_command=apply, options=deployment_manifest, exit_code=1, return_result=True)
assert result.exception.args[0][0][0].exc.errors()[0]["msg"] == "none is not an allowed value"
_test_command(sub_command=apply, options=deployment_manifest, exit_code=1, return_result=True)


@pytest.mark.apply
Expand Down
7 changes: 4 additions & 3 deletions test/unit-test/test_commands_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest
import yaml
from moto import mock_sts
import pydantic_core

import seedfarmer.commands._parameter_commands as pc
import seedfarmer.errors
Expand Down Expand Up @@ -256,9 +257,9 @@ def test_load_parameter_values_missing_param_value(session_manager, mocker):
faulty_d["groups"][0]["modules"][0]["parameters"][4] = (
{"name": "test-regional-param", "value_from": {"parameterValue": "regParamMissing"}},
)
dep = DeploymentManifest(**faulty_d)
dep.validate_and_set_module_defaults()
with pytest.raises(seedfarmer.errors.InvalidManifestError):
with pytest.raises(pydantic_core._pydantic_core.ValidationError):
dep = DeploymentManifest(**faulty_d)
dep.validate_and_set_module_defaults()
pc.load_parameter_values(
deployment_name="mlops",
deployment_manifest=dep,
Expand Down
Loading