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

quoted-strings only-when-needed incorrectly detecting strings with redundant quotes #242

Closed
RealOrangeOne opened this issue Mar 30, 2020 · 3 comments · Fixed by #254
Closed

Comments

@RealOrangeOne
Copy link

Since upgrading to 1.21.0, I enabled the following config:

  quoted-strings:
    quote-type: double
    required: only-when-needed

Unfortunately, seems there's some strings which it believes don't need quoting, when in fact they do:

  • aur_builder ALL=(ALL) NOPASSWD: /usr/bin/pacman
  • %H:%M:%S
  • 'Mac' in ansible_facts.product_name

It seems to be something around when a string either contains or starts with a special character, but isn't entirely a special character?

@adrienverge
Copy link
Owner

Hi @RealOrangeOne (cc @ruipinge), thanks for the report and sorry about that.

There are actually 2 problems:

  1. Strings that actually need quotes, but only-when-needed reports them as "redundantly quoted":
    - "aur_builder ALL=(ALL) NOPASSWD: /usr/bin/pacman"
    - "%H:%M:%S"
    - "'Mac' in ansible_facts.product_name"
  2. Strings that don't need quotes, but undetected by only-when-needed:
    - "-----"
    - ":char"

I believe using

START_TOKENS = {'#', '*', '!', '?', '@', '`', '&',
',', '-', '{', '}', '[', ']', ':'}

and checking token.value[0] in START_TOKENS is not a good way to do, because we can't ensure the need for quotes using only the first character.

Could an alternative solution be to drop START_TOKENS, and try parsing the value to check whether it would be seen as a string or something else?
Do you have any other idea?

@RealOrangeOne
Copy link
Author

Perhaps, although there are complexities. For example, given foo: 6, should it be quoted or not?

Perhaps attempting to parse the value, and if it results in a complex type (ie list, object, parse error, whatever), then raise?

I'd be interested in knowing if there's a solution for the simpler data types (int, bool etc), but I can't think of one.

@adrienverge
Copy link
Owner

From a user perspective, foo: 6 isn't a problem: either it's quoted (therefore it's a string), or it's not (so it's a mapping). In either case, yamllint shouldn't complain.

I'm not sure I understand what you mean by "then raise".

To be clearer about my idea on required: only-when-needed: if a quoted string is found (e.g. "foo: bar" or "-----"), try to parse its contents as YAML.
→ If it results in a string (e.g. "-----""-----"), then warn "redundantly quoted".
→ If it results in another type (e.g. "foo: bar"{foo: bar}), don't report anything.

adrienverge added a commit that referenced this issue Apr 13, 2020
Change implementation of `required: only-when-needed`, because
maintaining a list of `START_TOKENS` and just looking at the first
character of string values has proven to be partially broken.

Cf. discussion at
#246 (comment).

Fixes #242.
adrienverge added a commit that referenced this issue Apr 13, 2020
Change implementation of `required: only-when-needed`, because
maintaining a list of `START_TOKENS` and just looking at the first
character of string values has proven to be partially broken.

Cf. discussion at
#246 (comment).

Fixes #242 and
#244.
adrienverge added a commit that referenced this issue Apr 13, 2020
Change implementation of `required: only-when-needed`, because
maintaining a list of `START_TOKENS` and just looking at the first
character of string values has proven to be partially broken.

Cf. discussion at
#246 (comment).

Fixes #242 and
#244.
adrienverge added a commit that referenced this issue Apr 15, 2020
Change implementation of `required: only-when-needed`, because
maintaining a list of `START_TOKENS` and just looking at the first
character of string values has proven to be partially broken.

Cf. discussion at
#246 (comment).

Fixes #242 and
#244.
RealOrangeOne added a commit to RealOrangeOne/dotfiles that referenced this issue Apr 15, 2020
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

Successfully merging a pull request may close this issue.

2 participants