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

The type order of optional arguments affects the results #189

Open
fouzhe opened this issue Sep 19, 2022 · 2 comments
Open

The type order of optional arguments affects the results #189

fouzhe opened this issue Sep 19, 2022 · 2 comments

Comments

@fouzhe
Copy link

fouzhe commented Sep 19, 2022

I used version: 1.6.1. The following program is correct:

s = "mode: default"

schema_map = {
    "mode": sy.Str(),
    sy.Optional("duration", default=None, drop_if_none=False): sy.EmptyNone()
    | sy.Float(),
}

config_yaml = sy.load(
    yaml_string=s,
    schema=sy.Map(schema_map),
)

But when I change the type order of optional duration like following:

s = "mode: default"

schema_map = {
    "mode": sy.Str(),
    sy.Optional("duration", default=None, drop_if_none=False): sy.Float()
    | sy.EmptyNone(),
}

config_yaml = sy.load(
    yaml_string=s,
    schema=sy.Map(schema_map),
)

then there would be an error, here is the error log:

_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../vendor/lib/python3.10/site-packages/strictyaml/parser.py:323: in load
    return generic_load(yaml_string, schema=schema, label=label)
../vendor/lib/python3.10/site-packages/strictyaml/parser.py:301: in generic_load
    return schema(YAMLChunk(document, label=label))
../vendor/lib/python3.10/site-packages/strictyaml/validators.py:17: in __call__
    self.validate(chunk)
../vendor/lib/python3.10/site-packages/strictyaml/compound.py:180: in validate
    new_value = value_validator(
../vendor/lib/python3.10/site-packages/strictyaml/validators.py:107: in __call__
    result = self._validator_a(chunk)
../vendor/lib/python3.10/site-packages/strictyaml/scalar.py:27: in __call__
    return YAML(chunk, validator=self)
../vendor/lib/python3.10/site-packages/strictyaml/representation.py:63: in __init__
    self._value = validator.validate(value)
../vendor/lib/python3.10/site-packages/strictyaml/scalar.py:30: in validate
    return self.validate_scalar(chunk)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = Float()
chunk = <strictyaml.yamllocation.YAMLChunk object at 0x7f64f2212aa0>

    def validate_scalar(self, chunk):
        val = chunk.contents
        if utils.is_infinity(val) or utils.is_not_a_number(val):
            val = val.replace(".", "")
        elif not utils.is_decimal(val):
            chunk.expecting_but_found("when expecting a float")
        # Only Python 3.6+ supports underscores in numeric literals
>       return float(val.replace("_", ""))
E       ValueError: could not convert string to float: ''

../vendor/lib/python3.10/site-packages/strictyaml/scalar.py:230: ValueError

But if changing sy.Float() to sy.Int(), this error would not arise.

@wikiped
Copy link

wikiped commented Jul 26, 2023

is_decimal is using regex pattern which matches empty string and therefore returns False Positive result.

Adding two lines to is_decimal before existing return ...:

if not value:
    return False

Would fix the issue.

I would do a PR, but it looks like they are not processed / handled for now.

@fouzhe
Copy link
Author

fouzhe commented Jul 30, 2023

@wikiped Thanks!

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