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 type annotations for exotic types; re-use type validators in exotic types #171
Conversation
Codecov Report
@@ Coverage Diff @@
## master #171 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 9 9
Lines 950 939 -11
Branches 207 200 -7
=====================================
- Hits 950 939 -11 |
@samuelcolvin ping! :) |
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.
Otherwise looking good.
Please can you run benchmarks (python benchmarks/run.py pydantic-only
) for master and this branch and include the results here - we should just check that this hasn't caused a significant performance impact.
pydantic/types.py
Outdated
@@ -211,21 +196,13 @@ class NegativeInt(ConstrainedInt): | |||
|
|||
|
|||
class ConstrainedFloat(float): | |||
gt: Union[int, float] = None | |||
lt: Union[int, float] = None | |||
gt: Optional[Union[int, float]] = 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.
Surely cleaner here to use Union[None, int, float]
pydantic/types.py
Outdated
value = value[:cls.curtail_length] | ||
elif cls.max_length is not None and v_len > cls.max_length: | ||
raise ValueError(f'length greater than maximum allowed: {cls.max_length}') | ||
if cls.curtail_length and v_len > cls.curtail_length: |
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 guess here you can use
if cls.curtail_length and len(value) > cls.curtail_length:
...
Since the v_len
variable isn't reused.
Benchmarks on master branch:
And for this branch:
|
Thanks. In future please don't amend commits, I squash merge anyway and amending commits makes it harder to see the changes after my comments. |
No problem @samuelcolvin what do you think about?
|
Sounds good to me. I would do it so usage of We also need to be careful about the exact meaning Eg. with I think the second case makes more sense but most importantly we should be consistent and explicit. |
So to be clear. As I understand you're good with proposal and we can replace In code base we already have an issue with I see there ways:
From code-level I prefer option 1 (I don't like warnings and less code better), but from community side I think option 2 is better. What do you think? Maybe I don't understand you and you have better solution? |
Code is currently correct for the names lt and gt, I think. But will need to change to match the new names. Hence why we need a hard depication lt and gt, because the meaning will change. Shall I do a pr and you can review? |
Yep! |
* Using a function as descriminator for tagged unions * rename tag_key -> discriminator * deal correctly with incorrect return value * tweak error messages * tweak Discriminator logic * improve coverage
Changes:
To discuss:
gt
andlt
inConstrainedInt
andConstrainedFloat
because we already havemin_number_size
andmax_number_size
forint
andfloat
and to my mind it looks like kind of inconsistency. And I think we can do this backward compatible by adding newmin_size
andmax_size
without removinggt
andlt
. What do you think?