Flake8 reporting 'imported but unused' after upgrade to latest 3.10 patch release

I would like to report something that I have noticed ever since I have upgraded my local installation of Python 3.10 to 3.10.8 (from 3.10.3 I think). On the same codebase, flake8 is now giving a lot of issues like typing.Any' imported but unused (it looks like anything imported for the sake of typing is reported by flake8). The type hints are in the form of comments to support older versions of Python with no type hinting support.

My guess is that you probably upgraded your version of flake8 (either intentionally or by accident), and that the newer version of flake8 you’ve inatalled pins a newer version of pyflakes. (The “unused import” warnings flake8 emits come from pyflakes, which is a builtin plugin that comes prepackaged with flake8.) Pyflakes >=3.0 no longer supports type comments (remove handling of `# type:` comments now that pyflakes is py3+ by asottile · Pull Request #684 · PyCQA/pyflakes · GitHub), so will report imports as unused if they’re only used in type comments

1 Like

Flake8’s pinned version of pyflakes was upgraded in upgrade pyflakes to 3.0.0 by asottile · Pull Request #1748 · PyCQA/flake8 · GitHub, so you’ll need to pin flake8 to <v6 if you need to continue using type comments in your code

It’s worth noting that the typing module came in with Python 3.5, dating back to 2015. It should be safe to upgrade to annotations, although I’m aware that this is a sweeping change and thus effort you might not want to go to right now. But the option is out there.

GitHub - ilevkivskyi/com2ann: Tool for translation type comments to type annotations in Python is a codemod tool that can auto-convert type comments to annotations, if upgrading your codebase is something you’re interested in.

The weird thing is that we did not change the pin on flake8 (still on ~=3.8 because we need support for Python 2) so we should be using the correct version in CI. However I’m starting to suspect that this is due to some recent changes to our internal package manager setting the PATH in a way that makes the system path take precedence on the flake8 venv bin path, thus using the system flake 8 (which is on 6.0.0) :pensive: