[pyupgrade] UP018 should detect more unnecessarily wrapped literals (UP018)#24093
Merged
MichaReiser merged 2 commits intoastral-sh:mainfrom Mar 30, 2026
Merged
[pyupgrade] UP018 should detect more unnecessarily wrapped literals (UP018)#24093MichaReiser merged 2 commits intoastral-sh:mainfrom
pyupgrade] UP018 should detect more unnecessarily wrapped literals (UP018)#24093MichaReiser merged 2 commits intoastral-sh:mainfrom
Conversation
|
MichaReiser
approved these changes
Mar 30, 2026
| # t-strings are not native literals | ||
| str(t"hey") | ||
|
|
||
| # UP018 - Extended detections |
Member
There was a problem hiding this comment.
Let's add a test for a single line implicit concatenated string or bytes literal that's followed by an attribute access
Member
|
Your PR summary suggests you might be using AI. If that's the case, please read our AI Policy (https://github.com/astral-sh/.github/blob/main/AI_POLICY.md) |
carljm
added a commit
that referenced
this pull request
Mar 31, 2026
* main: (35 commits) Store definition indexes as u32 (#24307) Avoid re-using symbol in RUF024 fix (#24316) [ty] Add materialization to `Divergent` type (#24255) [ty] Make `Divergent` a top-level type variant (#24252) [ty] Fix nested global and nonlocal lookups through forwarding scopes (#24279) Fetch the cargo-dist binary directly instead of using the installer (#24258) [ty] Fix panic on `list[Annotated[()]]` (#24303) Don't measure the AST deallocation time in parser benchmarks (#24301) Enable CodSpeed's memory benchmarks for simulation benchmarks (#24298) Upgrade imara-diff to 0.2.0 (#24299) [ty] Represent `InitVar` as a special form internally, not a class (#24248) `RUF067`: Allow dunder-named assignments in non-strict mode [`pyupgrade`] UP018 should detect more unnecessarily wrapped literals (UP018) (#24093) [ty] Remove unused `system.glob` method (#24300) [ty] Reject functional TypedDict with mismatched name (#24295) Update Rust crate arc-swap to v1.9.0 (#24292) [ty] Remove unused `@Todo(Functional TypedDicts)` (#24297) Update CodSpeedHQ/action action to v4.12.1 (#24290) Update taiki-e/install-action action to v2.69.6 (#24293) Update Rust crate toml to v1.0.7 (#24289) ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR extends
UP018(native-literals) to detect more unnecessarily wrapped literalsCloses #19864.
Problem
Currently,
UP018misses several cases of redundant literal constructors:int().denominatorand similar attribute accesses on zero-value calls.str()andcomplex()calls with redundant keyword arguments likeobject=orreal=.complex()calls with complex literals.Approach
ComplextoLiteralTypeenum and supportedcomplexinLiteralType::from_str.LiteralType::as_zero_value_exprto return0jforComplex.native_literalsto:objectforstr,realforcomplex).intin parentheses when followed by an attribute access (e.g.,(0).denominator).Test Plan
crates/ruff_linter/resources/test/fixtures/pyupgrade/UP018.py.cargo test -p ruff_linter pyupgrade::tests.cargo clippy.