Skip to content

PTH101, PTH104, PTH105, and PTH121 ignore extra arguments #20134

@dscorbett

Description

@dscorbett

Summary

The fixes for os-chmod (PTH101), os-rename (PTH104), os-replace (PTH105), and os-path-samefile (PTH121) do not check for extra arguments. This can change a program’s behavior. The fix for PTH101 should preserve the follow_symlinks argument if specified. All four fixes should be suppressed when the arguments don’t match the signature. Example:

$ cat >pth1.py <<'# EOF'
import os

# PTH101
os.close(os.open("pth1_file", os.O_CREAT | os.O_WRONLY, 0o640))
try: os.unlink("pth1_link")
except FileNotFoundError: pass
os.symlink("pth1_file", "pth1_link")
os.chmod("pth1_link", 0o600, follow_symlinks=False)
print(oct(os.stat("pth1_file").st_mode))
print(oct(os.lstat("pth1_link").st_mode))

# PTH101
try: os.chmod("pth1_file", 0o700, None, True, 1, *[1], **{"x": 1}, foo=1)
except TypeError as e: print(e)

# PTH104
try: os.rename("pth1_file", "pth1_file1", None, None, 1, *[1], **{"x": 1}, foo=1)
except TypeError as e: print(e)

# PTH105
try: os.replace("pth1_file1", "pth1_file", None, None, 1, *[1], **{"x": 1}, foo=1)
except TypeError as e: print(e)

# PTH121
try: os.path.samefile("pth1_file", "pth1_link", 1, *[1], **{"x": 1}, foo=1)
except TypeError as e: print(e)

os.unlink("pth1_file")
os.unlink("pth1_link")
# EOF

$ python pth1.py
0o100640
0o120600
chmod() takes at most 4 arguments (8 given)
rename() takes at most 4 arguments (8 given)
replace() takes at most 4 arguments (8 given)
samefile() got an unexpected keyword argument 'x'

$ ruff --isolated check pth1.py --select PTH101,PTH104,PTH105,PTH121 --preview --fix
Found 5 errors (5 fixed, 0 remaining).

$ python pth1.py
0o100600
0o120755

Version

ruff 0.12.10 (c68ff8d 2025-08-21)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationspreviewRelated to preview mode features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions