os-path-abspath (PTH100)
Derived from the flake8-use-pathlib linter.
Fix is sometimes available.
What it does
Checks for uses of os.path.abspath
.
Why is this bad?
pathlib
offers a high-level API for path manipulation, as compared to
the lower-level API offered by os.path
. When possible, using Path
object
methods such as Path.resolve()
can improve readability over the os.path
module's counterparts (e.g., os.path.abspath()
).
Examples
Use instead:
Known issues
While using pathlib
can improve the readability and type safety of your code,
it can be less performant than the lower-level alternatives that work directly with strings,
especially on older versions of Python.
Fix Safety
This rule's fix is always marked as unsafe because Path.resolve()
resolves symlinks, while
os.path.abspath()
does not. If resolving symlinks is important, you may need to use
Path.absolute()
. However, Path.absolute()
also does not remove any ..
components in a
path, unlike os.path.abspath()
and Path.resolve()
, so if that specific combination of
behaviors is required, there's no existing pathlib
alternative. See CPython issue
#69200.