-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Make bool_validator strict #617
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #617 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 15 15
Lines 2710 2718 +8
Branches 532 534 +2
=====================================
+ Hits 2710 2718 +8 |
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 I'm happy with this.
@cazgp, you created the issue. Feedback here welcome.
|
Thanks for this! |
|
@samuelcolvin I've updated the docs. However, I still have two questions:
|
|
This pull request introduces 1 alert when merging fc41a25 into ccdf8e1 - view on LGTM.com new alerts:
|
|
This pull request introduces 1 alert when merging 1a73534 into ccdf8e1 - view on LGTM.com new alerts:
|
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 pretty good, RelaxedBool seems good.
See #576 (comment) for discussion on how we deal with history/releases.
docs/index.rst
Outdated
|
|
||
| If you want more permissive behavior, ``RelaxedBool`` can be used to cast values to bool using standard python logic. | ||
| For example, ``None`` would be cast to False. Note that this behaves differently than the standard ``bool`` field | ||
| for some values: for example, a ``RelaxedBool`` field would cast the string "false" to True. |
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.
| for some values: for example, a ``RelaxedBool`` field would cast the string "false" to True. | |
| for some values: for example, a ``RelaxedBool`` field would cast the string ``"false"`` to ``True``. |
7dc18d0 to
e0bfe47
Compare
21c28d8 to
773e160
Compare
|
please can you rebase and move the history change to |
pydantic/types.py
Outdated
| @@ -243,6 +244,21 @@ def validate(cls, value: str) -> str: | |||
| return validate_email(value)[1] | |||
|
|
|||
|
|
|||
| class RelaxedBool(int): | |||
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.
humm, I'm not sure Relaxed is a good choice of word.
I personally think this type is very wierd and people shouldn't use it since it makes absolutely zero sense to parse 'false' to False.
I think either we should:
- rename it to something like
StandardLibBooland recommend against using it - or, (better in my opinion) remove it altogether. Please can implement it themselves if they really want it.
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 think the only reason to use it would be performance; right now if you want speed when you know you are getting the right type, it's awkward. But I agree it would be better if the name reflected this.
Actually I guess the default validator is pretty fast if it is already the right type?
I'd be okay either dropping or giving a different name. (I'll keep my commits locally for when someone asks for it 😈)
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.
okay, let's keep it but say that in the docs.
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.
@samuelcolvin I did some minor investigation of the timing and I think the speed difference between the two bool validators is negligible (when the inputs are bools); in my runs I didn't see a clear winner.
I think probably the only real world use case where it might really be helpful is if you wanted to accept a custom type as a field input and had a custom __bool__ implementation.
At any rate, based on the above I can't think of anything worth saying in the docs except that it follows built-in python logic for boolean casting (what they currently say). Let me know if you still disagree, or if this changes your mind back to wanting to remove RelaxedBool.
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 would say let's remove it then. Nothing to stop people implementing it themselves if they really want it. But we we think there's basically no context in which you should use it, it shouldn't be added.
Sorry to flip flop.
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Adrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com> Co-authored-by: Samuel Colvin <s@muelcolvin.com> Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
Change Summary
Make
bool_validatora little more strict, in line with #579In particular, allow
True,False,1,0, all yaml true/false strings, the strings't'/'T'/'f'/'F'(following DRF), and the bytes versions of those strings. Anything else raises aValidationError.Also adds
RelaxedBoolto retain current functionality if desired.Related issue number
#579
Reviewing the discussion in #579, it wasn't clear to me whether the goal was to modify
StrictBoolto allow these strings, or to changebool_validator. My current interpretation is that the goal was to changebool_validatorto bring it in line with more expected/useful behavior in a non-python context. If that's wrong, I'd be happy to refactor so that this logic applies toStrictBoolinstead.This would be a breaking change for a very common type (if modifying
bool_validatoranyway), so I wouldn't necessarily expect it to be merged prior to a 1.0 release, but figured I'd start the process.Checklist
HISTORY.rsthas been updated#<number>@<whomever>