-
Notifications
You must be signed in to change notification settings - Fork 32
🎨 pydantic2 migration: fixed unit-tests for dynamic-sidecar #6534
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
Merged
GitHK
merged 26 commits into
ITISFoundation:pydantic_v2_migration
from
GitHK:pr-osparc-pydantic-v2-dynamic-sidecar2
Oct 18, 2024
Merged
Changes from 5 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
9c2f910
bumped pyndatic
df65a49
applied model conversion
61cc89c
fixed dynamic-sidecar
7a40c71
mypy
997ddf9
pylint
e44a381
using ByteSizeAdapter
4bb8712
rename file
c23c39a
rename function
b81f41a
one style
f60b2cf
refactor with common Adapter pattern
953d51d
renaming
4ef64b7
Merge remote-tracking branch 'upstream/pydantic_v2_migration' into pr…
9aa5beb
replace with typeadapter pattern
8cc3b78
broken ported values
e964d85
fixed broken port
7e8eb67
fixed mypy
5fea107
refactor to common validators
8da52aa
revert accidental port
7b5e3ea
renamed file
0f923ae
revert and use local implementation
aee1ce7
fixed warning
08fa3b2
fixed issue with RunID
7a41cd0
added timedelta validator in everywhere it is required
1147997
using suggested TypeAdapter pattern
698e1e4
revert all other typeadapter patters
02f4d22
fixed deprecation warnings
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
packages/common-library/src/common_library/pydantic_settings_validators.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import datetime | ||
|
|
||
| from pydantic import field_validator | ||
|
|
||
|
|
||
| def _get_float_string_as_seconds( | ||
GitHK marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| v: datetime.timedelta | str | float, | ||
| ) -> datetime.timedelta | float | str: | ||
| if isinstance(v, str): | ||
| try: | ||
| return float(v) | ||
| except ValueError: | ||
| # returns format like "1:00:00" | ||
| return v | ||
| return v | ||
|
|
||
|
|
||
| def validate_timedelta_in_legacy_mode(field: str): | ||
| """Transforms a float/int number into a valid datetime as it used to work in the past""" | ||
| return field_validator(field, mode="before")(_get_float_string_as_seconds) | ||
8 changes: 6 additions & 2 deletions
8
packages/common-library/src/common_library/pydantic_type_adapters.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,12 @@ | ||
| from typing import Final | ||
|
|
||
| from common_library.pydantic_networks_extension import AnyHttpUrlLegacy, AnyUrlLegacy | ||
| from pydantic import TypeAdapter | ||
| from pydantic import ByteSize, TypeAdapter | ||
|
|
||
| AnyUrlLegacyAdapter: Final[TypeAdapter[AnyUrlLegacy]] = TypeAdapter(AnyUrlLegacy) | ||
|
|
||
| AnyHttpUrlLegacyAdapter: Final[TypeAdapter[AnyHttpUrlLegacy]] = TypeAdapter(AnyHttpUrlLegacy) | ||
| AnyHttpUrlLegacyAdapter: Final[TypeAdapter[AnyHttpUrlLegacy]] = TypeAdapter( | ||
GitHK marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| AnyHttpUrlLegacy | ||
| ) | ||
|
|
||
| ByteSizeAdapter = TypeAdapter(ByteSize) | ||
42 changes: 42 additions & 0 deletions
42
packages/common-library/tests/test_pydantic_settings_validators.py
GitHK marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| from datetime import timedelta | ||
|
|
||
| import pytest | ||
| from common_library.pydantic_settings_validators import ( | ||
| validate_timedelta_in_legacy_mode, | ||
| ) | ||
| from faker import Faker | ||
| from pydantic import Field | ||
| from pydantic_settings import BaseSettings, SettingsConfigDict | ||
| from pytest_simcore.helpers.monkeypatch_envs import setenvs_from_dict | ||
|
|
||
|
|
||
| def test_validate_timedelta_in_legacy_mode( | ||
| monkeypatch: pytest.MonkeyPatch, faker: Faker | ||
| ): | ||
| class Settings(BaseSettings): | ||
| APP_NAME: str | ||
| REQUEST_TIMEOUT: timedelta = Field(default=timedelta(seconds=40)) | ||
|
|
||
| _legacy_parsing_request_timeout = validate_timedelta_in_legacy_mode( | ||
| "REQUEST_TIMEOUT" | ||
| ) | ||
|
|
||
| model_config = SettingsConfigDict() | ||
|
|
||
| app_name = faker.pystr() | ||
| env_vars: dict[str, str | bool] = {"APP_NAME": app_name} | ||
|
|
||
| # without timedelta | ||
| setenvs_from_dict(monkeypatch, env_vars) | ||
| settings = Settings() | ||
| print(settings.model_dump()) | ||
| assert app_name == settings.APP_NAME | ||
| assert timedelta(seconds=40) == settings.REQUEST_TIMEOUT | ||
|
|
||
| # with timedelta in seconds | ||
| env_vars["REQUEST_TIMEOUT"] = "5555" | ||
| setenvs_from_dict(monkeypatch, env_vars) | ||
| settings = Settings() | ||
| print(settings.model_dump()) | ||
| assert app_name == settings.APP_NAME | ||
| assert timedelta(seconds=5555) == settings.REQUEST_TIMEOUT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,14 @@ | ||
| from typing import Any | ||
|
|
||
| from pydantic import BaseModel, ConfigDict | ||
| from pydantic import BaseModel, ConfigDict, TypeAdapter | ||
|
|
||
| from .services import ServiceKey, ServiceVersion | ||
| from .services_resources import ServiceResourcesDict | ||
| from .services_types import ( | ||
| ServiceKey, | ||
| ServiceKeyTypeAdapter, | ||
| ServiceVersion, | ||
| ServiceVersionTypeAdapter, | ||
| ) | ||
| from .wallets import WalletID | ||
|
|
||
|
|
||
|
|
@@ -36,10 +41,18 @@ class CreateServiceMetricsAdditionalParams(BaseModel): | |
| "user_email": "[email protected]", | ||
| "project_name": "_!New Study", | ||
| "node_name": "the service of a lifetime _ *!", | ||
| "service_key": ServiceKey("simcore/services/dynamic/test"), | ||
| "service_version": ServiceVersion("0.0.1"), | ||
| "service_key": ServiceKeyTypeAdapter.validate_python( | ||
| "simcore/services/dynamic/test" | ||
| ), | ||
| "service_version": ServiceVersionTypeAdapter.validate_python("0.0.1"), | ||
| "service_resources": {}, | ||
| "service_additional_metadata": {}, | ||
| "pricing_unit_cost_id": None, | ||
| } | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| CreateServiceMetricsAdditionalParamsTypeAdapter = TypeAdapter( | ||
GitHK marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
GitHK marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CreateServiceMetricsAdditionalParams | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.