From 7ff873e76e906dd2eed6dc1dab7c3d07b4f610f3 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 15 May 2024 16:44:03 -0400 Subject: [PATCH] Don't quote strings that start with 0 when running ansible-lint --fix. In order to try to handle [the mess that are octals in YAML](https://github.com/ansible/ansible-lint/issues/2965), some [special-case code was added](https://github.com/ansible/ansible-lint/pull/3030) to handle leading-zeros. But it caught too much, and would force quotes strings like on 00-header and 0.0.0.0, even when .yamllint doesn't require them: ``` quoted-strings: required: false ``` and it generates awkward lists, like ```diff loop: - "00-header" - 10-help-text - 50-landscape-sysinfo - 50-motd-news - 88-esm-announce - 97-overlayroot ``` --- src/ansiblelint/yaml_utils.py | 23 +++++++++++++++------ test/fixtures/formatting-after/fmt-2.yml | 7 +++++++ test/fixtures/formatting-before/fmt-2.yml | 7 +++++++ test/fixtures/formatting-prettier/fmt-2.yml | 7 +++++++ 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/ansiblelint/yaml_utils.py b/src/ansiblelint/yaml_utils.py index 6a479211be..9d58d8c11b 100644 --- a/src/ansiblelint/yaml_utils.py +++ b/src/ansiblelint/yaml_utils.py @@ -642,14 +642,25 @@ def choose_scalar_style(self) -> Any: and self.event.value.startswith("0") and len(self.event.value) > 1 ): - if self.event.tag == "tag:yaml.org,2002:int" and self.event.implicit[0]: - if self.event.value.startswith("0x"): - self.event.tag = "tag:yaml.org,2002:str" - return "" - # ensures that "0123" string does not lose its quoting + # We have an as-yet unquoted token that starts with "0" (but is not itself the digit 0). + # It could be: + # - hexadecimal like "0xF1" or ; comes tagged as int. Should continue unquoted to continue as an int. + # - octal like "0666" or "0o755"; comes tagged as str. **Should** be quoted to be cross-YAML compatible. + # - string like "0.0.0.0" and "00-header". Should not be quoted, unless it has a quote in it. + if self.event.value.startswith("0x") and self.event.tag == "tag:yaml.org,2002:int" and self.event.implicit[0]: + # hexadecimal + self.event.tag = "tag:yaml.org,2002:str" + return "" + try: + int(self.event.value, 8) + except ValueError: + pass + # fallthrough to string + else: + # octal self.event.tag = "tag:yaml.org,2002:str" self.event.implicit = (True, True, True) - return '"' + return '"' if style != "'": # block scalar, double quoted, etc. return style diff --git a/test/fixtures/formatting-after/fmt-2.yml b/test/fixtures/formatting-after/fmt-2.yml index a162721772..3601acc225 100644 --- a/test/fixtures/formatting-after/fmt-2.yml +++ b/test/fixtures/formatting-after/fmt-2.yml @@ -22,3 +22,10 @@ - 10 - 9999 zero: 0 # Not an octal. See #2071 + +- string: + - 0steps + - 9steps + - 0.0.0.0 + - "0" + - "01234" diff --git a/test/fixtures/formatting-before/fmt-2.yml b/test/fixtures/formatting-before/fmt-2.yml index 294166327c..dbfb777413 100644 --- a/test/fixtures/formatting-before/fmt-2.yml +++ b/test/fixtures/formatting-before/fmt-2.yml @@ -22,3 +22,10 @@ - 10 - 9999 zero: 0 # Not an octal. See #2071 + + - string: + - 0steps + - 9steps + - 0.0.0.0 + - "0" + - "01234" diff --git a/test/fixtures/formatting-prettier/fmt-2.yml b/test/fixtures/formatting-prettier/fmt-2.yml index 90ac484e32..037c9dd73b 100644 --- a/test/fixtures/formatting-prettier/fmt-2.yml +++ b/test/fixtures/formatting-prettier/fmt-2.yml @@ -22,3 +22,10 @@ - 10 - 9999 zero: 0 # Not an octal. See #2071 + +- string: + - 0steps + - 9steps + - 0.0.0.0 + - "0" + - "01234"