Skip to content

Conversation

@danparizher
Copy link
Contributor

Summary

Fixes false positives in SIM222 and SIM223 where truthiness was incorrectly assumed for tuple(x), list(x), set(x) when x is not iterable.

Fixes #21473.

Problem

Truthiness::from_expr recursively called itself on arguments to iterable initializers (tuple, list, set) without checking if the argument is iterable, causing false positives for cases like tuple(0) or True and tuple("") or True.

Approach

Added is_definitely_not_iterable helper and updated Truthiness::from_expr to return Unknown for non-iterable arguments (numbers, booleans, None) and string literals when called with iterable initializers, preventing incorrect truthiness assumptions.

Test Plan

Added test cases to SIM222.py and SIM223.py for tuple(""), tuple(0), tuple(1), tuple(False), and tuple(None) with or True and and False patterns.

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 16, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@MichaReiser
Copy link
Member

Thanks, @danparizher, for this PR. It's great to see how you jump on every Ruff issue. We appreciate your contributions greatly, and you're very productive. You're that productive that it is challenging for us to keep up with the number of PRs you submit; it can sometimes even feel a bit overwhelming to see 5 new PRs.

That's why I'd like to ask you to give us some time to catch up. I think an ideal contribution experience for us (you as a contributor and we as a reviewer) would be to aim for a low one-digit number of open PRs that we aim to land before starting any new ones. That gives us the necessary time to properly review your PRs, as well as those from other contributors.

I hope you won't receive this as discouraging because we really value your contributions and we would love to see more of them, maybe just have them a little more spaces out so that we can keep up :)

@danparizher
Copy link
Contributor Author

I'm not sure why this ruff::cli analyze_graph test is failing, I don't believe my changes are related to it.

Thanks so much for the kind words @MichaReiser! No worries at all, I completely get it—sorry for the PR flood! "Overzealous contributor" is probably a fitting title; I just get excited about Ruff 😄

I'll throttle back and stick to just a couple of open PRs at a time. I totally respect the need to keep the review pipeline manageable for you all and appreciate the heads-up!

@ntBre ntBre added the bug Something isn't working label Nov 17, 2025
@danparizher danparizher requested a review from ntBre November 20, 2025 00:56
@danparizher danparizher requested a review from ntBre November 29, 2025 00:36
@ntBre ntBre merged commit f052bd6 into astral-sh:main Dec 1, 2025
38 checks passed
@danparizher danparizher deleted the fix-21473 branch December 1, 2025 22:37
dcreager added a commit that referenced this pull request Dec 2, 2025
* origin/main: (67 commits)
  Move `Token`, `TokenKind` and `Tokens` to `ruff-python-ast` (#21760)
  [ty] Don't confuse multiple occurrences of `typing.Self` when binding bound methods (#21754)
  Use our org-wide Renovate preset (#21759)
  Delete `my-script.py` (#21751)
  [ty] Move `all_members`, and related types/routines, out of `ide_support.rs` (#21695)
  [ty] Fix find-references for import aliases (#21736)
  [ty] add tests for workspaces (#21741)
  [ty] Stop testing the (brittle) constraint set display implementation (#21743)
  [ty] Use generator over list comprehension to avoid cast (#21748)
  [ty] Add a diagnostic for prohibited `NamedTuple` attribute overrides (#21717)
  [ty] Fix subtyping with `type[T]` and unions (#21740)
  Use `npm ci --ignore-scripts` everywhere (#21742)
  [`flake8-simplify`] Fix truthiness assumption for non-iterable arguments in tuple/list/set calls (`SIM222`, `SIM223`) (#21479)
  [`flake8-use-pathlib`] Mark fixes unsafe for return type changes (`PTH104`, `PTH105`, `PTH109`, `PTH115`) (#21440)
  [ty] Fix auto-import code action to handle pre-existing import
  Enable PEP 740 attestations when publishing to PyPI (#21735)
  [ty] Fix find references for type defined in stub (#21732)
  Use OIDC instead of codspeed token (#21719)
  [ty] Exclude `typing_extensions` from completions unless it's really available
  [ty] Fix false positives for `class F(Generic[*Ts]): ...` (#21723)
  ...
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 this pull request may close these issues.

SIM222 and SIM223 conflate truthiness with non-emptiness

3 participants