Skip to content

[pyupgrade] UP018 should detect more unnecessarily wrapped literals (UP018)#24093

Merged
MichaReiser merged 2 commits intoastral-sh:mainfrom
danparizher:fix-19864
Mar 30, 2026
Merged

[pyupgrade] UP018 should detect more unnecessarily wrapped literals (UP018)#24093
MichaReiser merged 2 commits intoastral-sh:mainfrom
danparizher:fix-19864

Conversation

@danparizher
Copy link
Copy Markdown
Contributor

Summary

This PR extends UP018 (native-literals) to detect more unnecessarily wrapped literals

Closes #19864.

Problem

Currently, UP018 misses several cases of redundant literal constructors:

  1. int().denominator and similar attribute accesses on zero-value calls.
  2. Implicitly concatenated string/bytes literals.
  3. str() and complex() calls with redundant keyword arguments like object= or real=.
  4. complex() calls with complex literals.

Approach

  1. Added Complex to LiteralType enum and supported complex in LiteralType::from_str.
  2. Updated LiteralType::as_zero_value_expr to return 0j for Complex.
  3. Updated native_literals to:
    • Support redundant keyword arguments (object for str, real for complex).
    • Remove the check that skipped implicitly concatenated strings.
    • Wrap implicitly concatenated strings/bytes in parentheses if they span multiple lines to maintain valid syntax.
    • Wrap zero-value int in parentheses when followed by an attribute access (e.g., (0).denominator).

Test Plan

  • Added new test cases to crates/ruff_linter/resources/test/fixtures/pyupgrade/UP018.py.
  • Verified existing and new tests pass with cargo test -p ruff_linter pyupgrade::tests.
  • Manually verified with a reproduction script.
  • Verified with cargo clippy.

@astral-sh-bot astral-sh-bot bot requested a review from ntBre March 21, 2026 04:27
@astral-sh-bot
Copy link
Copy Markdown

astral-sh-bot bot commented Mar 21, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

# t-strings are not native literals
str(t"hey")

# UP018 - Extended detections
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a test for a single line implicit concatenated string or bytes literal that's followed by an attribute access

@MichaReiser
Copy link
Copy Markdown
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)

@MichaReiser MichaReiser added the accepted Ready for implementation label Mar 30, 2026
@MichaReiser MichaReiser merged commit ef5b550 into astral-sh:main Mar 30, 2026
41 checks passed
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)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

accepted Ready for implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UP018 should detect more unnecessarily wrapped literals

3 participants