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

Booleans validate as integers #37

Closed
mthebridge opened this issue Jan 16, 2020 · 3 comments
Closed

Booleans validate as integers #37

mthebridge opened this issue Jan 16, 2020 · 3 comments

Comments

@mthebridge
Copy link

Yangson considers the Boolean true and false as valid values for integer type nodes.

For example, with this minimal schema:

$ cat test.json 
{
    "ietf-yang-library:modules-state": {
      "module-set-id": "",
      "module": [
        {
          "name": "test",
          "revision": "1.0.0",
          "conformance-type": "implement"
        }
      ]
    }
  }
$ cat test.yang 
module test {
    prefix ;
    revision 1.0.0;
    description "Test schema.";
    leaf number {
        type int32;
    }
}

Then I'd expect the following code to raise an exception, but it parses happily:

from yangson import DataModel
import yaml

data = DataModel.from_file("./test.json")
yaml_input = yaml.safe_load("'test:number': true")
inst = data.from_raw(yaml_input)
inst.validate()

Replacing true with any string or non-integer value triggers a RawTypeError as expected.

@mthebridge
Copy link
Author

mthebridge commented Jan 16, 2020

Suspect

def __contains__(self, val: int) -> bool:
is the culprit - confusingly, Python does consider Booleans to be of type Int:

>>> isinstance(True, int)
True

So probably needs some special handling...

CZNIC-GitLab pushed a commit that referenced this issue Jan 17, 2020
Damn Python booleans :-)
@llhotka
Copy link
Member

llhotka commented Jan 17, 2020

Fixed in de4a558 -> PyPI package 1.3.48

@llhotka llhotka closed this as completed Jan 17, 2020
@mthebridge
Copy link
Author

Awesome - thanks for the speedy turnaround!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants