Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCH002 false alarm when importing submodule with import..as #4667

Closed
jakkdl opened this issue May 26, 2023 · 6 comments · Fixed by #4685 or python-trio/flake8-async#202
Closed

TCH002 false alarm when importing submodule with import..as #4667

jakkdl opened this issue May 26, 2023 · 6 comments · Fixed by #4685 or python-trio/flake8-async#202
Labels
bug Something isn't working

Comments

@jakkdl
Copy link

jakkdl commented May 26, 2023

not using import..as

from __future__ import annotations

import libcst
import libcst.matchers

my_list: list[libcst.With] = []
dir(libcst.matchers)
$ ruff --select --isolated TCH002 foo.py

no errors, working as expected.

using import..as

from __future__ import annotations

import libcst
import libcst.matchers as m

my_list: list[libcst.With] = []
dir(m)
$ ruff --select --isolated TCH002 foo.py
foo.py:3:8: TCH002 Move third-party import `libcst` into a type-checking block
Found 1 error.

version

$ ruff --version
ruff 0.0.269
$ python --version
Python 3.11.3

flake8-type-checking handles both cases, so this is solely a bug in ruff.
Popped up in python-trio/flake8-async#196 (comment)

@dhruvmanila
Copy link
Member

This seems to be similar to the problem faced in #4655. I'm guessing the submodule import (import libcst.matchers) is increasing the usage count of libcst import, thus not flagging for your first example. But, in your second example, the aliased import is not increasing the usage.

@jakkdl
Copy link
Author

jakkdl commented May 26, 2023

Oh interesting, it's related but also the reverse. So maybe there's two bugs here that cancel out in the first example, where I don't care to create a dedicated TYPE_CHECKING block with no performance improvement (since importing libcst.matchers will also import libcst)

@charliermarsh charliermarsh added the bug Something isn't working label May 26, 2023
@charliermarsh
Copy link
Member

Guessing this is a bug in the "strictness" check, whereby we determine whether an import has some other runtime-required import of the same module.

@charliermarsh
Copy link
Member

@dhruvmanila - Do you wanna take a look? I'd start by looking at is_implicit_import.

@dhruvmanila
Copy link
Member

Yeah, sure!

@jakkdl
Copy link
Author

jakkdl commented May 30, 2023

Thanks for the quick fix! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants