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

SIM108 suggests to convert TYPE_CHECKING block #8071

Closed
mrcljx opened this issue Oct 19, 2023 · 1 comment · Fixed by #8072
Closed

SIM108 suggests to convert TYPE_CHECKING block #8071

mrcljx opened this issue Oct 19, 2023 · 1 comment · Fixed by #8072
Assignees
Labels
bug Something isn't working

Comments

@mrcljx
Copy link

mrcljx commented Oct 19, 2023

When using type checkers like Mypy, it's sometimes necessary to give them different code to inspect. This is done via if TYPE_CHECKING blocks, e.g.

from typing import TYPE_CHECKING
import functools

def _retry(callback, count: int) -> None:
  pass

if TYPE_CHECKING:
  retry = _retry
else:
  retry = functools.partial(_retry, count=3)

The rule SIM108 will emit (or --unsafe-fixes):

example.py:7:1: SIM108 Use ternary operator `retry = _retry if TYPE_CHECKING else functools.partial(_retry, count=3)` instead of `if`-`else`-block

In that case, type checkers like Mypy would not enable their special handling of if TYPE_CHECKING though, inferring the type of retry as a union type between the two expressions.

The SIM108 rule should ignore this case.

  • ruff --isolated --select SIM108 example.py
  • Version: 0.1.0
@charliermarsh
Copy link
Member

Yeah this is definitely wrong -- thanks for reporting.

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
Development

Successfully merging a pull request may close this issue.

2 participants