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

truthy rule: add check-keys option #247

Merged

Conversation

ilyam8
Copy link
Contributor

@ilyam8 ilyam8 commented Apr 4, 2020

Fixes: #158

This PR adds check-keys option to the truthy rule.

If check-keys option is disabled the rule applies only to the values. Default is enabled.

Copy link
Owner

@adrienverge adrienverge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution Илья!

I just changed a few bytes for the linter to pass.

--- a/yamllint/rules/truthy.py
+++ b/yamllint/rules/truthy.py
@@ -30,9 +30,9 @@ This can be useful to prevent surprises from YAML parsers transforming
   ``'False'``, ``'false'``, ``'YES'``, ``'Yes'``, ``'yes'``, ``'NO'``,
   ``'No'``, ``'no'``, ``'ON'``, ``'On'``, ``'on'``, ``'OFF'``, ``'Off'``,
   ``'off'``.
-
-* ``check-keys`` by default, truthy rule applies to both keys and values.
-To disable key verification set this option to ``'false'``.
+* ``check-keys`` disables verification for keys in mappings. By default,
+  ``truthy`` rule applies to both keys and values. Set this option to ``false``
+  to prevent this.
 
 .. rubric:: Examples
 
@@ -136,9 +136,9 @@ def check(conf, token, prev, next, nextnext, context):
     if prev and isinstance(prev, yaml.tokens.TagToken):
         return
 
-    if not conf['check-keys']:
-        if isinstance(prev, yaml.tokens.KeyToken) and isinstance(token, yaml.tokens.ScalarToken):
-            return
+    if (not conf['check-keys'] and isinstance(prev, yaml.tokens.KeyToken) and
+            isinstance(token, yaml.tokens.ScalarToken)):
+        return
 
     if isinstance(token, yaml.tokens.ScalarToken):
         if (token.value in (set(TRUTHY) - set(conf['allowed-values'])) and
--- a/tests/rules/test_truthy.py
+++ b/tests/rules/test_truthy.py
@@ -121,11 +121,22 @@ class TruthyTestCase(RuleTestCase):
                 '  check-keys: false\n'
                 'key-duplicates: disable\n')
         self.check('---\n'
-                   'YES: 0\nYes: 0\nyes: 0\n'
-                   'No: 0\nNo: 0\nno: 0\n'
-                   'TRUE: 0\nTrue: 0\ntrue: 0\n'
-                   'FALSE: 0\nFalse: 0\nfalse: 0\n'
-                   'ON: 0\nOn: 0\non: 0\n'
-                   'OFF: 0\nOff: 0\noff: 0\n'
-                   'YES:\n  Yes:\n    yes:\n      on: 0\n',
+                   'YES: 0\n'
+                   'Yes: 0\n'
+                   'yes: 0\n'
+                   'No: 0\n'
+                   'No: 0\n'
+                   'no: 0\n'
+                   'TRUE: 0\n'
+                   'True: 0\n'
+                   'true: 0\n'
+                   'FALSE: 0\n'
+                   'False: 0\n'
+                   'false: 0\n'
+                   'ON: 0\n'
+                   'On: 0\n'
+                   'on: 0\n'
+                   'OFF: 0\n'
+                   'Off: 0\n'
+                   'off: 0\n',
                    conf)

@ilyam8
Copy link
Contributor Author

ilyam8 commented Apr 8, 2020

Илья!

😲 ☺️

@coveralls
Copy link

Coverage Status

Coverage increased (+0.004%) to 97.878% when pulling 3d0fd01 on ilyam8:truthy_add_check_keys_option into 542ae75 on adrienverge:master.

@adrienverge adrienverge merged commit 6ce11de into adrienverge:master Apr 8, 2020
adrienverge added a commit that referenced this pull request Apr 8, 2020
adrienverge added a commit that referenced this pull request Jan 31, 2024
Specification of YAML ≤ 1.1 has 18 boolean values:

    true  | True  | TRUE | false | False | FALSE
    yes   | Yes   | YES  | no    | No    | NO
    on    | On    | ON   | off   | Off   | OFF

Whereas YAML 1.2 spec recognizes only 6:

    true  | True  | TRUE | false | False | FALSE

For documents that explicit state their YAML spec version at the top of
the document, let's adapt the list of forbidden values.

In the future, we should:
- implement a configuration option to declare the default YAML spec
  version, e.g. `default-yaml-spec-version: 1.2`,
- consider making 1.2 the default in a future release (this would be a
  slight breaking change, but yamllint always tried to be
  1.2-compatible).
- consider adapting yamllint to other 1.1 vs. 1.2 differences [^2].

Solves: #587

Related to: #559 #540 #430 #344 #247 #232 #158

[^1]: https://yaml.org/spec/1.2.2/#1032-tag-resolution
[^2]: https://yaml.org/spec/1.2.2/ext/changes/#changes-in-version-12-revision-120-2009-07-21
adrienverge added a commit that referenced this pull request Jan 31, 2024
Specification of YAML ≤ 1.1 has 18 boolean values:

    true  | True  | TRUE | false | False | FALSE
    yes   | Yes   | YES  | no    | No    | NO
    on    | On    | ON   | off   | Off   | OFF

Whereas YAML 1.2 spec recognizes only 6 [^1]:

    true  | True  | TRUE | false | False | FALSE

For documents that explicit state their YAML spec version at the top of
the document, let's adapt the list of forbidden values.

In the future, we should:
- implement a configuration option to declare the default YAML spec
  version, e.g. `default-yaml-spec-version: 1.2`,
- consider making 1.2 the default in a future release (this would be a
  slight breaking change, but yamllint always tried to be
  1.2-compatible).
- consider adapting yamllint to other 1.1 vs. 1.2 differences [^2].

Solves: #587

Related to: #559 #540 #430 #344 #247 #232 #158

[^1]: https://yaml.org/spec/1.2.2/#1032-tag-resolution
[^2]: https://yaml.org/spec/1.2.2/ext/changes/#changes-in-version-12-revision-120-2009-07-21
adrienverge added a commit that referenced this pull request Feb 4, 2024
Specification of YAML ≤ 1.1 has 22 boolean values:

    y     | Y            | n     | N
    yes   | Yes   | YES  | no    | No    | NO
    true  | True  | TRUE | false | False | FALSE
    on    | On    | ON   | off   | Off   | OFF

Whereas YAML 1.2 spec recognizes only 6 [^1]:

    true  | True  | TRUE | false | False | FALSE

For documents that explicit state their YAML spec version at the top of
the document, let's adapt the list of forbidden values.

In the future, we should:
- implement a configuration option to declare the default YAML spec
  version, e.g. `default-yaml-spec-version: 1.2`,
- consider making 1.2 the default in a future release (this would be a
  slight breaking change, but yamllint always tried to be
  1.2-compatible).
- consider adapting yamllint to other 1.1 vs. 1.2 differences [^2].

Solves: #587

Related to: #559 #540 #430 #344 #247 #232 #158

[^1]: https://yaml.org/spec/1.2.2/#1032-tag-resolution
[^2]: https://yaml.org/spec/1.2.2/ext/changes/#changes-in-version-12-revision-120-2009-07-21
adrienverge added a commit that referenced this pull request Feb 6, 2024
Specification of YAML ≤ 1.1 has 22 boolean values:

    y     | Y            | n     | N
    yes   | Yes   | YES  | no    | No    | NO
    true  | True  | TRUE | false | False | FALSE
    on    | On    | ON   | off   | Off   | OFF

Whereas YAML 1.2 spec recognizes only 6 [^1]:

    true  | True  | TRUE | false | False | FALSE

For documents that explicit state their YAML spec version at the top of
the document, let's adapt the list of forbidden values.

In the future, we should:
- implement a configuration option to declare the default YAML spec
  version, e.g. `default-yaml-spec-version: 1.2`,
- consider making 1.2 the default in a future release (this would be a
  slight breaking change, but yamllint always tried to be
  1.2-compatible).
- consider adapting yamllint to other 1.1 vs. 1.2 differences [^2].

Solves: #587

Related to: #559 #540 #430 #344 #247 #232 #158

[^1]: https://yaml.org/spec/1.2.2/#1032-tag-resolution
[^2]: https://yaml.org/spec/1.2.2/ext/changes/#changes-in-version-12-revision-120-2009-07-21
adrienverge added a commit that referenced this pull request Feb 6, 2024
Specification of YAML ≤ 1.1 has 22 boolean values:

    y     | Y            | n     | N
    yes   | Yes   | YES  | no    | No    | NO
    true  | True  | TRUE | false | False | FALSE
    on    | On    | ON   | off   | Off   | OFF

Whereas YAML 1.2 spec recognizes only 6 [^1]:

    true  | True  | TRUE | false | False | FALSE

For documents that explicit state their YAML spec version at the top of
the document, let's adapt the list of forbidden values.

In the future, we should:
- implement a configuration option to declare the default YAML spec
  version, e.g. `default-yaml-spec-version: 1.2`,
- consider making 1.2 the default in a future release (this would be a
  slight breaking change, but yamllint always tried to be
  1.2-compatible).
- consider adapting yamllint to other 1.1 vs. 1.2 differences [^2].

Solves: #587

Related to: #559 #540 #430 #344 #247 #232 #158

[^1]: https://yaml.org/spec/1.2.2/#1032-tag-resolution
[^2]: https://yaml.org/spec/1.2.2/ext/changes/#changes-in-version-12-revision-120-2009-07-21
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.

false positive truthy failure on travis files
3 participants