Skip to content

Commit

Permalink
Use new is_same_type() function in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
dosisod committed Feb 8, 2024
1 parent e178921 commit 02adfc1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 33 deletions.
2 changes: 2 additions & 0 deletions refurb/checks/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,9 @@ def is_same_type(ty: Type | TypeInfo | None, *expected: TypeLike) -> bool:
"builtins.bool": bool,
"builtins.bytearray": bytearray,
"builtins.bytes": bytes,
"builtins.complex": complex,
"builtins.dict": dict,
"builtins.float": float,
"builtins.frozenset": frozenset,
"builtins.int": int,
"builtins.list": list,
Expand Down
28 changes: 16 additions & 12 deletions refurb/checks/readability/no_or_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,36 +52,40 @@ def check(node: OpExpr, errors: list[Error]) -> None:
case (NameExpr(node=Var(type=ty)) as lhs, arg):
match arg:
case CallExpr(callee=NameExpr(fullname=fullname), args=[]):
pass
if fullname == "builtins.set":
expected_type: type = set

elif fullname == "builtins.frozenset":
expected_type = frozenset

else:
return

case ListExpr(items=[]):
fullname = "builtins.list"
expected_type = list

case DictExpr(items=[]):
fullname = "builtins.dict"
expected_type = dict

case TupleExpr(items=[]):
fullname = "builtins.tuple"
expected_type = tuple

case StrExpr(value=""):
fullname = "builtins.str"
expected_type = str

case BytesExpr(value=""):
fullname = "builtins.bytes"
expected_type = bytes

case IntExpr(value=0):
fullname = "builtins.int"
expected_type = int

case NameExpr(fullname="builtins.False"):
fullname = "builtins.bool"
expected_type = bool

case _:
return

type_name = "builtins.tuple" if is_same_type(ty, tuple) else str(ty)

# Must check fullname for compatibility with older Mypy versions
if fullname and type_name.startswith(fullname):
if is_same_type(ty, expected_type):
lhs_expr = stringify(lhs)
rhs_expr = stringify(arg)

Expand Down
36 changes: 15 additions & 21 deletions refurb/checks/readability/no_unnecessary_cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,44 +59,38 @@ class ErrorInfo(Error):
categories = ("readability",)


FUNC_NAMES = {
"builtins.bool": (None, ""),
"builtins.bytes": (BytesExpr, ""),
"builtins.complex": (ComplexExpr, ""),
"builtins.dict": (DictExpr, ".copy()"),
"builtins.float": (FloatExpr, ""),
"builtins.int": (IntExpr, ""),
"builtins.list": (ListExpr, ".copy()"),
"builtins.str": (StrExpr, ""),
"builtins.tuple": (TupleExpr, ""),
"tuple[]": (TupleExpr, ""),
FUNC_NAME_MAPPING = {
"builtins.bool": (bool, None, ""),
"builtins.bytes": (bytes, BytesExpr, ""),
"builtins.complex": (complex, ComplexExpr, ""),
"builtins.dict": (dict, DictExpr, ".copy()"),
"builtins.float": (float, FloatExpr, ""),
"builtins.int": (int, IntExpr, ""),
"builtins.list": (list, ListExpr, ".copy()"),
"builtins.str": (str, StrExpr, ""),
"builtins.tuple": (tuple, TupleExpr, ""),
}


def is_boolean_literal(node: Expression) -> bool:
return isinstance(node, NameExpr) and node.fullname in {
"builtins.True",
"builtins.False",
}
return isinstance(node, NameExpr) and node.fullname in {"builtins.True", "builtins.False"}


def check(node: CallExpr, errors: list[Error]) -> None:
match node:
case CallExpr(
callee=NameExpr(fullname=fullname, name=name),
args=[arg],
arg_kinds=[arg_kind],
) if arg_kind != ArgKind.ARG_STAR2 and fullname in FUNC_NAMES:
node_type, suffix = FUNC_NAMES[fullname]
arg_kinds=[ArgKind.ARG_POS],
) if found := FUNC_NAME_MAPPING.get(fullname):
expected_type, node_type, suffix = found

if (type(arg) == node_type) or (is_boolean_literal(arg) and name == "bool"):
pass

else:
match arg:
case NameExpr(node=Var(type=ty)) if (
str(ty).startswith(fullname) or is_same_type(ty, tuple)
):
case NameExpr(node=Var(type=ty)) if is_same_type(ty, expected_type):
pass

case _:
Expand Down
2 changes: 2 additions & 0 deletions test/data/err_123.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@
_ = tuple([1, 2, 3])
_ = int("0xFF")
_ = dict(**d) # noqa: FURB173
_ = int(t) # type: ignore
_ = int(*123) # type: ignore
1 change: 1 addition & 0 deletions test/data/err_143.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ def __init__(self) -> None:
_ = b or b"abc"
_ = n or True
_ = i or 123
_ = i or print()

0 comments on commit 02adfc1

Please sign in to comment.