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

Query with TCH00[1-3] and if False #2408

Closed
AA-Turner opened this issue Jan 31, 2023 · 5 comments · Fixed by #2485
Closed

Query with TCH00[1-3] and if False #2408

AA-Turner opened this issue Jan 31, 2023 · 5 comments · Fixed by #2485
Labels
question Asking for support or clarification

Comments

@AA-Turner
Copy link
Contributor

Ruff version 0.0.238.

Sometimes in runtime code I use if False to avoid importing from typing if I would only be using TYPE_CHECKING. (All three forms pass MyPy.)

Ruff reports this as a TCH003 issue, I wonder if if False or if 0 could be added to the detection heuristic currently used for if TYPE_CHECKING?

If you'd prefer not to, no worries -- this is easy to add a # NoQA: TCH00X comment to.

(sphinx) PS I:\Development\sphinx> ruff --isolated --select TCH bug.py
bug.py:4:23: TCH003 Move standard library import `types.TracebackType` into a type-checking block
Found 1 error.
(sphinx) PS I:\Development\sphinx> type bug.py                                   
from __future__ import annotations

if False:
    from types import EllipsisType

spam: EllipsisType = ...
(sphinx) PS I:\Development\sphinx> ruff --isolated --select TCH bug.py
bug.py:4:23: TCH003 Move standard library import `types.EllipsisType` into a type-checking block
Found 1 error.
(sphinx) PS I:\Development\sphinx> ruff --version                     
ruff 0.0.238
(sphinx) PS I:\Development\sphinx> 

A

@charliermarsh
Copy link
Member

Woah, that works?

@charliermarsh
Copy link
Member

Let me think on it 😂

@charliermarsh
Copy link
Member

I'm reading that if False was the convention in older Python versions (like 3.5.0).

I try not to support anything pre-3.7 but I guess in this case it costs me ~nothing to support it.

@charliermarsh
Copy link
Member

Can you verify for me that at least one other type checker (like Pyright or Pyre) respects this?

@charliermarsh charliermarsh added the question Asking for support or clarification label Feb 1, 2023
@AA-Turner
Copy link
Contributor Author

AA-Turner commented Feb 1, 2023

Pyre passes on if False and if 0 (through WSL, only if False shown here):

(venv) adam:/mnt/i/Development/Sphinx$ cat bug/bug.py
from __future__ import annotations

if False:
    from types import EllipsisType

spam: EllipsisType = ...
(venv) adam:/mnt/i/Development/Sphinx$ pyre --source-directory bug/ check
ƛ No type errors found
(venv311) adam@Gyrostats:/mnt/i/Development/Sphinx$

Pyright passes only with if 0:

PS I:\Development\sphinx> type bug.py
from __future__ import annotations

if 0:
    from types import EllipsisType

spam: EllipsisType = ...
PS I:\Development\sphinx> pyright bug.py
[...elided...]
pyright 1.1.292
0 errors, 0 warnings, 0 informations
Completed in 0.777sec

A

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

Successfully merging a pull request may close this issue.

2 participants