SCR110: comparison-with-literal-identity
is / is not with a string or number literal
| Code | SCR110 |
| Name | comparison-with-literal-identity |
| Category | Scripts (SCR) |
| Default severity | warning |
| Enabled by default | yes |
| Fix available | sometimes |
| Fix safety | unsafe |
| ign-lint equivalent | none |
What it does
Section titled “What it does”Reports x is 'text', x is 1, x is not '' and the like: identity
comparisons where one side is a string or number literal. Comparisons with
None, True and False are fine.
Why is this bad?
Section titled “Why is this bad?”is compares object identity, not value. Whether two equal strings or
numbers are the same object is an implementation detail; in Jython it
differs between interned literals, values read from tags or databases
(Java strings converted to Python), and small and large integers. The
comparison works in testing and fails in production.
Example
Section titled “Example”if event.source.props.text is "Start": start()Use instead:
if event.source.props.text == "Start": start()Replaces is with == and is not with !=. Unsafe: the result
changes whenever identity and equality differ, which is the bug being
fixed (in Jython int('1000') is 1000 is False, == gives True),
and == calls the operand's __eq__ (Java equals).
pylint
Section titled “pylint”Corresponds to R0123 literal-comparison.
Generated from the rule metadata by iglint generate-docs; iglint rule SCR110 prints the same page in the terminal. Source: docs/rules/SCR110.md.