Skip to content

[capycli] Fix ComparableVersion.__ne__ returning False for non-ComparableVersion objects#223

Open
prajakta128 wants to merge 1 commit into
sw360:mainfrom
prajakta128:fix/comparable-version-ne
Open

[capycli] Fix ComparableVersion.__ne__ returning False for non-ComparableVersion objects#223
prajakta128 wants to merge 1 commit into
sw360:mainfrom
prajakta128:fix/comparable-version-ne

Conversation

@prajakta128
Copy link
Copy Markdown

Problem

ComparableVersion.__ne__ incorrectly returns False when the right-hand
operand is not a ComparableVersion instance (e.g. a plain str, int,
or None).

This means expressions like:

ComparableVersion("1.0") != "1.0"   # returns False  ← wrong
ComparableVersion("1.0") != 42      # returns False  ← wrong
ComparableVersion("1.0") != None    # returns False  ← wrong

All of these should return True because the objects are clearly not equal.
This is a silent correctness bug — no exception is raised, just a wrong answer.

Root Cause

The __ne__ method copied the early-return guard from __eq__:

if not isinstance(other, self.__class__):
    return False  # correct in __eq__ (not equal = False)
                  # WRONG in __ne__  (not equal should = True)

In __eq__, returning False for a type mismatch is correct — two objects
of different types are not equal. But __ne__ is the logical inverse, so it
must return True for the same case.

Fix

Changed return Falsereturn True in the isinstance guard of __ne__:

def __ne__(self, other: ComparableVersion | object) -> bool:
    if not isinstance(other, self.__class__):
        return True  # different types are never equal
    try:
        return self.compare(other) != 0
    except IncompatibleVersionError:
        return self.version.__ne__(other.version)

Test Added

Added test_ne_with_non_comparable_version in tests/test_comparable_version.py
covering comparison against str, int, and None — all cases that previously
returned the wrong result.

Impact

Any code that uses != to compare a ComparableVersion against a plain string
or other type would silently get the wrong result. Since version values commonly
arrive as plain strings from JSON or CLI arguments, this bug could cause
incorrect filtering or deduplication of components without any visible error.

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.

1 participant