Skip to content

PTH100 unsafely converts os.path.abspath to pathlib.Path.resolve #20088

@dscorbett

Description

@dscorbett

Summary

The fix for os-path-abspath (PTH100) converts os.path.abspath to pathlib.Path.resolve, which does not do the same thing. Sometimes, as in upstream flake8-use-pathlib issue #8, pathlib.Path.absolute is the preferred replacement, but there is no exact equivalent; see python/cpython#69200. The documentation for PTH100 should explain this and the fix should be marked unsafe.

Function Removes ..? Resolves symlinks?
abspath
resolve
absolute

Example:

$ cat >pth100.py <<'# EOF'
import os
open("pth100_src", "w").close()
os.symlink("pth100_src", "pth100_dst")
os.mkdir("pth100_dir")
print(str(os.path.abspath("pth100_dir/../dst")).removeprefix(f"{os.getcwd()}/"))
# EOF

$ rm -fr pth100_{dir,dst,src}

$ python pth100.py
dst

$ ruff --isolated check pth100.py --select PTH100 --preview --fix
Found 1 error (1 fixed, 0 remaining).

$ cat pth100.py
import os
import pathlib
open("pth100_src", "w").close()
os.symlink("pth100_src", "pth100_dst")
os.mkdir("pth100_dir")
print(str(pathlib.Path("pth100_dir/../dst").resolve()).removeprefix(f"{os.getcwd()}/"))

$ rm -fr pth100_{dir,dst,src}

$ python pth100.py
src

Version

ruff 0.12.10 (c68ff8d 2025-08-21)

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixesRelated 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