Skip to content

UP018 should detect more unnecessarily wrapped literals #19864

@dscorbett

Description

@dscorbett

Summary

Some unnecessarily wrapped literals would be easy for native-literals (UP018) to detect but are not detected.

int() is not detected when followed by an attribute. This is to avoid a syntax error. However, the syntax error can be avoided with parentheses. Compare int(0), which UP018 always detects, inserting parentheses when needed.

Implicitly concatenated string literals are skipped for no apparent reason. If it spans multiple lines, it still needs to be parenthesized, but the str call is unnecessary.

The object keyword of str is not supported.

complex is not supported.

Example:

$ cat >up018.py <<'# EOF'
int().denominator
str("A" "B")
str(
    "A"
    "B"
)
str(object="!")
complex(1j)
complex(real=1j)
# EOF

$ ruff --isolated check up018.py --select UP018
All checks passed!

Ideal fixed output:

(0).denominator
"A" "B"
(
    "A"
    "B"
)
"!"
1j
1j

Version

ruff 0.12.8 (f51a228 2025-08-07)

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions