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

Update Pydantic to version 2.4.2 #10

Merged
merged 3 commits into from
Nov 7, 2023

Conversation

paulopaixaoamaral
Copy link
Contributor

@paulopaixaoamaral paulopaixaoamaral commented Nov 6, 2023

Upgrades Pydantic to version 2.4.2 and updates the code accordingly.
Closes #9

@paulopaixaoamaral paulopaixaoamaral requested review from djm, hugorodgerbrown and a team November 6, 2023 16:49
@paulopaixaoamaral paulopaixaoamaral self-assigned this Nov 6, 2023
@paulopaixaoamaral paulopaixaoamaral linked an issue Nov 6, 2023 that may be closed by this pull request
Comment on lines +51 to +52
@model_validator(mode="after")
def validate_directives(self) -> ReportData:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using the model_validator with mode before would allow us to get the values as a dictionary, similar to what was happening with the deprecated root_validator. However, it seems like using before with the model_validator ignores the field's alias parameter, which means that if users are passing effective-directive, that's what we get on the values dictionary.

I'm not sure if this Pydantic change was intentional or not, since before validators are supposed to validate before any parsing is done on the fields, but it seems to me like we should be able to access the original field by the field's name, not its alias.

raise ValueError(
"Either 'effective_directive' or 'violated_directive' must be present."
)

class Config:
allow_population_by_field_name = True
model_config = ConfigDict(populate_by_name=True, coerce_numbers_to_str=True)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

coerce_numbers_to_str=True was added to allow status_code to be an integer or a string, which was the behaviour before this change.
This is because Pydantic 1.x.x would coerce numbers to strings by default, whereas Pydantic 2.x.x will not do this by default.

I'm not sure if we want to keep this behaviour or not though.

Copy link
Collaborator

@hugorodgerbrown hugorodgerbrown left a comment

Choose a reason for hiding this comment

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

I've added a suggestion on the tox support (which may have implications for GH actions).

tox.ini Outdated Show resolved Hide resolved
Copy link
Collaborator

@hugorodgerbrown hugorodgerbrown left a comment

Choose a reason for hiding this comment

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

LGTM - although I'm largely commenting on the version upgrade itself, not on the changes made to decorators.

@paulopaixaoamaral paulopaixaoamaral merged commit e2d3215 into master Nov 7, 2023
12 checks passed
@paulopaixaoamaral paulopaixaoamaral deleted the issues/upgrade-pydantic-to-v2 branch November 7, 2023 10:59
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 Pydantic version to V2
2 participants