mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-19 03:38:35 +00:00
[flake8-use-pathlib] Fix false negative on direct Path() instantiation (PTH210) (#19388)
## Summary Fixes #19329 --------- Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
This commit is contained in:
parent
ff94fe7447
commit
029de784f1
3 changed files with 157 additions and 12 deletions
|
|
@ -54,6 +54,13 @@ windows_path.with_suffix(r"s")
|
||||||
windows_path.with_suffix(u'' "json")
|
windows_path.with_suffix(u'' "json")
|
||||||
windows_path.with_suffix(suffix="js")
|
windows_path.with_suffix(suffix="js")
|
||||||
|
|
||||||
|
Path().with_suffix(".")
|
||||||
|
Path().with_suffix("py")
|
||||||
|
PosixPath().with_suffix("py")
|
||||||
|
PurePath().with_suffix("py")
|
||||||
|
PurePosixPath().with_suffix("py")
|
||||||
|
PureWindowsPath().with_suffix("py")
|
||||||
|
WindowsPath().with_suffix("py")
|
||||||
|
|
||||||
### No errors
|
### No errors
|
||||||
path.with_suffix()
|
path.with_suffix()
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use crate::{Edit, Fix, FixAvailability, Violation};
|
||||||
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
use ruff_macros::{ViolationMetadata, derive_message_formats};
|
||||||
use ruff_python_ast::{self as ast, PythonVersion, StringFlags};
|
use ruff_python_ast::{self as ast, PythonVersion, StringFlags};
|
||||||
use ruff_python_semantic::SemanticModel;
|
use ruff_python_semantic::SemanticModel;
|
||||||
use ruff_python_semantic::analyze::typing;
|
use ruff_python_semantic::analyze::typing::{self, PathlibPathChecker, TypeChecker};
|
||||||
use ruff_text_size::Ranged;
|
use ruff_text_size::Ranged;
|
||||||
|
|
||||||
/// ## What it does
|
/// ## What it does
|
||||||
|
|
@ -141,12 +141,13 @@ fn is_path_with_suffix_call(semantic: &SemanticModel, func: &ast::Expr) -> bool
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let ast::Expr::Name(name) = &**value else {
|
match &**value {
|
||||||
return false;
|
ast::Expr::Name(name) => {
|
||||||
};
|
let Some(binding) = semantic.only_binding(name).map(|id| semantic.binding(id)) else {
|
||||||
let Some(binding) = semantic.only_binding(name).map(|id| semantic.binding(id)) else {
|
return false;
|
||||||
return false;
|
};
|
||||||
};
|
typing::is_pathlib_path(binding, semantic)
|
||||||
|
}
|
||||||
typing::is_pathlib_path(binding, semantic)
|
expr => PathlibPathChecker::match_initializer(expr, semantic),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -536,7 +536,7 @@ PTH210.py:54:1: PTH210 [*] Dotless suffix passed to `.with_suffix()`
|
||||||
54 |+windows_path.with_suffix(u'.' "json")
|
54 |+windows_path.with_suffix(u'.' "json")
|
||||||
55 55 | windows_path.with_suffix(suffix="js")
|
55 55 | windows_path.with_suffix(suffix="js")
|
||||||
56 56 |
|
56 56 |
|
||||||
57 57 |
|
57 57 | Path().with_suffix(".")
|
||||||
|
|
||||||
PTH210.py:55:1: PTH210 [*] Dotless suffix passed to `.with_suffix()`
|
PTH210.py:55:1: PTH210 [*] Dotless suffix passed to `.with_suffix()`
|
||||||
|
|
|
|
||||||
|
|
@ -544,6 +544,8 @@ PTH210.py:55:1: PTH210 [*] Dotless suffix passed to `.with_suffix()`
|
||||||
54 | windows_path.with_suffix(u'' "json")
|
54 | windows_path.with_suffix(u'' "json")
|
||||||
55 | windows_path.with_suffix(suffix="js")
|
55 | windows_path.with_suffix(suffix="js")
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH210
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH210
|
||||||
|
56 |
|
||||||
|
57 | Path().with_suffix(".")
|
||||||
|
|
|
|
||||||
= help: Add a leading dot
|
= help: Add a leading dot
|
||||||
|
|
||||||
|
|
@ -554,5 +556,140 @@ PTH210.py:55:1: PTH210 [*] Dotless suffix passed to `.with_suffix()`
|
||||||
55 |-windows_path.with_suffix(suffix="js")
|
55 |-windows_path.with_suffix(suffix="js")
|
||||||
55 |+windows_path.with_suffix(suffix=".js")
|
55 |+windows_path.with_suffix(suffix=".js")
|
||||||
56 56 |
|
56 56 |
|
||||||
57 57 |
|
57 57 | Path().with_suffix(".")
|
||||||
58 58 | ### No errors
|
58 58 | Path().with_suffix("py")
|
||||||
|
|
||||||
|
PTH210.py:57:1: PTH210 Invalid suffix passed to `.with_suffix()`
|
||||||
|
|
|
||||||
|
55 | windows_path.with_suffix(suffix="js")
|
||||||
|
56 |
|
||||||
|
57 | Path().with_suffix(".")
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^ PTH210
|
||||||
|
58 | Path().with_suffix("py")
|
||||||
|
59 | PosixPath().with_suffix("py")
|
||||||
|
|
|
||||||
|
= help: Remove "." or extend to valid suffix
|
||||||
|
|
||||||
|
PTH210.py:58:1: PTH210 [*] Dotless suffix passed to `.with_suffix()`
|
||||||
|
|
|
||||||
|
57 | Path().with_suffix(".")
|
||||||
|
58 | Path().with_suffix("py")
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^ PTH210
|
||||||
|
59 | PosixPath().with_suffix("py")
|
||||||
|
60 | PurePath().with_suffix("py")
|
||||||
|
|
|
||||||
|
= help: Add a leading dot
|
||||||
|
|
||||||
|
ℹ Unsafe fix
|
||||||
|
55 55 | windows_path.with_suffix(suffix="js")
|
||||||
|
56 56 |
|
||||||
|
57 57 | Path().with_suffix(".")
|
||||||
|
58 |-Path().with_suffix("py")
|
||||||
|
58 |+Path().with_suffix(".py")
|
||||||
|
59 59 | PosixPath().with_suffix("py")
|
||||||
|
60 60 | PurePath().with_suffix("py")
|
||||||
|
61 61 | PurePosixPath().with_suffix("py")
|
||||||
|
|
||||||
|
PTH210.py:59:1: PTH210 [*] Dotless suffix passed to `.with_suffix()`
|
||||||
|
|
|
||||||
|
57 | Path().with_suffix(".")
|
||||||
|
58 | Path().with_suffix("py")
|
||||||
|
59 | PosixPath().with_suffix("py")
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH210
|
||||||
|
60 | PurePath().with_suffix("py")
|
||||||
|
61 | PurePosixPath().with_suffix("py")
|
||||||
|
|
|
||||||
|
= help: Add a leading dot
|
||||||
|
|
||||||
|
ℹ Unsafe fix
|
||||||
|
56 56 |
|
||||||
|
57 57 | Path().with_suffix(".")
|
||||||
|
58 58 | Path().with_suffix("py")
|
||||||
|
59 |-PosixPath().with_suffix("py")
|
||||||
|
59 |+PosixPath().with_suffix(".py")
|
||||||
|
60 60 | PurePath().with_suffix("py")
|
||||||
|
61 61 | PurePosixPath().with_suffix("py")
|
||||||
|
62 62 | PureWindowsPath().with_suffix("py")
|
||||||
|
|
||||||
|
PTH210.py:60:1: PTH210 [*] Dotless suffix passed to `.with_suffix()`
|
||||||
|
|
|
||||||
|
58 | Path().with_suffix("py")
|
||||||
|
59 | PosixPath().with_suffix("py")
|
||||||
|
60 | PurePath().with_suffix("py")
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH210
|
||||||
|
61 | PurePosixPath().with_suffix("py")
|
||||||
|
62 | PureWindowsPath().with_suffix("py")
|
||||||
|
|
|
||||||
|
= help: Add a leading dot
|
||||||
|
|
||||||
|
ℹ Unsafe fix
|
||||||
|
57 57 | Path().with_suffix(".")
|
||||||
|
58 58 | Path().with_suffix("py")
|
||||||
|
59 59 | PosixPath().with_suffix("py")
|
||||||
|
60 |-PurePath().with_suffix("py")
|
||||||
|
60 |+PurePath().with_suffix(".py")
|
||||||
|
61 61 | PurePosixPath().with_suffix("py")
|
||||||
|
62 62 | PureWindowsPath().with_suffix("py")
|
||||||
|
63 63 | WindowsPath().with_suffix("py")
|
||||||
|
|
||||||
|
PTH210.py:61:1: PTH210 [*] Dotless suffix passed to `.with_suffix()`
|
||||||
|
|
|
||||||
|
59 | PosixPath().with_suffix("py")
|
||||||
|
60 | PurePath().with_suffix("py")
|
||||||
|
61 | PurePosixPath().with_suffix("py")
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH210
|
||||||
|
62 | PureWindowsPath().with_suffix("py")
|
||||||
|
63 | WindowsPath().with_suffix("py")
|
||||||
|
|
|
||||||
|
= help: Add a leading dot
|
||||||
|
|
||||||
|
ℹ Unsafe fix
|
||||||
|
58 58 | Path().with_suffix("py")
|
||||||
|
59 59 | PosixPath().with_suffix("py")
|
||||||
|
60 60 | PurePath().with_suffix("py")
|
||||||
|
61 |-PurePosixPath().with_suffix("py")
|
||||||
|
61 |+PurePosixPath().with_suffix(".py")
|
||||||
|
62 62 | PureWindowsPath().with_suffix("py")
|
||||||
|
63 63 | WindowsPath().with_suffix("py")
|
||||||
|
64 64 |
|
||||||
|
|
||||||
|
PTH210.py:62:1: PTH210 [*] Dotless suffix passed to `.with_suffix()`
|
||||||
|
|
|
||||||
|
60 | PurePath().with_suffix("py")
|
||||||
|
61 | PurePosixPath().with_suffix("py")
|
||||||
|
62 | PureWindowsPath().with_suffix("py")
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH210
|
||||||
|
63 | WindowsPath().with_suffix("py")
|
||||||
|
|
|
||||||
|
= help: Add a leading dot
|
||||||
|
|
||||||
|
ℹ Unsafe fix
|
||||||
|
59 59 | PosixPath().with_suffix("py")
|
||||||
|
60 60 | PurePath().with_suffix("py")
|
||||||
|
61 61 | PurePosixPath().with_suffix("py")
|
||||||
|
62 |-PureWindowsPath().with_suffix("py")
|
||||||
|
62 |+PureWindowsPath().with_suffix(".py")
|
||||||
|
63 63 | WindowsPath().with_suffix("py")
|
||||||
|
64 64 |
|
||||||
|
65 65 | ### No errors
|
||||||
|
|
||||||
|
PTH210.py:63:1: PTH210 [*] Dotless suffix passed to `.with_suffix()`
|
||||||
|
|
|
||||||
|
61 | PurePosixPath().with_suffix("py")
|
||||||
|
62 | PureWindowsPath().with_suffix("py")
|
||||||
|
63 | WindowsPath().with_suffix("py")
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PTH210
|
||||||
|
64 |
|
||||||
|
65 | ### No errors
|
||||||
|
|
|
||||||
|
= help: Add a leading dot
|
||||||
|
|
||||||
|
ℹ Unsafe fix
|
||||||
|
60 60 | PurePath().with_suffix("py")
|
||||||
|
61 61 | PurePosixPath().with_suffix("py")
|
||||||
|
62 62 | PureWindowsPath().with_suffix("py")
|
||||||
|
63 |-WindowsPath().with_suffix("py")
|
||||||
|
63 |+WindowsPath().with_suffix(".py")
|
||||||
|
64 64 |
|
||||||
|
65 65 | ### No errors
|
||||||
|
66 66 | path.with_suffix()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue