|
| 1 | +use crate::checkers::ast::Checker; |
| 2 | +use crate::preview::is_fix_os_chmod_enabled; |
| 3 | +use crate::rules::flake8_use_pathlib::helpers::{ |
| 4 | + check_os_pathlib_two_arg_calls, is_file_descriptor, is_keyword_only_argument_non_default, |
| 5 | +}; |
| 6 | +use crate::{FixAvailability, Violation}; |
| 7 | +use ruff_macros::{ViolationMetadata, derive_message_formats}; |
| 8 | +use ruff_python_ast::ExprCall; |
| 9 | + |
| 10 | +/// ## What it does |
| 11 | +/// Checks for uses of `os.chmod`. |
| 12 | +/// |
| 13 | +/// ## Why is this bad? |
| 14 | +/// `pathlib` offers a high-level API for path manipulation, as compared to |
| 15 | +/// the lower-level API offered by `os`. When possible, using `Path` object |
| 16 | +/// methods such as `Path.chmod()` can improve readability over the `os` |
| 17 | +/// module's counterparts (e.g., `os.chmod()`). |
| 18 | +/// |
| 19 | +/// ## Examples |
| 20 | +/// ```python |
| 21 | +/// import os |
| 22 | +/// |
| 23 | +/// os.chmod("file.py", 0o444) |
| 24 | +/// ``` |
| 25 | +/// |
| 26 | +/// Use instead: |
| 27 | +/// ```python |
| 28 | +/// from pathlib import Path |
| 29 | +/// |
| 30 | +/// Path("file.py").chmod(0o444) |
| 31 | +/// ``` |
| 32 | +/// |
| 33 | +/// ## Known issues |
| 34 | +/// While using `pathlib` can improve the readability and type safety of your code, |
| 35 | +/// it can be less performant than the lower-level alternatives that work directly with strings, |
| 36 | +/// especially on older versions of Python. |
| 37 | +/// |
| 38 | +/// ## Fix Safety |
| 39 | +/// This rule's fix is marked as unsafe if the replacement would remove comments attached to the original expression. |
| 40 | +/// |
| 41 | +/// ## References |
| 42 | +/// - [Python documentation: `Path.chmod`](https://docs.python.org/3/library/pathlib.html#pathlib.Path.chmod) |
| 43 | +/// - [Python documentation: `os.chmod`](https://docs.python.org/3/library/os.html#os.chmod) |
| 44 | +/// - [PEP 428 – The pathlib module – object-oriented filesystem paths](https://peps.python.org/pep-0428/) |
| 45 | +/// - [Correspondence between `os` and `pathlib`](https://docs.python.org/3/library/pathlib.html#correspondence-to-tools-in-the-os-module) |
| 46 | +/// - [Why you should be using pathlib](https://treyhunner.com/2018/12/why-you-should-be-using-pathlib/) |
| 47 | +/// - [No really, pathlib is great](https://treyhunner.com/2019/01/no-really-pathlib-is-great/) |
| 48 | +#[derive(ViolationMetadata)] |
| 49 | +pub(crate) struct OsChmod; |
| 50 | + |
| 51 | +impl Violation for OsChmod { |
| 52 | + const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; |
| 53 | + |
| 54 | + #[derive_message_formats] |
| 55 | + fn message(&self) -> String { |
| 56 | + "`os.chmod()` should be replaced by `Path.chmod()`".to_string() |
| 57 | + } |
| 58 | + |
| 59 | + fn fix_title(&self) -> Option<String> { |
| 60 | + Some("Replace with `Path(...).chmod(...)`".to_string()) |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// PTH101 |
| 65 | +pub(crate) fn os_chmod(checker: &Checker, call: &ExprCall, segments: &[&str]) { |
| 66 | + if segments != ["os", "chmod"] { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // `dir_fd` is not supported by pathlib, so check if it's set to non-default values. |
| 71 | + // Signature as of Python 3.13 (https://docs.python.org/3/library/os.html#os.chmod) |
| 72 | + // ```text |
| 73 | + // 0 1 2 3 |
| 74 | + // os.chmod(path, mode, *, dir_fd=None, follow_symlinks=True) |
| 75 | + // ``` |
| 76 | + if call |
| 77 | + .arguments |
| 78 | + .find_argument_value("path", 0) |
| 79 | + .is_some_and(|expr| is_file_descriptor(expr, checker.semantic())) |
| 80 | + || is_keyword_only_argument_non_default(&call.arguments, "dir_fd") |
| 81 | + { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + check_os_pathlib_two_arg_calls( |
| 86 | + checker, |
| 87 | + call, |
| 88 | + "chmod", |
| 89 | + "path", |
| 90 | + "mode", |
| 91 | + is_fix_os_chmod_enabled(checker.settings()), |
| 92 | + OsChmod, |
| 93 | + ); |
| 94 | +} |
0 commit comments