Skip to content

Commit

Permalink
Merge branch 'main' into print-empty-string
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Sep 24, 2023
2 parents 6cab211 + b194f59 commit b7b328f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@

# Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458885
assert getattr(func, '_rpc')is True

# Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387247
getattr(*foo, "bar")
setattr(*foo, "bar", None)
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ pub(crate) fn getattr_with_constant(
let [obj, arg] = args else {
return;
};
if obj.is_starred_expr() {
return;
}
let Expr::Constant(ast::ExprConstant {
value: Constant::Str(ast::StringConstant { value, .. }),
..
Expand All @@ -77,6 +80,9 @@ pub(crate) fn getattr_with_constant(
if is_mangled_private(value) {
return;
}
if !checker.semantic().is_builtin("getattr") {
return;
}

let mut diagnostic = Diagnostic::new(GetAttrWithConstant, expr.range());
if checker.patch(diagnostic.kind.rule()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ pub(crate) fn setattr_with_constant(
let [obj, name, value] = args else {
return;
};
if obj.is_starred_expr() {
return;
}
let Expr::Constant(ast::ExprConstant {
value: Constant::Str(name),
..
Expand All @@ -91,6 +94,10 @@ pub(crate) fn setattr_with_constant(
if is_mangled_private(name) {
return;
}
if !checker.semantic().is_builtin("setattr") {
return;
}

// We can only replace a `setattr` call (which is an `Expr`) with an assignment
// (which is a `Stmt`) if the `Expr` is already being used as a `Stmt`
// (i.e., it's directly within an `Stmt::Expr`).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ B009_B010.py:58:8: B009 [*] Do not call `getattr` with a constant attribute valu
57 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458885
58 | assert getattr(func, '_rpc')is True
| ^^^^^^^^^^^^^^^^^^^^^ B009
59 |
60 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387247
|
= help: Replace `getattr` with attribute access

Expand All @@ -330,5 +332,8 @@ B009_B010.py:58:8: B009 [*] Do not call `getattr` with a constant attribute valu
57 57 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722458885
58 |-assert getattr(func, '_rpc')is True
58 |+assert func._rpc is True
59 59 |
60 60 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1732387247
61 61 | getattr(*foo, "bar")


2 changes: 1 addition & 1 deletion crates/ruff_python_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,7 @@ def f(arg=%timeit a = b):
// This test case is to just make sure that the lexer doesn't go into
// infinite loop on invalid input.
#[test]
fn test_infite_loop() {
fn test_infinite_loop() {
let source = "[1";
let _ = lex(source, Mode::Module).collect::<Vec<_>>();
}
Expand Down

0 comments on commit b7b328f

Please sign in to comment.