Skip to content

Conversation

@kleinschrader
Copy link

This pr adds a new option to the quoted string rule set:
single-unless-contains-escaped
I was drawing a blank on the name of it so if its unfavorable i'm completely open to change it.

The reason behind this PR is the following problem:
At our company we use yamllint to lint yamfiles and some time ago some Idiot with the name Timo decided it would be a good idea to force single quote strings. And it want an issue until now as our ci pipelines only lint files that got changed. However recently i had to lint a file with URLs in them and they would trigger the line too long error. As it is there is no good way of breaking down a long string styled with single quotes without intoducing something like spaces. I changing the configuration to require double quotes but that go veto'd so im asking for this feature.

It allows a user to force single quote strings unless it contains a backslash, which would usually indicate an escape character and then allows the use of double quotes.

Affermations:

  • No AI was used in the creation of this code, commit message or PR.
  • I ran flake8 and the testsuite, adding my own tests copied from consistent.

@coveralls
Copy link

coveralls commented Sep 15, 2025

Coverage Status

coverage: 99.816% (+0.001%) from 99.815%
when pulling ea55e79 on kleinschrader:master
into e3b72f5 on adrienverge:master.

@adrienverge
Copy link
Owner

Hello Timo, thanks for the detailed and non-AI-generated story and reason behind this pull request 😉

The problem you describe looks the same as #609 and #753.

Instead of your proposal quote-type: single-unless-contains-escaped, I propose a new option in rule quoted-strings: allow-double-quotes-for-escaping: true | false.

  • When this new option allow-double-quotes-for-escaping is true and required quotes are ' OR when quotes are forbidden, double-quoted strings containing escaped characters would be tolerated.

  • I'm not sure whether this new option should default to true or false (true would be handy but would be a small breaking change), so every contributor's or reader's opinion is welcome now.

  • Instead of changing _quote_match(), I think the code would be more readable (and more performant) if you added some logic similar to the already-existing or (conf['allow-quoted-quotes'] and _has_quoted_quotes(token)). Something like or (conf['allow-double-quotes-for-escaping'] and _has_escaping_in_double_quotes(token)). What do you think?

  • At first I thought you shouldn't simply look for a single backslash \ in the token raw value, but rather look precisely for any of the 3 possible escapings for double-quoted strings shown in https://stackoverflow.com/a/21699210: either an escaped backslash \\, or an escaped double-quote \", or a silented newline \↵. But actually, these 3 escapings are not the only ones allowed inside double quotes. Indeed, the following examples are valid YAML and would be interpreted differently with single quotes: "\0", "\b", "\t", "\n", "\x0a"… So I agree with your code that just looks whether a \ is present or not.

    As an alternative to looking for \ in the value, your code could try to interpret the raw string without quotes and see whether they're parsed differently, similarly to what is done inside _quotes_are_needed(), but it might be too complicated.

  • This is not directly related, but I'm wondering about recent commit 1a6542c "quoted-strings: Fix only-when-needed on multiline with backslash", which added tolerance for \↵ (a silented newline, i.e. a backslash before a newline in the raw token) when and required: only-when-needed and double quotes are expected. I'm not 100% sure, but I think this fix and the present pull request apply to independent situations and should not be gathered.

Can you please clean your code to use the consistent coding style of the rest of yamllint, as well as a commit message that matches the contribution guidelines, and same for the PR title? See recent commits for examples.

@kleinschrader kleinschrader changed the title Feature: Added single-unless-contains-escaped quote type to quoted strings. quoted-strings: Add allow-double-quotes-for-escaping Sep 30, 2025
@kleinschrader
Copy link
Author

@adrienverge Thanks for your feedback!

Instead of your proposal quote-type: single-unless-contains-escaped, I propose a new option in rule quoted-strings: allow->double-quotes-for-escaping: true | false.

When this new option allow-double-quotes-for-escaping is true and required quotes are ' OR when quotes are forbidden, >double-quoted strings containing escaped characters would be tolerated.

This is a great suggestion and i implemented it like that!

Instead of changing _quote_match(), I think the code would be more readable (and more performant) if you added some logic >similar to the already-existing or (conf['allow-quoted-quotes'] and _has_quoted_quotes(token)). Something like or (conf['allow->double-quotes-for-escaping'] and _has_escaping_in_double_quotes(token)). What do you think?

I tried building it like that but i am unsure if that was what you had envisioned.

At first I thought you shouldn't simply look for a single backslash \ in the token raw value, but rather look precisely for any of the >3 possible escapings for double-quoted strings shown in https://stackoverflow.com/a/21699210: either an escaped backslash \, >or an escaped double-quote ", or a silented newline \↵. But actually, these 3 escapings are not the only ones allowed inside >double quotes. Indeed, the following examples are valid YAML and would be interpreted differently with single quotes: "\0", "\b", >"\t", "\n", "\x0a"… So I agree with your code that just looks whether a \ is present or not.

As an alternative to looking for \ in the value, your code could try to interpret the raw string without quotes and see whether >they're parsed differently, similarly to what is done inside _quotes_are_needed(), but it might be too complicated.

This is not directly related, but I'm wondering about recent commit https://github.com/adrienverge/yamllint/>commit/1a6542c58a548ddf92e79b84f31f07167437c679 "quoted-strings: Fix only-when-needed on multiline with backslash", >which added tolerance for \↵ (a silented newline, i.e. a backslash before a newline in the raw token) when and required: only->when-needed and double quotes are expected. I'm not 100% sure, but I think this fix and the present pull request apply to >independent situations and should not be gathered.

For now i would like to avoid checking for specific escapes if thats ok, this does open up a new talking point though if there should be a feature that checks for valid escape sequence [1].

Can you please clean your code to use the consistent coding style of the rest of yamllint, as well as a commit message that >matches the contribution guidelines, and same for the PR title? See recent commits for examples.

I changed the PR title and reworded the commit however could point out your issues with the code styling? Im not trying to be confrontational im just confused.

There is also an issue with this, specifically with quote-type: consistent where if the first string in the file is either an escaped quote or double quoted string it will set the expected qoute type to the wrong one, i however feel like that is out of the scope of this pr.

Thank you for your time!

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 this pull request may close these issues.

3 participants