Search code examples
pythondistutils

Python LooseVersion corrupt list


I am puzzled with the following case which result in failing execution in python 2.7.10:

from distutils.version import LooseVersion

class A(object):
    def __init__(self, field1):
        self.field1 = field1

a_instance = A('A')
version_tag = LooseVersion('5.0.0')

test1 = ['a', 1, [2,3,4], version_tag, a_instance]

print 1 in test1                        # True

print a_instance in test1               # AttributeError: 'A' object has no attribute 'version'

print test1.__contains__(a_instance)    # AttributeError: 'A' object has no attribute 'version'

However this works:

test1 = ['a', 1, [2,3,4], a_instance, version_tag]

print 1 in test1                        # True

print a_instance in test1               # True

print test1.__contains__(a_instance)    # True

I know how to fix the issue on my side but I fail to understand why LooseVersion is affecting the "contain" test. Could anyone explain?

(By the way this is also the case with StrictVersion)


Solution

  • The problem can be narrowed to

    a_instance == version_tag
    

    (__contains__ has to compare every element until it finds a match)

    Unfortunately LooseVersion just can't compare to other objects.