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

on is loaded as True instead of a string #376

Closed
StrayDragon opened this issue Jan 20, 2020 · 7 comments
Closed

on is loaded as True instead of a string #376

StrayDragon opened this issue Jan 20, 2020 · 7 comments
Labels

Comments

@StrayDragon
Copy link

Minimum reproducible situation, complete code here

Use PyYaml 5.3 and CPython 3.8.1

import yaml
s = """on: [push]"""
o = yaml.safe_load(s)
print(o)

will output:

{True: ['push']}

Obviously, it's a wrong answer... and expect {'on': ['push']}

@perlpunk
Copy link
Member

It is actually correct. on is part of the list of strings that are recognized as booleans in YAML 1.1: https://yaml.org/type/bool.html
And PyYAML implements YAML 1.1 (with the exception that y/Y and n/N are not recognized as booleans)

If you want on to be a string, you have to quote it. In PyYAML, there is currently no way to change that behaviour, except using BaseLoader (which will not recognize any special types, only strings).

I'm not sure what the problem has to do with inline lists. As far as I can see you habve a problem with on being loaded as a boolean. So I'll change the title.

Feel free to reopen if you have more questions.

@perlpunk perlpunk changed the title parsing in-line list wrong on is loaded as True instead of a string Jan 20, 2020
@StrayDragon
Copy link
Author

Thanks for your detailed explanation, I understand how to do,

This issue was raised because some other implementations did not resolve on / off to True / False.

In addition, I'm sorry, the title was not described very accurately, because I didn't know these details before, I naturally searched for the same keywords, and then raised this issue. If that can better help others who have the same as me Confused people, that couldn't be better.

@nitzmahone
Copy link
Member

nitzmahone commented Jan 21, 2020

@StrayDragon Actually you can change this behavior if you want by overriding the bool tag regex in the base resolver (https://github.com/yaml/pyyaml/blob/master/lib/yaml/resolver.py#L170-L175), then wire up your resolver instead of the default on a new loader instance (which gets passed to yaml.load().

@StrayDragon
Copy link
Author

@nitzmahone Thanks for your reply, now, I know how to change this behavior at least.

@sukumarp2022

This comment was marked as outdated.

@iharob
Copy link

iharob commented Apr 27, 2024

Is this still correct when on is the key and not the value? This damaged several github action workflows for me :/

@perlpunk
Copy link
Member

Yes, this is correct.
Keys are not really special in YAML in that regard.
There are programming languages where keys can be more than just strings. They can be numbers, booleans or even sequences or mappings.

I recently pulished yamlcore which you can use on top of PyYAML. It will treat github workflow files correctly.
See #555 (comment)

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

No branches or pull requests

5 participants