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

Pydantic: add helper, generalize forbid extra code #169

Merged
merged 8 commits into from
Sep 16, 2024

Conversation

felixfontein
Copy link
Collaborator

No description provided.

@felixfontein
Copy link
Collaborator Author

Hmm, Pydantic does funky stuff with Python 3.9 and 3.10:

>>> issubclass(dict, p.BaseModel)
False
>>> issubclass(int, p.BaseModel)
False
>>> issubclass(dict[int, int], p.BaseModel)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/abc.py", line 123, in __subclasscheck__
    return _abc_subclasscheck(cls, subclass)
TypeError: issubclass() arg 1 must be a class

(Not that I'm surprised it does funky stuff...)

src/antsibull_core/pydantic.py Outdated Show resolved Hide resolved
src/antsibull_core/pydantic.py Outdated Show resolved Hide resolved
_modify_config(models, processed_classes, change_config)


def forbid_extras(models: type[p.BaseModel] | Collection[type[p.BaseModel]]) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looking at this, I feel there has to be a better way. Maybe file an RFE with pydantic asking for a way to validate a model with extra="forbid" without having to change the model's configuration? Maybe an extra kwarg in the model_validate() method to configure this? In the meantime, I think this is workable, just not super pretty.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There's already pydantic/pydantic#2652, to which I added a comment.

src/antsibull_core/pydantic.py Outdated Show resolved Hide resolved
src/antsibull_core/pydantic.py Outdated Show resolved Hide resolved
felixfontein added a commit to felixfontein/antsibull-docs that referenced this pull request Sep 15, 2024
felixfontein added a commit to felixfontein/antsibull-docs that referenced this pull request Sep 15, 2024
felixfontein added a commit to felixfontein/antsibull-docs that referenced this pull request Sep 15, 2024
felixfontein added a commit to felixfontein/antsibull-docs that referenced this pull request Sep 15, 2024
Copy link
Collaborator

@gotmax23 gotmax23 left a comment

Choose a reason for hiding this comment

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

Looks like all the feedback was addressed. Thanks for this!

@felixfontein felixfontein merged commit c59cc9f into ansible-community:main Sep 16, 2024
5 checks passed
@felixfontein felixfontein deleted the pydantic-lint branch September 16, 2024 21:25
@felixfontein
Copy link
Collaborator Author

@gotmax23 thanks for reviewing this!

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.

2 participants