diff --git a/crates/ruff/src/rules/flake8_use_pathlib/violations.rs b/crates/ruff/src/rules/flake8_use_pathlib/violations.rs index 738bffb76d77dd..fa4d53d9515057 100644 --- a/crates/ruff/src/rules/flake8_use_pathlib/violations.rs +++ b/crates/ruff/src/rules/flake8_use_pathlib/violations.rs @@ -2,6 +2,37 @@ use ruff_diagnostics::Violation; use ruff_macros::{derive_message_formats, violation}; // PTH100 + +/// ## What it does +/// Detects the use of `os.path.abspath`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.resolve()` improves readability over their `os` counterparts such as +/// `os.path.abspath()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// file_path = os.path.abspath("../path/to/file") +/// ``` +/// +/// Use instead: +/// ```python +/// file_path = Path("../path/to/file").resolve() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.resolve`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.resolve) +/// - [Python documentation: `os.path.abspath`](https://docs.python.org/3/library/os.path.html#os.path.abspath) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathAbspath; @@ -13,6 +44,37 @@ impl Violation for OsPathAbspath { } // PTH101 + +/// ## What it does +/// Detects the use of `os.chmod`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.chmod()` improves readability over their `os` counterparts such as +/// `os.chmod()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.chmod("file.py", 0o444) +/// ``` +/// +/// Use instead: +/// ```python +/// Path("file.py").chmod(0o444) +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.chmod`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.chmod) +/// - [Python documentation: `os.chmod`](https://docs.python.org/3/library/os.html#os.chmod) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsChmod; @@ -24,6 +86,37 @@ impl Violation for OsChmod { } // PTH102 + +/// ## What it does +/// Detects the use of `os.makedirs`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.mkdir(parents=True)` improves readability over their `os` +/// counterparts such as `os.makedirs()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.makedirs("./nested/directory/") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("./nested/directory/").mkdir(parents=True) +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.mkdir`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir) +/// - [Python documentation: `os.makedirs`](https://docs.python.org/3/library/os.html#os.makedirs) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsMakedirs; @@ -35,6 +128,37 @@ impl Violation for OsMakedirs { } // PTH103 + +/// ## What it does +/// Detects the use of `os.mkdir`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.mkdir()` improves readability over their `os` counterparts such as +/// `os.mkdir()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.mkdir("./directory/") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("./directory/").mkdir() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.mkdir`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.mkdir) +/// - [Python documentation: `os.mkdir`](https://docs.python.org/3/library/os.html#os.mkdir) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsMkdir; @@ -46,6 +170,37 @@ impl Violation for OsMkdir { } // PTH104 + +/// ## What it does +/// Detects the use of `os.rename`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.rename()` improves readability over their `os` counterparts such as +/// `os.rename()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.rename("old.py", "new.py") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("old.py").rename("new.py") +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.rename`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.rename) +/// - [Python documentation: `os.rename`](https://docs.python.org/3/library/os.html#os.rename) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsRename; @@ -57,6 +212,37 @@ impl Violation for OsRename { } // PTH105 + +/// ## What it does +/// Detects the use of `os.replace`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.replace()` improves readability over their `os` counterparts such as +/// `os.replace()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.replace("old.py", "new.py") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("old.py").replace("new.py") +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.replace`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.replace) +/// - [Python documentation: `os.replace`](https://docs.python.org/3/library/os.html#os.replace) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsReplace; @@ -68,6 +254,37 @@ impl Violation for OsReplace { } // PTH106 + +/// ## What it does +/// Detects the use of `os.rmdir`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.rmdir()` improves readability over their `os` counterparts such as +/// `os.rmdir()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.rmdir("folder/") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("folder/").rmdir() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.rmdir`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.rmdir) +/// - [Python documentation: `os.rmdir`](https://docs.python.org/3/library/os.html#os.rmdir) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsRmdir; @@ -79,6 +296,37 @@ impl Violation for OsRmdir { } // PTH107 + +/// ## What it does +/// Detects the use of `os.remove`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.unlink()` improves readability over their `os` counterparts such as +/// `os.remove()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.remove("file.py") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("file.py").unlink() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.unlink`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.unlink) +/// - [Python documentation: `os.remove`](https://docs.python.org/3/library/os.html#os.remove) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsRemove; @@ -90,6 +338,37 @@ impl Violation for OsRemove { } // PTH108 + +/// ## What it does +/// Detects the use of `os.unlink`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.unlink()` improves readability over their `os` counterparts such as +/// `os.unlink()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.unlink("file.py") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("file.py").unlink() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.unlink`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.unlink) +/// - [Python documentation: `os.unlink`](https://docs.python.org/3/library/os.html#os.unlink) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsUnlink; @@ -101,6 +380,38 @@ impl Violation for OsUnlink { } // PTH109 + +/// ## What it does +/// Detects the use of `os.getcwd` and `os.getcwdb`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.cwd()` improves readability over their `os` counterparts such as +/// `os.getcwd()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// cwd = os.getcwd() +/// ``` +/// +/// Use instead: +/// ```python +/// cwd = Path.cwd() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.cwd`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.cwd) +/// - [Python documentation: `os.getcwd`](https://docs.python.org/3/library/os.html#os.getcwd) +/// - [Python documentation: `os.getcwdb`](https://docs.python.org/3/library/os.html#os.getcwdb) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsGetcwd; @@ -112,6 +423,37 @@ impl Violation for OsGetcwd { } // PTH110 + +/// ## What it does +/// Detects the use of `os.path.exists`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.exists()` improves readability over their `os` counterparts such as +/// `os.path.exists()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.path.exists("file.py") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("file.py").exists() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.exists`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.exists) +/// - [Python documentation: `os.path.exists`](https://docs.python.org/3/library/os.path.html#os.path.exists) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathExists; @@ -123,6 +465,37 @@ impl Violation for OsPathExists { } // PTH111 + +/// ## What it does +/// Detects the use of `os.path.expanduser`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.expanduser()` improves readability over their `os` counterparts such +/// as `os.path.expanduser()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.path.expanduser("~/films/Monty Python") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("~/films/Monty Python").expanduser() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.expanduser`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.expanduser) +/// - [Python documentation: `os.path.expanduser`](https://docs.python.org/3/library/os.path.html#os.path.expanduser) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathExpanduser; @@ -134,6 +507,37 @@ impl Violation for OsPathExpanduser { } // PTH112 + +/// ## What it does +/// Detects the use of `os.path.isdir`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.is_dir()` improves readability over their `os` counterparts such as +/// `os.path.isdir()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.path.isdir("docs") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("docs").is_dir() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.is_dir`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.is_dir) +/// - [Python documentation: `os.path.isdir`](https://docs.python.org/3/library/os.path.html#os.path.isdir) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathIsdir; @@ -145,6 +549,37 @@ impl Violation for OsPathIsdir { } // PTH113 + +/// ## What it does +/// Detects the use of `os.path.isfile`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.is_file()` improves readability over their `os` counterparts such as +/// `os.path.isfile()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.path.isfile("docs") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("docs").is_file() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.is_file`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.is_file) +/// - [Python documentation: `os.path.isfile`](https://docs.python.org/3/library/os.path.html#os.path.isfile) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathIsfile; @@ -156,6 +591,37 @@ impl Violation for OsPathIsfile { } // PTH114 + +/// ## What it does +/// Detects the use of `os.path.islink`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.is_link()` improves readability over their `os` counterparts such as +/// `os.path.islink()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.path.islink("docs") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("docs").is_link() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.is_link`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.is_link) +/// - [Python documentation: `os.path.islink`](https://docs.python.org/3/library/os.path.html#os.path.islink) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathIslink; @@ -167,6 +633,37 @@ impl Violation for OsPathIslink { } // PTH115 + +/// ## What it does +/// Detects the use of `os.readlink`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.readlink()` improves readability over their `os` counterparts such as +/// `os.readlink()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.readlink(file_name) +/// ``` +/// +/// Use instead: +/// ```python +/// Path(file_name).readlink() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.readlink`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.readline) +/// - [Python documentation: `os.readlink`](https://docs.python.org/3/library/os.html#os.readlink) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsReadlink; @@ -178,6 +675,50 @@ impl Violation for OsReadlink { } // PTH116 + +/// ## What it does +/// Detects the use of `os.stat`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.stat()` improves readability over their `os` counterparts such as +/// `os.path.stat()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// import os +/// from pwd import getpwuid +/// from grp import getgrgid +/// +/// stat = os.stat(file_name) +/// owner_name = getpwuid(stat.st_uid).pw_name +/// group_name = getgrgid(stat.st_gid).gr_name +/// ``` +/// +/// Use instead: +/// ```python +/// from pathlib import Path +/// +/// file_path = Path(file_name) +/// stat = file_path.stat() +/// owner_name = file_path.owner() +/// group_name = file_path.group() +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.stat`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.group) +/// - [Python documentation: `Path.group`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.group) +/// - [Python documentation: `Path.owner`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.owner) +/// - [Python documentation: `os.stat`](https://docs.python.org/3/library/os.html#os.stat) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsStat; @@ -191,6 +732,39 @@ impl Violation for OsStat { } // PTH117 + +/// ## What it does +/// Detects the use of `os.path.isabs`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.is_absolute()` improves readability over their `os` counterparts such +/// as `os.path.isabs()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// if os.path.isabs(file_name): +/// print("Absolute path!") +/// ``` +/// +/// Use instead: +/// ```python +/// if Path(file_name).is_absolute(): +/// print("Absolute path!") +/// ``` +/// +/// ## References +/// - [Python documentation: `PurePath.is_absolute`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.is_absolute) +/// - [Python documentation: `os.path.isabs`](https://docs.python.org/3/library/os.path.html#os.path.isabs) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathIsabs; @@ -202,6 +776,37 @@ impl Violation for OsPathIsabs { } // PTH118 + +/// ## What it does +/// Detects the use of `os.path.join`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.joinpath()` improves readability over their `os` counterparts such as +/// `os.path.join()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.path.join(os.path.join(ROOT_PATH, "folder"), "file.py") +/// ``` +/// +/// Use instead: +/// ```python +/// Path(ROOT_PATH) / "folder" / "file.py" +/// ``` +/// +/// ## References +/// - [Python documentation: `PurePath.joinpath`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.joinpath) +/// - [Python documentation: `os.path.join`](https://docs.python.org/3/library/os.path.html#os.path.join) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathJoin; @@ -213,6 +818,37 @@ impl Violation for OsPathJoin { } // PTH119 + +/// ## What it does +/// Detects the use of `os.path.basename`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.name` improves readability over their `os` counterparts such as +/// `os.path.basename()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.path.basename(__file__) +/// ``` +/// +/// Use instead: +/// ```python +/// Path(__file__).name +/// ``` +/// +/// ## References +/// - [Python documentation: `PurePath.name`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.name) +/// - [Python documentation: `os.path.basename`](https://docs.python.org/3/library/os.path.html#os.path.basename) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathBasename; @@ -224,6 +860,37 @@ impl Violation for OsPathBasename { } // PTH120 + +/// ## What it does +/// Detects the use of `os.path.dirname`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.parent` improves readability over their `os` counterparts such as +/// `os.path.dirname()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.path.dirname(__file__) +/// ``` +/// +/// Use instead: +/// ```python +/// Path(__file__).parent +/// ``` +/// +/// ## References +/// - [Python documentation: `PurePath.parent`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.parent) +/// - [Python documentation: `os.path.dirname`](https://docs.python.org/3/library/os.path.html#os.path.dirname) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathDirname; @@ -235,6 +902,37 @@ impl Violation for OsPathDirname { } // PTH121 + +/// ## What it does +/// Detects the use of `os.path.samefile`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.samefile()` improves readability over their `os` counterparts such as +/// `os.path.samefile()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.path.samefile("f1.py", "f2.py") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("f1.py").samefile("f2.py") +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.samefile`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.samefile) +/// - [Python documentation: `os.path.samefile`](https://docs.python.org/3/library/os.path.html#os.path.samefile) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathSamefile; @@ -246,6 +944,38 @@ impl Violation for OsPathSamefile { } // PTH122 + +/// ## What it does +/// Detects the use of `os.path.splitext`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.suffix` improves readability over their `os` counterparts such as +/// `os.path.splitext()`. +/// +/// There are situations where creating many `Path` object causes overhead. +/// `os` functions therefore remain preferable in heavy loops and data +/// structures storing paths (e.g. pandas). +/// +/// ## Examples +/// ```python +/// os.path.splitext("f1.py") +/// ``` +/// +/// Use instead: +/// ```python +/// Path("f1.py").suffix +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.suffix`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.suffix) +/// - [Python documentation: `Path.suffixes`](https://docs.python.org/3/library/pathlib.html#pathlib.PurePath.suffixes) +/// - [Python documentation: `os.path.splitext`](https://docs.python.org/3/library/os.path.html#os.path.splitext) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct OsPathSplitext; @@ -257,6 +987,36 @@ impl Violation for OsPathSplitext { } // PTH123 + +/// ## What it does +/// Detects the use of `open`. +/// +/// ## Why is this bad? +/// `pathlib` offers high-level path manipulations, `os` offers low-level +/// manipulation of paths. The use of the `Path` object where possible, e.g. +/// `Path.open()` improves readability over their `os` counterparts that are +/// often used to manipulate the input argument to `open()`. +/// +/// +/// ## Examples +/// ```python +/// with open("f1.py", "wb") as fp: +/// ... +/// ``` +/// +/// Use instead: +/// ```python +/// with Path("f1.py").open("wb") as fp: +/// ... +/// ``` +/// +/// ## References +/// - [Python documentation: `Path.open`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.open) +/// - [Python documentation: `open`](https://docs.python.org/3/library/functions.html#open) +/// - [PEP 428](https://peps.python.org/pep-0428/) +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) #[violation] pub struct BuiltinOpen; @@ -268,6 +1028,28 @@ impl Violation for BuiltinOpen { } // PTH124 + +/// ## What it does +/// Detects the use of the `py.path` lirbary. +/// +/// ## Why is this bad? +/// `py.path` is in maintenance mode, use the `pathlib` module from the +/// standard library, or third-party modules such as `path` (formerly +/// `py.path`). +/// +/// ## Examples +/// ```python +/// p = py.path.local("/foo/bar").join("baz/qux") +/// ``` +/// +/// Use instead: +/// ```python +/// p = Path("/foo/bar") / "bar" / "qux" +/// ``` +/// +/// ## References +/// - [Python documentation: `Pathlib`](https://docs.python.org/3/library/pathlib.html) +/// - [Path repository](https://github.com/jaraco/path) #[violation] pub struct PyPath;