Skip to content

Commit 67ef370

Browse files
[flake8-use-pathlib] Fix PTH116 false positive when stat is passed a file descriptor (#17709)
Co-authored-by: Dhruv Manilawala <[email protected]>
1 parent e17e1e8 commit 67ef370

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/full_name.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ def opener(path, flags):
5656
def foo(y: int):
5757
open(y)
5858

59+
5960
# https://github.com/astral-sh/ruff/issues/17691
6061
def f() -> int:
6162
return 1
@@ -68,3 +69,16 @@ def f() -> int:
6869
def bytes_str_func() -> bytes:
6970
return b"foo"
7071
open(bytes_str_func())
72+
73+
# https://github.com/astral-sh/ruff/issues/17693
74+
os.stat(1)
75+
os.stat(x)
76+
77+
78+
def func() -> int:
79+
return 2
80+
os.stat(func())
81+
82+
83+
def bar(x: int):
84+
os.stat(x)

crates/ruff_linter/src/rules/flake8_use_pathlib/rules/replaceable_by_pathlib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,16 @@ pub(crate) fn replaceable_by_pathlib(checker: &Checker, call: &ExprCall) {
5555
// PTH114
5656
["os", "path", "islink"] => OsPathIslink.into(),
5757
// PTH116
58-
["os", "stat"] => OsStat.into(),
58+
["os", "stat"] => {
59+
if call
60+
.arguments
61+
.find_positional(0)
62+
.is_some_and(|expr| is_file_descriptor(expr, checker.semantic()))
63+
{
64+
return;
65+
}
66+
OsStat.into()
67+
}
5968
// PTH117
6069
["os", "path", "isabs"] => OsPathIsabs.into(),
6170
// PTH118

0 commit comments

Comments
 (0)