-
Notifications
You must be signed in to change notification settings - Fork 247
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 max length error on conlist with type int #902
Fix max length error on conlist with type int #902
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #902 +/- ##
==========================================
+ Coverage 93.78% 93.84% +0.05%
==========================================
Files 105 105
Lines 15331 15459 +128
Branches 25 25
==========================================
+ Hits 14378 14507 +129
+ Misses 947 946 -1
Partials 6 6
... and 11 files with indirect coverage changes Continue to review full report in Codecov by Sentry.
|
CodSpeed Performance ReportMerging #902 will not alter performanceComparing Summary
|
@@ -391,9 +396,9 @@ def f(v: int) -> int: | |||
{ | |||
'type': 'too_long', | |||
'loc': (), | |||
'msg': 'List should have at most 10 items after validation, not 11', | |||
'msg': 'List should have at most 10 items after validation, not 15', |
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.
Since the actual size of the list is 15 and not 11 I decided to change the values.
Please review |
tests/serializers/test_bytes.py
Outdated
@@ -86,7 +86,7 @@ def test_subclass_bytes(schema_type, input_value, expected_json): | |||
|
|||
v = s.to_python(input_value, mode='json') | |||
assert v == expected_json | |||
assert type(v) == str # noqa: E721 | |||
assert isinstance(v, str) |
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.
I see that this is also happening in #914. I might make a PR to make just this change so we avoid conflicts
let capacity = self | ||
.generic_len() | ||
.unwrap_or_else(|| max_length.unwrap_or(DEFAULT_CAPACITY)); |
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.
So if it's somethign that has a len (like a list) we use that, if it doesn't have a len we use DEFAULT_CAPACITY
. That means that if you feed a generator in you'll end up with an error message that includes whatever DEFAULT_CAPACITY
is, right?
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.
That's right, for the first part but for the generator I realise it wasn't working. The max_length need to be caught by a None condition. So if someone validates this:
from pydantic_core import SchemaValidator
def infinite_generator():
i = 0
while True:
yield i
i += 1
v = SchemaValidator({'type': 'list'})
v.validate_python(infinite_generator())
Then the error message will be:
Otherwise, this will run into an infinite loop.
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.
Sorry meant to request changes in the earlier review.
Please review |
this is a mistake, we need to revert it ASAP. |
Improved in #990 |
Change Summary
The list length displayed an incorrect error message when constrained to only integer types
Example Code:
Before:
List should have at most 4 items after validation, not 5.
Now:
List should have at most 4 items after validation, not 9.
Related issue number
fix pydantic/pydantic#7059
Checklist
pydantic-core
(except for expected changes)Selected Reviewer: @dmontagu