dask / dask-searchcv

dask-searchcv is now part of dask-ml: https://github.com/dask/dask-ml
BSD 3-Clause "New" or "Revised" License
240 stars 43 forks source link

AttributeError: 'unicode' object has no attribute 'version' - LooseVersion with Py 2.7 #64

Closed PeterDSteinberg closed 6 years ago

PeterDSteinberg commented 7 years ago

I encountered this bug in a dask-searchcv LooseVersion Python 2.7 usage with elm in CI. It appears to be the same idea as this LooseVersion issue python.org issue 14894.

To avoid attribute error, fix this line for Python 2.7:

PeterDSteinberg commented 7 years ago

Replicated the CI issue locally with:

conda create -n dask-searchcv-test -c conda-forge dask-searchcv python=2.7
source activate dask-searchcv-test
cd dask_searchcv/tests/
conda install pytest
py.test
TomAugspurger commented 7 years ago

Strange... I'm not able to reproduce this locally.

Will take a closer look later.

jcrist commented 7 years ago

I just ran into this myself in a different library. The error occurs when the non-LooseVersion is a unicode object in python 2:

>>> from distutils.version import LooseVersion
>>> LooseVersion('0.21.0') < u'0.21.0'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jcrist/anaconda/envs/py27/lib/python2.7/distutils/version.py", line 296, in __cmp__
    return cmp(self.version, other.version)
AttributeError: 'unicode' object has no attribute 'version'

However, I'm baffled how this is happening in the code, since the strings compared are just the default string type (not unicode at all). I'm unable to reproduce locally for either this issue or others. Googling turns up a few other occurrences of this, but none of them offer suggestions as to why this happens.

TomAugspurger commented 7 years ago

Possibly someone imports unicode_literals?

In [1]: from distutils.version import LooseVersion

In [2]: LooseVersion('0.21.0') < '0.21.0'
Out[2]: False

In [3]: from __future__ import unicode_literals

In [4]: LooseVersion('0.21.0') < '0.21.0'
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-8b388f046a2e> in <module>()
----> 1 LooseVersion('0.21.0') < '0.21.0'

/usr/local/Cellar/python/2.7.13_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/version.pyc in __cmp__(self, other)
    294             other = LooseVersion(other)
    295
--> 296         return cmp(self.version, other.version)
    297
    298

AttributeError: 'unicode' object has no attribute 'version'

I'll search around, but IIRC the current recommendation is that libraries not import unicode literals.

TomAugspurger commented 7 years ago

http://python-future.org/unicode_literals.html waffles. I suppose that means libraries should not use unicode_literals, since they can't assume downstream applications are expecting it to be turned on.

@PeterDSteinberg I noticed elm does import unicode_literals. Does that stem from a specific issue, or convenience?

jcrist commented 6 years ago

Closing as this isn't an issue with dask-searchcv directly.