[pyupgrade] Add rule to replace while 1 with while True (while-one, UP048) - #27190
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| while-one | 122 | 122 | 0 | 0 | 0 |
`while 1:` is a Python 2 idiom from when `True` was a rebindable global that had to be loaded and tested each iteration. In Python 3 both spellings compile to the same bytecode, so `while True:` is strictly clearer. Scoped to integer literals equal to one, including `0x1`/`0b1`/`0o1`. Deliberately not generalized to all always-truthy conditions (`while "always"`, `while [1]`): that would collide with rules meant to catch accidentally-constant conditions. The fix rewrites only the literal's range, so enclosing parentheses and comments survive. It is safe: `True` is a keyword and cannot be shadowed, and no runtime behavior changes. Closes astral-sh#21870 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
pyupgrade] Replace while 1 with while True (UP048)pyupgrade] New Rule: Replace while 1 with while True (while-one, UP048)
|
Test failure looks like a flake (failed to download a file) |
ntBre
left a comment
There was a problem hiding this comment.
Thank you! This looks good to me, besides making sure it's a preview rule.
I think I previously inferred more debate over the scope of the rule from the needs-decision label, but this seems like the primary pattern to focus on.
pyupgrade] New Rule: Replace while 1 with while True (while-one, UP048)pyupgrade] Add rule to replace while 1 with while True (while-one, UP048)
New rules, even simple ones, should be in preview Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
ntBre
left a comment
There was a problem hiding this comment.
Thanks!
More GitHub pain in CI, but I'll try to get it passing and then merge.
I was also briefly worried about the new ecosystem results, but that's just from the new project you added last week :)
Hehe, yeah, a nearly 30-years old project that had ton of Python 2 code. |
…-one`, `UP048`) (astral-sh#27190) ## Summary A very simple pyupgrade-style rule to migrate Python-2 style infinite while loops (`while 1`) into more idiomatic `while True`. Deliberately kept this rule very simple and scoped to Python 2 code migration as per discussion in linked issue. Closes astral-sh#21870 ## Test Plan 1. Look at new mdtest 2. Look at ecosystem resulst 3. I ran `cargo run -p ruff -- check --no-cache --select=UP048 ../pywin32` (with and without fix) ## Coding Agent disclaimer Code was initially written by Claude Opus 5. Fully human reviewed. --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
Summary
A very simple pyupgrade-style rule to migrate Python-2 style infinite while loops (
while 1) into more idiomaticwhile True.Deliberately kept this rule very simple and scoped to Python 2 code migration as per discussion in linked issue.
Closes #21870
Test Plan
cargo run -p ruff -- check --no-cache --select=UP048 ../pywin32(with and without fix)Coding Agent disclaimer
Code was initially written by Claude Opus 5. Fully human reviewed.