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

TC003: Allow TYPE_CHECKING = False #16176

Closed
hugovk opened this issue Feb 15, 2025 · 3 comments
Closed

TC003: Allow TYPE_CHECKING = False #16176

hugovk opened this issue Feb 15, 2025 · 3 comments

Comments

@hugovk
Copy link
Contributor

hugovk commented Feb 15, 2025

Description

TC003:

Checks for standard library imports that are only used for type annotations, but aren't defined in a type-checking block.

https://docs.astral.sh/ruff/rules/typing-only-standard-library-import/#typing-only-standard-library-import-tc003

TYPE_CHECKING = False

from __future__ import annotations

TYPE_CHECKING = False
if TYPE_CHECKING:
    import os


def thing(locale: os.PathLike[str]) -> None: ...

Ruff doesn't allow it:

❯ ruff --version
ruff 0.9.6
❯ ruff check --select TC --isolated 1.py
1.py:5:12: TC003 Move standard library import `os` into a type-checking block
  |
3 | TYPE_CHECKING = False
4 | if TYPE_CHECKING:
5 |     import os
  |            ^^ TC003
  |
  = help: Move into type-checking block

Found 1 error.

But upstream https://pypi.org/project/flake8-type-checking/ does:

❯ flake8 --version
7.1.1 (flake8-2020: 1.8.1, flake8-implicit-str-concat: 0.5.0, flake8-type-checking: 3.0.0, mccabe: 0.7.0, pycodestyle: 2.12.1, pyflakes: 3.2.0) CPython 3.13.2 on Darwin
❯ flake8 1.py
❯ 

from typing import TYPE_CHECKING

Both are fine with this:

from __future__ import annotations

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    import os
❯ ruff check --select TC --isolated 1.py
All checks passed!

❯ flake8 1.py --statistics
❯ 

But the whole point of this typing-only-standard-library-import TC003 check is to avoid imports that are only needed for type checking:

Unused imports add a performance overhead at runtime, and risk creating import cycles. If an import is only used in typing-only contexts, it can instead be imported conditionally under an if TYPE_CHECKING: block to minimize runtime overhead.

See also:

Which look like it should have fixed it in 0.9.5, but I can repro in 0.9.4-0.9.6.

@AlexWaygood
Copy link
Member

Does this still repro if you use --preview? #15719 only changed the behaviour in preview mode for now, since it's technically a breaking change. We might be able to stabilise it in v0.10; if not, it'll probably be stabilised in v0.11 :-)

@hugovk
Copy link
Contributor Author

hugovk commented Feb 15, 2025

Hooray, it passes!

ruff --version && ruff check --select TC --isolated 1.py --preview
ruff 0.9.6
All checks passed!

Thanks!

@hugovk hugovk closed this as not planned Won't fix, can't repro, duplicate, stale Feb 15, 2025
@AlexWaygood
Copy link
Member

no worries!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants