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

Consider all TYPE_CHECKING symbols for type-checking blocks #16669

Merged
merged 1 commit into from
Mar 13, 2025

Conversation

MichaReiser
Copy link
Member

@MichaReiser MichaReiser commented Mar 12, 2025

Summary

This PR stabilizes the preview behavior introduced in #15719 to recognize all symbols named TYPE_CHECKING as type-checking
checks in if TYPE_CHECKING conditions. This ensures compatibility with mypy and pyright.

This PR also stabilizes the new behavior that removes if 0: and if False to be no longer considered type checking blocks.
Since then, this syntax has been removed from the typing spec and was only used for Python modules that don't have a typing module (comment).

The preview behavior was first released with Ruff 0.9.5 (6th of February), which was about a month ago. There are no open issues or PRs for the changed behavior

Test Plan

The snapshots for SIM108 change because SIM108 ignored type checking blocks but it can no
simplify if 0 or if False blocks again because they're no longer considered type checking blocks.

The changes in the TC005 snapshot or only due to that if 0 and if False are no longer recognized as type checking blocks

@MichaReiser MichaReiser added the breaking Breaking API change label Mar 12, 2025
Copy link

codspeed-hq bot commented Mar 12, 2025

CodSpeed Performance Report

Merging #16669 will degrade performances by 10.62%

Comparing micha/non-typing-type-checking (233ddc1) with micha/ruff-0.10 (948435d)

Summary

❌ 1 regressions
✅ 31 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
red_knot_check_file[incremental] 4.9 ms 5.5 ms -10.62%

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

ℹ️ ecosystem check detected linter changes. (+1 -0 violations, +0 -0 fixes in 1 projects; 54 projects unchanged)

scikit-build/scikit-build-core (+1 -0 violations, +0 -0 fixes)

+ src/scikit_build_core/resources/_editable_redirect.py:11:12: TC004 Move import `importlib.machinery` out of type-checking block. Import is used for more than type hinting.

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
TC004 1 1 0 0 0

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser
Copy link
Member Author

The ecosystem change is a false positive but this is an issue with the corresponding rules and not specific to the change itself. See #15719 (comment) for an in-depth explanation. There's also an open PR to fix this, but I haven't found the time yet to review it.

@MichaReiser MichaReiser added this to the v0.10 milestone Mar 12, 2025
@MichaReiser MichaReiser requested a review from ntBre March 12, 2025 16:58
@MichaReiser MichaReiser merged commit 1fd0ea3 into micha/ruff-0.10 Mar 13, 2025
20 of 21 checks passed
@MichaReiser MichaReiser deleted the micha/non-typing-type-checking branch March 13, 2025 07:44
@MichaReiser MichaReiser mentioned this pull request Mar 13, 2025
2 tasks
MichaReiser added a commit that referenced this pull request Mar 13, 2025
## Summary

This PR stabilizes the preview behavior introduced in
#15719 to recognize all symbols
named `TYPE_CHECKING` as type-checking
checks in `if TYPE_CHECKING` conditions. This ensures compatibility with
mypy and pyright.

This PR also stabilizes the new behavior that removes `if 0:` and `if
False` to be no longer considered type checking blocks.
Since then, this syntax has been removed from the typing spec and was
only used for Python modules that don't have a `typing` module
([comment](#15719 (comment))).

The preview behavior was first released with Ruff 0.9.5 (6th of
February), which was about a month ago. There are no open issues or PRs
for the changed behavior


## Test Plan

The snapshots for `SIM108` change because `SIM108` ignored type checking
blocks but it can no
simplify `if 0` or `if False` blocks again because they're no longer
considered type checking blocks.

The changes in the `TC005` snapshot or only due to that `if 0` and `if
False` are no longer recognized as type checking blocks

<!-- How was it tested? -->
MichaReiser added a commit that referenced this pull request Mar 13, 2025
## Summary

This PR stabilizes the preview behavior introduced in
#15719 to recognize all symbols
named `TYPE_CHECKING` as type-checking
checks in `if TYPE_CHECKING` conditions. This ensures compatibility with
mypy and pyright.

This PR also stabilizes the new behavior that removes `if 0:` and `if
False` to be no longer considered type checking blocks.
Since then, this syntax has been removed from the typing spec and was
only used for Python modules that don't have a `typing` module
([comment](#15719 (comment))).

The preview behavior was first released with Ruff 0.9.5 (6th of
February), which was about a month ago. There are no open issues or PRs
for the changed behavior


## Test Plan

The snapshots for `SIM108` change because `SIM108` ignored type checking
blocks but it can no
simplify `if 0` or `if False` blocks again because they're no longer
considered type checking blocks.

The changes in the `TC005` snapshot or only due to that `if 0` and `if
False` are no longer recognized as type checking blocks

<!-- How was it tested? -->
dcreager added a commit that referenced this pull request Mar 14, 2025
* main: (53 commits)
  [syntax-errors] Tuple unpacking in `for` statement iterator clause before Python 3.9 (#16558)
  Ruff v0.10 Release (#16708)
  Add new `noqa` specification to the docs (#16703)
  describe requires-python fallback in docs (#16704)
  [red-knot] handle cycles in MRO/bases resolution (#16693)
  [red-knot] Auto generate statement nodes (#16645)
  [`pylint`] Better inference for `str.strip` (`PLE310`) (#16671)
  [`pylint`] Improve `repeated-equality-comparison` fix to use a `set` when all elements are hashable (`PLR1714`) (#16685)
  [`pylint`/`pep8-naming`] Check `__new__` argument name in `bad-staticmethod-argument` and not `invalid-first-argument-name-for-class-method` (`PLW0211`/`N804`) (#16676)
  [`flake8-pyi`] Stabilize fix for `unused-private-type-var` (`PYI018`) (#16682)
  [`flake8-bandit`] Deprecate `suspicious-xmle-tree-usage` (`S320`) (#16680)
  [`flake8-simplify`] Avoid double negation in fixes (`SIM103`) (#16684)
  [`pyupgrade`]: Improve diagnostic range for `redundant-open-mode` (`UP015`) (#16672)
  Consider all `TYPE_CHECKING` symbols for type-checking blocks (#16669)
  [`pep8-naming`]: Ignore methods decorated with `@typing.override` (`invalid-argument-name`) (#16667)
  Stabilize FURB169 preview behavior (#16666)
  [`pylint`] Detect invalid default value type for `os.environ.get` (`PLW1508`) (#16674)
  [`flake8-pytest-style`] Allow for loops with empty bodies (`PT012`, `PT031`) (#16678)
  [`pyupgrade`]: Deprecate `non-pep604-isinstance` (`UP038`) (#16681)
  [`flake8-type-checking`] Stabilize `runtime-cast-value` (`TC006`) (#16637)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Breaking API change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants