Skip to content

Commit 82c7812

Browse files
run bump-pydantic
1 parent 6cbe7d9 commit 82c7812

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

services/invitations/src/simcore_service_invitations/core/settings.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
from functools import cached_property
22

33
from models_library.products import ProductName
4-
from pydantic import Field, HttpUrl, PositiveInt, SecretStr, validator
4+
from pydantic import (
5+
AliasChoices,
6+
Field,
7+
HttpUrl,
8+
PositiveInt,
9+
SecretStr,
10+
field_validator,
11+
)
512
from settings_library.base import BaseCustomSettings
613
from settings_library.basic_types import BuildTargetEnum, LogLevel, VersionTag
714
from settings_library.utils_logging import MixinLoggingSettings
@@ -38,22 +45,23 @@ class _BaseApplicationSettings(BaseCustomSettings, MixinLoggingSettings):
3845
# RUNTIME -----------------------------------------------------------
3946

4047
INVITATIONS_LOGLEVEL: LogLevel = Field(
41-
default=LogLevel.INFO, env=["INVITATIONS_LOGLEVEL", "LOG_LEVEL", "LOGLEVEL"]
48+
default=LogLevel.INFO,
49+
validation_alias=AliasChoices("INVITATIONS_LOGLEVEL", "LOG_LEVEL", "LOGLEVEL"),
4250
)
4351
INVITATIONS_LOG_FORMAT_LOCAL_DEV_ENABLED: bool = Field(
4452
default=False,
45-
env=[
53+
validation_alias=AliasChoices(
4654
"INVITATIONS_LOG_FORMAT_LOCAL_DEV_ENABLED",
4755
"LOG_FORMAT_LOCAL_DEV_ENABLED",
48-
],
56+
),
4957
description="Enables local development log format. WARNING: make sure it is disabled if you want to have structured logs!",
5058
)
5159

5260
@cached_property
5361
def LOG_LEVEL(self):
5462
return self.INVITATIONS_LOGLEVEL
5563

56-
@validator("INVITATIONS_LOGLEVEL")
64+
@field_validator("INVITATIONS_LOGLEVEL")
5765
@classmethod
5866
def valid_log_level(cls, value: str) -> str:
5967
return cls.validate_log_level(value)

services/invitations/src/simcore_service_invitations/services/invitations.py

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import base64
22
import binascii
33
import logging
4-
from typing import Any, ClassVar, cast
54
from urllib import parse
65

76
from cryptography.fernet import Fernet, InvalidToken
87
from models_library.invitations import InvitationContent, InvitationInputs
98
from models_library.products import ProductName
10-
from pydantic import HttpUrl, ValidationError, parse_obj_as
9+
from pydantic import ConfigDict, HttpUrl, TypeAdapter, ValidationError
1110
from starlette.datastructures import URL
1211

1312
_logger = logging.getLogger(__name__)
1413

1514

15+
def _to_initial(v: str):
16+
return v[0]
17+
18+
1619
class InvalidInvitationCodeError(Exception):
1720
...
1821

@@ -25,7 +28,7 @@ def serialize(cls, model_obj: InvitationContent) -> str:
2528
"""Exports to json using *short* aliases and values in order to produce shorter codes"""
2629
model_w_short_aliases_json: str = cls.construct(
2730
**model_obj.dict(exclude_unset=True)
28-
).json(exclude_unset=True, by_alias=True)
31+
).model_dump_json(exclude_unset=True, by_alias=True)
2932
# NOTE: json arguments try to minimize the amount of data
3033
# serialized. The CONS is that it relies on models in the code
3134
# that might change over time. This might lead to some datasets in codes
@@ -40,31 +43,13 @@ def deserialize(cls, raw_json: str) -> InvitationContent:
4043
**model_w_short_aliases.dict(exclude_unset=True)
4144
)
4245

43-
class Config:
44-
allow_population_by_field_name = True # NOTE: can parse using field names
45-
allow_mutation = False
46-
anystr_strip_whitespace = True
46+
model_config = ConfigDict(
4747
# NOTE: Can export with alias: short aliases to minimize the size of serialization artifact
48-
fields: ClassVar[dict[str, Any]] = {
49-
"issuer": {
50-
"alias": "i",
51-
},
52-
"guest": {
53-
"alias": "g",
54-
},
55-
"trial_account_days": {
56-
"alias": "t",
57-
},
58-
"extra_credits_in_usd": {
59-
"alias": "e",
60-
},
61-
"product": {
62-
"alias": "p",
63-
},
64-
"created": {
65-
"alias": "c",
66-
},
67-
}
48+
alias_generator=_to_initial,
49+
populate_by_name=True, # NOTE: can parse using field names
50+
frozen=True,
51+
str_strip_whitespace=True,
52+
)
6853

6954

7055
#
@@ -81,7 +66,7 @@ def _build_link(
8166
# Adds query to fragment
8267
base_url = f"{base_url.rstrip('/')}/"
8368
url = URL(base_url).replace(fragment=f"{r}")
84-
return cast(HttpUrl, parse_obj_as(HttpUrl, f"{url}"))
69+
return TypeAdapter(HttpUrl).validate_python(f"{url}")
8570

8671

8772
def _fernet_encrypt_as_urlsafe_code(

0 commit comments

Comments
 (0)