feat(truthy): Allow explicit types
With this change, we don't require quotes for truthy values that are
explicitly typed. For instance, the following examples are all
considered valid:
string1: !!str True
string2: !!str yes
string3: !!str off
encoded: !!binary |
True
OFF
pad== # this decodes as 'N\xbb\x9e8Qii'
boolean1: !!bool true
boolean2: !!bool "false"
boolean3: !!bool FALSE
boolean4: !!bool True
boolean5: !!bool off
boolean6: !!bool NO
This commit is contained in:
@@ -30,24 +30,39 @@ class TruthyTestCase(RuleTestCase):
|
|||||||
def test_enabled(self):
|
def test_enabled(self):
|
||||||
conf = 'truthy: enable\n'
|
conf = 'truthy: enable\n'
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'1: True\n', conf, problem=(2, 4))
|
'1: True\n'
|
||||||
self.check('---\n'
|
'True: 1\n',
|
||||||
'True: 1\n', conf, problem=(2, 1))
|
conf, problem1=(2, 4), problem2=(3, 1))
|
||||||
self.check('---\n'
|
|
||||||
'1: "True"\n', conf)
|
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
|
'1: "True"\n'
|
||||||
'"True": 1\n', conf)
|
'"True": 1\n', conf)
|
||||||
|
self.check('---\n'
|
||||||
|
'[\n'
|
||||||
|
' true, false,\n'
|
||||||
|
' "false", "FALSE",\n'
|
||||||
|
' "true", "True",\n'
|
||||||
|
' True, FALSE,\n'
|
||||||
|
' on, OFF,\n'
|
||||||
|
' NO, Yes\n'
|
||||||
|
']\n', conf,
|
||||||
|
problem1=(6, 3), problem2=(6, 9),
|
||||||
|
problem3=(7, 3), problem4=(7, 7),
|
||||||
|
problem5=(8, 3), problem6=(8, 7))
|
||||||
|
|
||||||
def test_explicit_boolean(self):
|
def test_explicit_types(self):
|
||||||
conf = 'truthy: enable\n'
|
conf = 'truthy: enable\n'
|
||||||
self.check('---\n'
|
self.check('---\n'
|
||||||
'!!seq [\n'
|
'string1: !!str True\n'
|
||||||
' !!bool true, !!bool false,\n'
|
'string2: !!str yes\n'
|
||||||
' !!bool "false", !!bool "FALSE",\n'
|
'string3: !!str off\n'
|
||||||
' !!bool "true", !!bool "True",\n'
|
'encoded: !!binary |\n'
|
||||||
' !!bool "false", !!bool "FALSE",\n'
|
' True\n'
|
||||||
']\n', conf)
|
' OFF\n'
|
||||||
self.check('---\n'
|
' pad==\n' # this decodes as 'N\xbb\x9e8Qii'
|
||||||
'!!seq [\n'
|
'boolean1: !!bool true\n'
|
||||||
' !!bool true, !!bool True,\n'
|
'boolean2: !!bool "false"\n'
|
||||||
']\n', conf, problem=(3, 23))
|
'boolean3: !!bool FALSE\n'
|
||||||
|
'boolean4: !!bool True\n'
|
||||||
|
'boolean5: !!bool off\n'
|
||||||
|
'boolean6: !!bool NO\n',
|
||||||
|
conf)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Use this rule to forbid truthy values that are not quoted.
|
Use this rule to forbid truthy values that are not quoted nor explicitly typed.
|
||||||
|
|
||||||
This would prevent YAML parsers to tranform ``[yes, FALSE, Off]`` into ``[true,
|
This would prevent YAML parsers to tranform ``[yes, FALSE, Off]`` into ``[true,
|
||||||
false, false]`` or ``{y: 1, yes: 2, on: 3, true: 4, True: 5}`` into ``{y: 1,
|
false, false]`` or ``{y: 1, yes: 2, on: 3, true: 4, True: 5}`` into ``{y: 1,
|
||||||
@@ -37,6 +37,21 @@ true: 5}``.
|
|||||||
"true": 3
|
"true": 3
|
||||||
"True": 4
|
"True": 4
|
||||||
|
|
||||||
|
explicit:
|
||||||
|
string1: !!str True
|
||||||
|
string2: !!str yes
|
||||||
|
string3: !!str off
|
||||||
|
encoded: !!binary |
|
||||||
|
True
|
||||||
|
OFF
|
||||||
|
pad== # this decodes as 'N\xbb\x9e8Qii'
|
||||||
|
boolean1: !!bool true
|
||||||
|
boolean2: !!bool "false"
|
||||||
|
boolean3: !!bool FALSE
|
||||||
|
boolean4: !!bool True
|
||||||
|
boolean5: !!bool off
|
||||||
|
boolean6: !!bool NO
|
||||||
|
|
||||||
the following code snippet would **FAIL**:
|
the following code snippet would **FAIL**:
|
||||||
::
|
::
|
||||||
|
|
||||||
@@ -68,6 +83,9 @@ TRUTHY = ['YES', 'Yes', 'yes',
|
|||||||
|
|
||||||
|
|
||||||
def check(conf, token, prev, next, nextnext, context):
|
def check(conf, token, prev, next, nextnext, context):
|
||||||
|
if prev and isinstance(prev, yaml.tokens.TagToken):
|
||||||
|
return
|
||||||
|
|
||||||
if isinstance(token, yaml.tokens.ScalarToken):
|
if isinstance(token, yaml.tokens.ScalarToken):
|
||||||
if token.value in TRUTHY and token.style is None:
|
if token.value in TRUTHY and token.style is None:
|
||||||
yield LintProblem(token.start_mark.line + 1,
|
yield LintProblem(token.start_mark.line + 1,
|
||||||
|
|||||||
Reference in New Issue
Block a user