-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Fix const validations #794
Conversation
4a6b5cf
to
f964990
Compare
Codecov Report
@@ Coverage Diff @@
## master #794 +/- ##
======================================
Coverage 100% 100%
======================================
Files 17 17
Lines 2984 2847 -137
Branches 580 559 -21
======================================
- Hits 2984 2847 -137
Continue to review full report at Codecov.
|
Not sure if you wan't this in master or 0.32 so I submitted both |
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 a good start, please can you also fix coverage.
does this also resolve #793? |
f964990
to
fb08c18
Compare
No, unfortunately it doesn't, and I don't really have a clue how to fix it. However it might not be a big issue for us since we initialize the constant fields with json compatible dicts instead of submodel instances |
fb08c18
to
43152ee
Compare
Ok changed it up a bit more and now it should work for #793. At least in the most obvious cases. It still fails if you initialize the default value with a BaseModel instance instead of a dict. |
43152ee
to
9260975
Compare
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 good to me, just a few small things to change.
@tiangolo, please could you review this since you did most of the original work on schema.
|
||
m = Model() | ||
assert m.a == [SubModel(b=1), SubModel(b=2), SubModel(b=3)] | ||
assert m.b == [SubModel(b=4), SubModel(b=5), SubModel(b=6)] |
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.
would it also help to add m.schema() == {...}
here?
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.
Added, So this is interesting:
It seems that adding SubModel instances as default breaks in a lot of ways: Also the schema is now not json serializable, similar to the error as per #793 .
It might be a good idea to just document or add a warning of some sorts for that case and consider that unsupported. Of course not all use cases need json serializability and some use cases might want to set the default using a SubModel instance.
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.
please create an issue about this.
tests/test_main.py
Outdated
try: | ||
# Fails | ||
Form(field1={'field': 1}, field2=[{'field': 1}]) | ||
except ValidationError as e: |
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.
use pytest.raises(ValidationError) as exc_info:.... exc_info.e.json()
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.
Fixed
tests/test_main.py
Outdated
Form(field1={'field': 1}, field2=[{'field': 1}]) | ||
except ValidationError as e: | ||
# This should not raise an Json error | ||
assert json.dumps(e.json()) |
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.
this doesn't make sense, e.json()
returns a string, this is effectively just json.dumps('foo') -> '"foo"'
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.
Fixed
Hi @hmvp would be great if you could work on fixing this. I'm trying to get a beta for version 1 out the door asap and I want to include this. |
Sure, I will try to get a fixed MR out today |
This fixes pydantic#620 and pydantic#793
9260975
to
e6d6389
Compare
Pushed an update |
if getattr(self.type_, '__origin__', None): | ||
# type_ has been refined eg. as the type of a List and sub_fields needs to be populated | ||
self.sub_fields = [self._create_sub_type(self.type_, '_' + self.name)] | ||
# type_ has been refined eg. as the type of a List and sub_fields needs to be populated |
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.
|
||
m = Model() | ||
assert m.a == [SubModel(b=1), SubModel(b=2), SubModel(b=3)] | ||
assert m.b == [SubModel(b=4), SubModel(b=5), SubModel(b=6)] |
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.
please create an issue about this.
This fixes #620
NOTICE: pydantic is currently pushing towards V1 release,
see issue 576. Changes not required to release version 1
may be be delayed until after version 1 is released. If your PR is a bugfix for v0.32, please base off and target the
v0.32.x
branch.Change Summary
Related issue number
Checklist
changes/<pull request or issue id>-<github username>.rst
file added describing change(see changes/README.md for details)