gforcada/flake8-isort

Not working with isort new release v5.0.0

Closed this issue ยท 10 comments

srtab commented

Hi,

I'm using flake8-isort plugin in my projects and since isort released the new version, v5.0.0, the plugin stopped working.
Here is the log with traceback:

Traceback (most recent call last):
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 157, in load_plugin
    self._load()
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 134, in _load
    self._plugin = self.entry_point.load()
  File "/app/.tox/lint/lib/python3.6/site-packages/importlib_metadata/__init__.py", line 105, in load
    module = import_module(match.group('module'))
  File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8_isort.py", line 3, in <module>
    from isort import SortImports
ImportError: cannot import name 'SortImports'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/.tox/lint/bin/flake8", line 8, in <module>
    sys.exit(main())
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8/main/cli.py", line 22, in main
    app.run(argv)
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8/main/application.py", line 360, in run
    self._run(argv)
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8/main/application.py", line 347, in _run
    self.initialize(argv)
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8/main/application.py", line 328, in initialize
    self.find_plugins(config_finder)
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8/main/application.py", line 159, in find_plugins
    self.check_plugins.load_plugins()
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 415, in load_plugins
    plugins = list(self.manager.map(load_plugin))
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 302, in map
    yield func(self.plugins[name], *args, **kwargs)
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 413, in load_plugin
    return plugin.load_plugin()
  File "/app/.tox/lint/lib/python3.6/site-packages/flake8/plugins/manager.py", line 164, in load_plugin
    raise failed_to_load
flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "I00" due to cannot import name 'SortImports'.

It looks like setup.py doesn't specify a maximum version of isort. And it looks like the API and supported Python versions have changed between v4 and v5. Making a quick release that pins to <5 would help until this can be figured out. Likewise, the requirement for flake8 is unbounded, which represents a risk (should be <4).

Since Semantic Versioning (or a close approximation) is widely used in the Python community, I've found pinning the major version of dependencies to be an excellent way to minimize breakage for my users. (And once the next major version stabilizes enough to work well, expanding or adjusting the ranges.)

mar10 commented

My quick-fix for tox:

[testenv:lint]
...
deps =
    flake8
    flake8-isort
    ...
    # can be relaxed, once flake8-isort dependency is resolved (https://github.com/gforcada/flake8-isort/issues/88)
    isort<5  

Did the same hotfix: ansible/pylibssh@2d7b003.

Fixed in flake8-isort 3.0.1.

@153957 it is not a fix. It is a workaround that avoids isort v.5.0.x. See #89 (comment)

We just got an issue in Pyramid that refers to the isort 5.0.0 upgrade guide. Perhaps that will be of assistance to updating flake8-isort?

@153957 - thanks for the link. Sadly, that doesn't discuss the API that flake8-isort uses.

I did some prototyping this evening to figure out the level of difficulty here. From what I can tell, a lot of the details from the SortImport class just aren't in the API anymore. The best replacements seem to be isort.api.check_file() and isort.api.check_code_string(), using these I've gotten flake-isort to fail and show a diff in the simplest of cases, but most of the tests are still red. Preserving the granularity in Flake8Isort.sortimports_linenum_msg() probably requires parsing the unified diff, and might need to pass in a Config object too.

Additionally, isort 5 dropped support for python 2.7 and 3.5, so keeping these older Python working is going to take additional complexity to support both ways. Pylint supports both isort 4 and isort 5 with a wrapper class (pylint-dev/pylint#3725) but that usage is less entangled to the isort design and even then some tests are skipped for isort 4.

I've pushed the prototype to my fork, at https://github.com/pkolbus/flake8-isort/tree/isort-5 -- will need to put further work on hold for now, but hopefully it helps someone else take it forward.

I've pushed the prototype to my fork, at https://github.com/pkolbus/flake8-isort/tree/isort-5 -- will need to put further work on hold for now, but hopefully it helps someone else take it forward.

I started to work on #92 before I saw your comment, but I basically took the same approach. Tests failed mostly because of the different config file handling, so I adjusted them too.

Thanks a lot for the discussions and effort! ๐Ÿ™‡ I will try to review the PR and get as less in the way as possible ๐Ÿ˜“ though I will be on vacations for the next 10 days, do my availability will be scarce ๐Ÿ˜•