diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/full_name.py b/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/full_name.py index 8680212b20c3a..bd00f5193e3e8 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/full_name.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_use_pathlib/full_name.py @@ -56,6 +56,7 @@ def opener(path, flags): def foo(y: int): open(y) + # https://github.com/astral-sh/ruff/issues/17691 def f() -> int: return 1 @@ -68,3 +69,16 @@ def f() -> int: 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) diff --git a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/replaceable_by_pathlib.rs b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/replaceable_by_pathlib.rs index a4bce41939700..dd47bb12d0a27 100644 --- a/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/replaceable_by_pathlib.rs +++ b/crates/ruff_linter/src/rules/flake8_use_pathlib/rules/replaceable_by_pathlib.rs @@ -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