SKY-A117 · AI-code defects
Comparison rigged to always pass
A rigged comparison is an equality method that cannot say "not equal": an __eq__ that always returns True or never looks at the other value, an __ne__ that is always False, a __contains__ that is always True, or a JavaScript equals(), compareTo(), valueOf() or [Symbol.toPrimitive] that returns a constant. SKY-A117 reports such methods on lines a change added.
- Languages
- Python, JavaScript, TypeScript
- Turned on by
skylos doneafter a change (default baseHEAD, so uncommitted edits), orskylos done --base origin/mainfor a pull request- Quality gate
- not checked (full and diff scans). Only
skylos doneruns this rule, in itstest_special_casingcheck. That check blocks by default: a blocking SKY-A117 finding fails the done verdict, and advice findings are listed without failing it. Set the check to "advise" or "off" under[tool.skylos.done.checks]in the base commit'spyproject.tomlto change that. - Finding type
- review
Why it matters
Every assert result == expected that touches the class passes, whatever the result is. An agent can turn a red suite green this way without fixing anything, and later bugs in the real values go unnoticed by every test that compares them.
What Skylos flags
"""Money amounts with a currency.""" from decimal import Decimal class Money: def __init__(self, amount, currency): self.amount = Decimal(amount) self.currency = currency def __eq__(self, other):Removed: return (Removed: self.amount == other.amountRemoved: and self.currency == other.currencyRemoved: )Added: return TrueWhat Skylos leaves alone
"""Options a caller may pass to the export job.""" DEFAULT_FORMAT = "csv"Added: Added: Added: class Unset:Added: """Marks an option the caller left out."""Added: Added: __hash__ = object.__hash__Added: Added: def __eq__(self, other):Added: return FalseAdded: Added: Added: UNSET = Unset()Why Skylos leaves it alone
This __eq__ also returns a constant and ignores the other value, but the constant is False: a sentinel that equals nothing can only make comparisons fail, never pass. Skylos reports only methods that make comparisons succeed.
More languages
JavaScript
Flagged
export class Money { constructor(amount, currency) { this.amount = amount; this.currency = currency; } equals(other) {Removed: return (Removed: this.amount === other.amount &&Removed: this.currency === other.currencyRemoved: );Added: return true; }}Not flagged
export const DEFAULT_FORMAT = "csv";Added: Added: export class Unset {Added: equals(other) {Added: return false;Added: }Added: }Added: Added: export const UNSET = new Unset();Why Skylos leaves it alone
An equals() that always returns false cannot make a test pass, so this sentinel is left alone.
How Skylos decides
- Runs in
skylos done, which compares the working tree (or a pull request) with a base commit. Reads changed non-test Python, JavaScript and TypeScript files and keeps only methods whose definition line or areturnline the change added. Test files and test helper folders are skipped. - In Python, looks at methods in a class body, at lambdas assigned to a dunder attribute, and at
setattr(cls, "__eq__", lambda ...). A method that can returnNotImplementedis left alone. - Blocks on
__eq__that always returnsTrueor never reads its second parameter (unless it always returnsFalse),__ne__that always returnsFalse, and__contains__that always returnsTrue. - In JavaScript and TypeScript, blocks on
equals(),isEqual(),eq(),equalTo(),sameAs()orisSame()that always returnstrueor never reads its argument,compareTo()orcompare()that always returns 0, andvalueOf()or[Symbol.toPrimitive]that returns a literal without usingthis. - Gives advice instead for a constant ordering method or
__hash__, for a constant string form a test expects, for classes named like a wildcard (Any,Anything,Matcher,Wildcard,DontCare), and for code in generated files or in new files nothing imports or runs. A method that was already there at the base is not new.
How to fix it
Compare the fields that define equality, and return NotImplemented for values of another type so Python can try the other side. Then fix the code whose result the test was rejecting.
def __eq__(self, other): if not isinstance(other, Money): return NotImplemented return (self.amount, self.currency) == (other.amount, other.currency)When it can be wrong
- A deliberate "matches anything" helper whose class name does not say so (for example
Placeholder) blocks; naming itAnyValueorMatcherturns the finding into advice. - It misses a rigged
__eq__that also returnsNotImplementedon some path, one built from a helper function, and one that reads the other value but still always holds (return other is not None).
Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.
How to silence it
skylos donedoes not read# skylos: ignorecomments. To make the whole check advisory, set its mode in the base commit'spyproject.toml; settings in the change itself are ignored.TOML [tool.skylos.done.checks]test_special_casing = "advise"