`is` with a literal
This is how the comment reads inline, under the flagged line.
Why it matters
x is 1 or x is 'a' depends on interpreter caching. It can be true in tests and false in production.
What to do
Use == for values. Keep is for None, True, False.
Examples
if status is 'ok':
if status == 'ok':
What it catches
It reads a pattern on each line, aware of strings and comments. Languages: Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
if x is 1:
pass
if s is 'a':
pass
Left alone
if x is None:
pass
Research
`==`, `var`, `== None`, and `is` with a literal. Coercion and leaky scope make comparisons true or false for the wrong reason.
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"is-literal"
],
"severity": {
"is-literal": "low"
}
}
}
is 1compares identity, not value. It works by accident for small ints and some strings.