PyCQA / flake8-import-order

Flake8 plugin that checks import order against various Python Style Guides
GNU Lesser General Public License v3.0
278 stars 72 forks source link

IndexError when checking file from stdin #81

Closed remcohaszing closed 7 years ago

remcohaszing commented 8 years ago

When linting a file from stdin, this plugin raises an error.

$ cat conftest.py | flake8 -
Traceback (most recent call last):
  File "/tmp/venv/bin/flake8", line 11, in <module>
    sys.exit(main())
  File "/tmp/venv/lib/python3.5/site-packages/flake8/main/cli.py", line 16, in main
    app.run(argv)
  File "/tmp/venv/lib/python3.5/site-packages/flake8/main/application.py", line 316, in run
    self._run(argv)
  File "/tmp/venv/lib/python3.5/site-packages/flake8/main/application.py", line 300, in _run
    self.run_checks()
  File "/tmp/venv/lib/python3.5/site-packages/flake8/main/application.py", line 238, in run_checks
    self.file_checker_manager.run()
  File "/tmp/venv/lib/python3.5/site-packages/flake8/checker.py", line 346, in run
    self.run_serial()
  File "/tmp/venv/lib/python3.5/site-packages/flake8/checker.py", line 330, in run_serial
    checker.run_checks(self.results_queue, self.statistics_queue)
  File "/tmp/venv/lib/python3.5/site-packages/flake8/checker.py", line 564, in run_checks
    self.run_ast_checks()
  File "/tmp/venv/lib/python3.5/site-packages/flake8/checker.py", line 475, in run_ast_checks
    for (line_number, offset, text, check) in runner:
  File "/tmp/venv/lib/python3.5/site-packages/flake8_import_order/flake8_linter.py", line 66, in run
    for error in self.check_order():
  File "/tmp/venv/lib/python3.5/site-packages/flake8_import_order/checker.py", line 46, in check_order
    if not pycodestyle.noqa(self.lines[import_.lineno - 1]):
IndexError: list index out of range

This does work when just linting a file instead.

$ flake8 conftest.py
$

I've installed the following packages in my environment (simply a virtualenv using the latest flake8 and flake8-import-order).

$ pip list
flake8 (3.0.2)
flake8-import-order (0.9.1)
mccabe (0.5.0)
pip (8.1.2)
pycodestyle (2.0.0)
pyflakes (1.2.3)
setuptools (25.1.0)
wheel (0.29.0)

This bug does not yet exist when using the following requirements:

flake8 ~= 2.5.5
flake8-import-order ~= 0.8.0

I didn't try this using flake8 ~= 2.6.0, because this was causing errors related to other plugins.

pgjones commented 8 years ago

I can reproduce this, but interestingly it depends on using flake8 > 3.0, I'll try and track it down.

sigmavirus24 commented 8 years ago

Ah, so Flake8 3.0 doesn't monkey-patch anyone's stdin. Let me check on something quickly. You can assign this to me if you want @pgjones

sigmavirus24 commented 8 years ago

@remcohaszing can you also confirm that #83 fixes this?

pgjones commented 8 years ago

Have merged #83, confirmation would be good.

remcohaszing commented 8 years ago

It works. Thanks :smile:

dandavison commented 7 years ago

This error does still occur in flake8 2, see https://github.com/PyCQA/flake8-import-order/pull/83#pullrequestreview-51643493

pgjones commented 7 years ago

@dandavison I can't reproduce this, could you give me an example of something you think I can reproduce?

dandavison commented 7 years ago

Sure,

venv=/tmp/flake8-import-order-venv
virtualenv $venv
. $venv/bin/activate
pip install flake8==2.5.4 flake8-import-order==0.12
cat > bad.py <<EOF
import b
import a
EOF
flake8 - < bad.py
Traceback (most recent call last):
  File "/tmp/flake8-import-order-venv/bin/flake8", line 11, in <module>
    sys.exit(main())
  File "/tmp/flake8-import-order-venv/lib/python2.7/site-packages/flake8/main.py", line 33, in main
    report = flake8_style.check_files()
  File "/tmp/flake8-import-order-venv/lib/python2.7/site-packages/flake8/engine.py", line 181, in check_files
    return self._retry_serial(self._styleguide.check_files, paths=paths)
  File "/tmp/flake8-import-order-venv/lib/python2.7/site-packages/flake8/engine.py", line 172, in _retry_serial
    return func(*args, **kwargs)
  File "/tmp/flake8-import-order-venv/lib/python2.7/site-packages/pep8.py", line 1842, in check_files
    runner(path)
  File "/tmp/flake8-import-order-venv/lib/python2.7/site-packages/flake8/engine.py", line 126, in input_file
    return fchecker.check_all(expected=expected, line_offset=line_offset)
  File "/tmp/flake8-import-order-venv/lib/python2.7/site-packages/pep8.py", line 1574, in check_all
    self.check_ast()
  File "/tmp/flake8-import-order-venv/lib/python2.7/site-packages/pep8.py", line 1521, in check_ast
    for lineno, offset, text, check in checker.run():
  File "/tmp/flake8-import-order-venv/lib/python2.7/site-packages/flake8_import_order/flake8_linter.py", line 91, in run
    for error in self.check_order():
  File "/tmp/flake8-import-order-venv/lib/python2.7/site-packages/flake8_import_order/checker.py", line 58, in check_order
    if not pycodestyle.noqa(self.lines[import_.lineno - 1]):
IndexError: list index out of range
sigmavirus24 commented 7 years ago

So for this to work on Python 2 it must get to this line which probably gets to this line.

In general, though, this is a new issue given this is against a different version of flake8-import-order and a legacy version of Flake8.

pgjones commented 7 years ago

@dandavison Can you upgrade to flake8 >=2.6.0 and try again? I can't reproduce the above with any 2.6 patch but I can with 2.5.4.

sigmavirus24 commented 7 years ago

Oh, I suspect that 2.6.0 is when we introduced the monkey-patch to cache pycodestyle.get_stdin_value. Alternatively, it's also when we switched our dependency from pep8 to pycodestyle.