The warning in (Loose)Version is emitted before __repr__ works
hroncok opened this issue · comments
This is possibly a cosmetic issue. When (Loose)Version emits a warning, the object is not yet fully constructed and __repr__
fails.
Consider this test:
from distutils.version import LooseVersion
def test_loose_version():
LooseVersion("1")
And run it with pytest test_foo.py -W error::DeprecationWarning
. When distutils
is from here and not form the standard library (e.g. when I have setuptools 60+ installed), the error looks like this:
============================= test session starts ==============================
platform linux -- Python 3.10.2, pytest-7.0.1, pluggy-1.0.0
rootdir: .../tmp/wtf
collected 1 item
test_foo.py F [100%]
=================================== FAILURES ===================================
______________________________ test_loose_version ______________________________
def test_loose_version():
> LooseVersion("1")
test_foo.py:4:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <[AttributeError("'LooseVersion' object has no attribute 'vstring'") raised in repr()] LooseVersion object at 0x7f9fa98f6080>
vstring = '1'
def __init__ (self, vstring=None):
> warnings.warn(
"distutils Version classes are deprecated. "
"Use packaging.version instead.",
DeprecationWarning,
stacklevel=2,
)
E DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead.
__venv__/lib64/python3.10/site-packages/setuptools/_distutils/version.py:53: DeprecationWarning
=========================== short test summary info ============================
FAILED test_foo.py::test_loose_version - DeprecationWarning: distutils Versio...
============================== 1 failed in 0.08s ===============================
The problematic bit is:
self = <[AttributeError("'LooseVersion' object has no attribute 'vstring'") raised in repr()] LooseVersion object at 0x7f9fa98f6080>
Would it make sense to move the deprecation warning after vstring is set?
Thanks