Skip to content
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

Improve boolean validator #24294

Merged
merged 4 commits into from
Jun 8, 2019

Conversation

Swamp-Ig
Copy link
Contributor

@Swamp-Ig Swamp-Ig commented Jun 4, 2019

Description:

Improved the boolean validator to make it more robust. Picked out the important bits of #23293

No longer allows non-boolean, non-number, non-string, non None types through (invalid value). Strips out surrounding white space. It seemed odd that " true" would be an invalid value as would "dog", but [56] would not and would validate to True.

The core of the validator is unchanged in that strings are either explicitly true or false, or invalid, it's just more robust in the face of non-string types and whitespace.

It may be worth considering 'open' to be true and 'closed' to be false as well.

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist

If the code does not interact with devices:

  • Tests have been added to verify that the new code works.

@Swamp-Ig Swamp-Ig requested a review from a team as a code owner June 4, 2019 05:28
@homeassistant homeassistant added core small-pr PRs with less than 30 lines. cla-signed labels Jun 4, 2019
@@ -81,14 +82,20 @@ def validate(obj: Dict) -> Dict:

def boolean(value: Any) -> bool:
"""Validate and coerce a boolean value."""
if value is None:
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we allow this? I think this could be quite unintuitive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the face of it I agree, however I am concerned about regressions if it's not there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't fear too many regressions, it is difficult to specify None in YAML, so I doubt many people do. (famous last words!)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest we remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will remove it and see what happens 🙈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh... it didn't work there were a few tests depending on the None behaviour, although they were validating that it didn't actually change anything but not catching a validation exception. I've patched them so they work right.

Otherwise we can roll-back to allowing None as a value.

IDK, I'm still a bit edgy about it. Open to comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise just merge it 😨

homeassistant/helpers/config_validation.py Outdated Show resolved Hide resolved
Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to merge when is None check removed. Also fine to continue the discussion!

@Swamp-Ig Swamp-Ig mentioned this pull request Jun 7, 2019
4 tasks
@Swamp-Ig Swamp-Ig requested a review from balloob June 7, 2019 05:57
@frenck
Copy link
Member

frenck commented Jun 7, 2019

" true" simply isn't a valid boolean in YAML 1.1, for YAML 1.2 it becomes even stricter.

In YAML 1.1: y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF
In YAML 1.2: true|false

I would like to suggest not making it less strict on that part. So I personally do not agree on the .strip() part.

PS: Recently we made all documentation YAML 1.2 compatible.

@OttoWinter
Copy link
Member

OttoWinter commented Jun 7, 2019

@frenck Yes, YAML 1.2 no longer auto-converts certain text to booleans. But the reason that was done was very simple: tons of people were trying to use the given strings as text (strings), but any valid YAML parser would happily auto-convert that to a boolean, removing the original text information.

However, here we know that the given option is a boolean, so why not make it aceept a bunch of types that clearly indicate the user's intent.

The only reason not to that i can think of is that having multiple possible values for booleans is potentially confusing. So for example users might be confused whether all of the above have the same semantics (which they do).

But on the other hand, it would also be a huge breaking change - and i think there's a lot of value for accepting data when the user's intent is very clear.

Edit: breaking change would also hurt for a long time after that: Many examples on other websites (not home-assistant.io) will no longer work.

Copy link
Member

frenck commented Jun 7, 2019

I would rather educate people than handle every possible "wrong" use-case in code just to be friendly. IMHO we should stick to spec to prevent falling into edge cases like this and giving the user the false sense of doing it right.

@Swamp-Ig
Copy link
Contributor Author

Swamp-Ig commented Jun 7, 2019

While the package is config validation, it also validates other things too like arguments to service requests, so we're not strictly talking yaml here.

I don't really feel that there's all that much to be gained by not accepting " true" since like @OttoWinter said, the user's intent is pretty clear from that, particularly in the context that we're actually validating a boolean 'thing'. But... I don't think it matters all that much in the grand scheme and could be persuaded.

Really the main thing I didn't like about the old validator was that it validated non-empty complex types like arrays as True and that's gotta be a bad thing.

@balloob
Copy link
Member

balloob commented Jun 8, 2019

I think that strip is fine, because YAML will also automatically strip non-quoted strings:

image

@balloob balloob merged commit b30f4b8 into home-assistant:dev Jun 8, 2019
@Swamp-Ig Swamp-Ig deleted the improve-boolean-validator branch June 8, 2019 06:11
@balloob balloob mentioned this pull request Jun 26, 2019
alandtse pushed a commit to alandtse/home-assistant that referenced this pull request Oct 12, 2019
* Improve boolean validator

* Remove extra throw

* Remove None test as discussed

* Fix for tests depending on None == False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed core small-pr PRs with less than 30 lines.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants