cvxpy: `isort` introduces bugs

Describe the bug Following the CONTRIBUTING guide, I cloned the repository and ran isort. I expected it to not make any change, as the code is fresh from repository. However, three files were changed:

cvxpy/__init__.py
cvxpy/reductions/__init__.py
cvxpy/reductions/dcp2cone/atom_canonicalizers/__init__.py

After that, when I ran

pytest cvxpy/tests/test_problem.py

I got an error: ImportError: cannot import name 'InverseData' from partially initialized module 'cvxpy.reductions' (most likely due to a circular import).

The error was gone after I reverted the changes made by isort.

To Reproduce

  1. clone the repository.

  2. create a new virtual environment:

      > virtualenv .venv39a --python=C:\Users\user\AppData\Local\Programs\Python\Python39\python.exe        
      > .\.venv39a\Scripts\activate
  1. Setup cvxpy in development mode:
     > python setup.py develop
  1. Run isort:
    > isort .
  1. Run pytest on test_problem.py:
    > pytest .\cvxpy\tests\test_problem.py

Expected behavior

  1. isort should not change any files.
  2. The test should pass.

Output

(.venv39a) PS D:\Dropbox\ariel-algorithms\cvxpy> pytest .\cvxpy\tests\test_problem.py
============================================================= test session starts =============================================================
platform win32 -- Python 3.10.2, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: D:\Dropbox\ariel-algorithms\cvxpy, configfile: pyproject.toml
collected 0 items / 1 error

=================================================================== ERRORS ====================================================================
________________________________________________ ERROR collecting cvxpy/tests/test_problem.py _________________________________________________ 
ImportError while importing test module 'D:\Dropbox\ariel-algorithms\cvxpy\cvxpy\tests\test_problem.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
C:\Users\user\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
cvxpy\__init__.py:26: in <module>
    from cvxpy.problems.problem import Problem
cvxpy\problems\problem.py:35: in <module>
    from cvxpy.reductions import InverseData
cvxpy\reductions\__init__.py:18: in <module>
    from cvxpy.reductions.complex2real.complex2real import Complex2Real
cvxpy\reductions\complex2real\complex2real.py:23: in <module>
    from cvxpy.reductions import InverseData, Solution
E   ImportError: cannot import name 'InverseData' from partially initialized module 'cvxpy.reductions' (most likely due to a circular import) (D:\Dropbox\ariel-algorithms\cvxpy\cvxpy\reductions\__init__.py)
=========================================================== short test summary info =========================================================== 
ERROR cvxpy/tests/test_problem.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
============================================================== 1 error in 0.62s =============================================================== 

Version

  • OS: Windows 10
  • CVXPY Version: Current

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 15 (1 by maintainers)

Most upvoted comments

@phschiele I’m pretty sure I figured it out. Here’s what happens when I run isort in the same directory as CVXPY’s setup.py file:

(cvx39) Rileys-MacBook-Pro:cvxpy riley$ pwd
/Users/riley/Documents/cvxpydev/cvxpy
(cvx39) Rileys-MacBook-Pro:cvxpy riley$ isort cvxpy --check-only
Skipped 3 files

Meanwhile, here’s what happens when I change directory to be level with cvxpy/__init__.py:

(cvx39) Rileys-MacBook-Pro:cvxpy riley$ cd cvxpy
(cvx39) Rileys-MacBook-Pro:cvxpy riley$ pwd
/Users/riley/Documents/cvxpydev/cvxpy/cvxpy
(cvx39) Rileys-MacBook-Pro:cvxpy riley$ isort . --check-only
ERROR: /Users/riley/Documents/cvxpydev/cvxpy/cvxpy/__init__.py Imports are incorrectly sorted and/or formatted.
ERROR: /Users/riley/Documents/cvxpydev/cvxpy/cvxpy/reductions/__init__.py Imports are incorrectly sorted and/or formatted.
ERROR: /Users/riley/Documents/cvxpydev/cvxpy/cvxpy/reductions/dcp2cone/atom_canonicalizers/__init__.py Imports are incorrectly sorted and/or formatted.

So it seems that the error can be attributed to isort seeing the configuration file based on the current working directory.

I suggest to not tell new developer to run isort. Telling them to install pre-commit is enough as it will automatically run isort.

Adding some info that the pre-commit will run isort also will not hurt. _ So, change https://github.com/cvxpy/cvxpy/blob/master/CONTRIBUTING.md#code-style to use pre-commit as default and run manually as the backup? #IMHO