Skip to content

Commit

Permalink
Update FURB149 to include == and != (#161):
Browse files Browse the repository at this point in the history
Also bump version for next release.
  • Loading branch information
dosisod authored Dec 19, 2022
1 parent f49cf24 commit 2e90c3c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "refurb"
version = "1.8.0"
version = "1.9.0"
description = "A tool for refurbish and modernize Python codebases"
authors = ["dosisod"]
license = "GPL-3.0-only"
Expand Down
18 changes: 8 additions & 10 deletions refurb/checks/readability/no_is_bool_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@dataclass
class ErrorInfo(Error):
"""
Don't use `is` or `is not` to check if a boolean is True or False, simply
Don't use `is` or `==` to check if a boolean is True or False, simply
use the name itself:
Bad:
Expand Down Expand Up @@ -49,27 +49,25 @@ def is_bool_variable(expr: Expression) -> bool:
return False


IS_TRUTHY: dict[tuple[str, str], bool] = {
("is", "True"): True,
("is", "False"): False,
("is not", "True"): False,
("is not", "False"): True,
}
def is_truthy(oper: str, name: str) -> bool:
value = name == "True"

return not value if oper in ("is not", "!=") else value


def check(node: ComparisonExpr, errors: list[Error]) -> None:
match node:
case ComparisonExpr(
operators=["is" | "is not" as oper],
operators=["is" | "is not" | "==" | "!=" as oper],
operands=[NameExpr() as lhs, NameExpr() as rhs],
):
if is_bool_literal(lhs) and is_bool_variable(rhs):
old = f"{lhs.name} {oper} x"
new = "x" if IS_TRUTHY[(oper, lhs.name)] else "not x"
new = "x" if is_truthy(oper, lhs.name) else "not x"

elif is_bool_variable(lhs) and is_bool_literal(rhs):
old = f"x {oper} {rhs.name}"
new = "x" if IS_TRUTHY[(oper, rhs.name)] else "not x"
new = "x" if is_truthy(oper, rhs.name) else "not x"

else:
return
Expand Down
8 changes: 8 additions & 0 deletions test/data/err_149.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
_ = b is not True
_ = b is not False
_ = True is b
_ = False is b

_ = b == True
_ = b == False
_ = b != True
_ = b != False
_ = True == b
_ = False == b


# these should not
Expand Down
7 changes: 7 additions & 0 deletions test/data/err_149.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@ test/data/err_149.py:6:5 [FURB149]: Replace `x is False` with `not x`
test/data/err_149.py:7:5 [FURB149]: Replace `x is not True` with `not x`
test/data/err_149.py:8:5 [FURB149]: Replace `x is not False` with `x`
test/data/err_149.py:9:5 [FURB149]: Replace `True is x` with `x`
test/data/err_149.py:10:5 [FURB149]: Replace `False is x` with `not x`
test/data/err_149.py:12:5 [FURB149]: Replace `x == True` with `x`
test/data/err_149.py:13:5 [FURB149]: Replace `x == False` with `not x`
test/data/err_149.py:14:5 [FURB149]: Replace `x != True` with `not x`
test/data/err_149.py:15:5 [FURB149]: Replace `x != False` with `x`
test/data/err_149.py:16:5 [FURB149]: Replace `True == x` with `x`
test/data/err_149.py:17:5 [FURB149]: Replace `False == x` with `not x`

0 comments on commit 2e90c3c

Please sign in to comment.