Skip to content

[flake8-bandit] Check for builtins instead of builtin (S102, PTH123) #15443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/flake8_bandit/S102.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ def fn():
exec('x = 2')

exec('y = 3')


## https://github.com/astral-sh/ruff/issues/15442
def _():
from builtins import exec
exec('') # Error

def _():
from builtin import exec
exec('') # No error
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,16 @@
with open(p) as fp:
fp.read()
open(p).close()


# https://github.com/astral-sh/ruff/issues/15442
def _():
from builtins import open

with open(p) as _: ... # Error


def _():
from builtin import open

with open(p) as _: ... # No error
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ impl Violation for ExecBuiltin {

/// S102
pub(crate) fn exec_used(checker: &mut Checker, func: &Expr) {
if checker
.semantic()
.resolve_qualified_name(func)
.is_some_and(|qualified_name| matches!(qualified_name.segments(), ["" | "builtin", "exec"]))
{
if checker.semantic().match_builtin_expr(func, "exec") {
checker
.diagnostics
.push(Diagnostic::new(ExecBuiltin, func.range()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/flake8_bandit/mod.rs
snapshot_kind: text
---
S102.py:3:5: S102 Use of `exec` detected
|
Expand All @@ -19,3 +18,13 @@ S102.py:5:1: S102 Use of `exec` detected
5 | exec('y = 3')
| ^^^^ S102
|

S102.py:11:5: S102 Use of `exec` detected
|
9 | def _():
10 | from builtins import exec
11 | exec('') # Error
| ^^^^ S102
12 |
13 | def _():
|
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ pub(crate) fn replaceable_by_pathlib(checker: &mut Checker, call: &ExprCall) {
// PTH205
["os", "path", "getctime"] => Some(OsPathGetctime.into()),
// PTH123
["" | "builtin", "open"] => {
// `closefd` and `openener` are not supported by pathlib, so check if they are
["" | "builtins", "open"] => {
// `closefd` and `opener` are not supported by pathlib, so check if they are
// are set to non-default values.
// https://github.com/astral-sh/ruff/issues/7620
// Signature as of Python 3.11 (https://docs.python.org/3/library/functions.html#open):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/ruff_linter/src/rules/flake8_use_pathlib/mod.rs
snapshot_kind: text
---
import_from.py:9:5: PTH100 `os.path.abspath()` should be replaced by `Path.resolve()`
|
Expand Down Expand Up @@ -268,3 +267,11 @@ import_from.py:36:1: PTH123 `open()` should be replaced by `Path.open()`
36 | open(p).close()
| ^^^^ PTH123
|

import_from.py:43:10: PTH123 `open()` should be replaced by `Path.open()`
|
41 | from builtins import open
42 |
43 | with open(p) as _: ... # Error
| ^^^^ PTH123
|
Loading