Skip to content

UP018 fix should add spaces between tokens #17606

@dscorbett

Description

@dscorbett

Summary

The fix for native-literals (UP018) should add spaces between tokens as necessary to avoid syntax errors and other problems.

Reverted syntax error:

$ cat >up018_1.py <<'# EOF'
bool(True)and None
# EOF

$ ruff --isolated check up018_1.py --select UP018 --diff 2>&1 | grep error:
error: Fix introduced a syntax error. Reverting all changes.

Runtime SyntaxWarnings:

$ cat >up018_2.py <<'# EOF'
int(1)and None
float(1.)and None
# EOF

$ ruff --isolated check up018_2.py --select UP018 --fix
Found 2 errors (2 fixed, 0 remaining).

$ cat up018_2.py
1and None
1.and None

$ python up018_2.py
up018_2.py:1: SyntaxWarning: invalid decimal literal
  1and None
up018_2.py:2: SyntaxWarning: invalid decimal literal
  1.and None

Runtime behavior change:

$ cat >up018_3.py <<'# EOF'
bool(True)and()
# EOF

$ ruff --isolated check up018_3.py --select UP018 --fix
Found 1 error (1 fixed, 0 remaining).

$ cat up018_3.py
Trueand()

$ python up018_3.py 
Traceback (most recent call last):
  File "up018_3.py", line 1, in <module>
    Trueand()
    ^^^^^^^
NameError: name 'Trueand' is not defined

Version

ruff 0.11.6 (fcd50a0 2025-04-17)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationshelp wantedContributions especially welcome

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions