-
Notifications
You must be signed in to change notification settings - Fork 6
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
Pydantic: add helper, generalize forbid extra code #169
Conversation
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...) |
_modify_config(models, processed_classes, change_config) | ||
|
||
|
||
def forbid_extras(models: type[p.BaseModel] | Collection[type[p.BaseModel]]) -> None: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this 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!
@gotmax23 thanks for reviewing this! |
No description provided.