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

[#38] code migration to use pydantic v2 #51

Open
wants to merge 36 commits into
base: develop
Choose a base branch
from

Conversation

pkdash
Copy link
Collaborator

@pkdash pkdash commented Aug 14, 2023

No description provided.

@pkdash pkdash linked an issue Sep 19, 2023 that may be closed by this pull request

import typer
from apscheduler.events import EVENT_JOB_ERROR, EVENT_JOB_EXECUTED
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to replace rocketry with apscheduler as rocketry has a dependency on pydantic. Currently rocketry is using pydantic v1.

@pkdash pkdash marked this pull request as ready for review November 1, 2023 03:24
funder.name = self.funding_agency_name
if self.funding_agency_url:
funder.url = self.funding_agency_url
funder.url = str(self.funding_agency_url)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the Organization.funder.url attribute be updated to an HttpUrl instead of casting to a string here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -24,13 +25,14 @@ def __init__(self, **data: Any) -> None:
super().__init__(**data)
if self.testing:
self.database_name = f"{self.database_name}"
self.hydroshare_meta_read_url = str(self.hydroshare_meta_read_url)
self.hydroshare_file_read_url = str(self.hydroshare_file_read_url)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, there must be something else going on with the HttpUrl->str conversions that I'm not understanding. Why are these lines in here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also was a big headche for me when doing this upgrade. See this pydantic discussion where it talks about the problem with pydantic HttpUrl type.
pydantic/pydantic#6395


@property
def db_connection_string(self):
return f"{self.db_protocol}://{self.db_username}:{self.db_password}@{self.db_host}/?retryWrites=true&w=majority"

class Config:
env_file = ".env"
model_config = SettingsConfigDict(env_file=".env", env_file_encoding="utf-8")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting that I have since learned that if we leave the env_file Config out then it just reads environment variables which eases deployments. This comment is not relevant to these changes, just making a note here.

# two possible types - one of which is null (due to the field being optional)
json_schema = handler(core_schema)
json_schema = handler.resolve_ref_schema(json_schema)
for field in ("url", "description"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These one off json schema modifiers look hairy. Any way we can consolidate these changes to the base class and make the changes for certain types of attributes?

@model_validator(mode='after')
def url_to_string(self):
if self.url is not None:
self.url = str(self.url)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm missing something on these httpurl to str conversions. Why is this change needed?

Copy link
Collaborator

@sblack-usu sblack-usu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It mostly looks good. I just have the question around the httpurl->str conversions and also would like to revisite the json schema modifications to see if we can make the changes more general by type instead of having custom implementations in each class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Upgrade to pydantic v2
2 participants