[flake8-use-pathlib] Fix PTH116 false positive when stat is passed a file descriptor (#17709)

Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
This commit is contained in:
Victor Hugo Gomes
2025-05-01 03:16:28 -03:00
committed by GitHub
parent e17e1e860b
commit 67ef370733
2 changed files with 24 additions and 1 deletions

View File

@@ -56,6 +56,7 @@ open(x)
def foo(y: int):
open(y)
# https://github.com/astral-sh/ruff/issues/17691
def f() -> int:
return 1
@@ -68,3 +69,16 @@ open(byte_str)
def bytes_str_func() -> bytes:
return b"foo"
open(bytes_str_func())
# https://github.com/astral-sh/ruff/issues/17693
os.stat(1)
os.stat(x)
def func() -> int:
return 2
os.stat(func())
def bar(x: int):
os.stat(x)

View File

@@ -55,7 +55,16 @@ pub(crate) fn replaceable_by_pathlib(checker: &Checker, call: &ExprCall) {
// PTH114
["os", "path", "islink"] => OsPathIslink.into(),
// PTH116
["os", "stat"] => OsStat.into(),
["os", "stat"] => {
if call
.arguments
.find_positional(0)
.is_some_and(|expr| is_file_descriptor(expr, checker.semantic()))
{
return;
}
OsStat.into()
}
// PTH117
["os", "path", "isabs"] => OsPathIsabs.into(),
// PTH118