Skip to content

Commit

Permalink
(3.8) Replace deprecated ast literals (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
FozzieHi authored Mar 14, 2023
1 parent b4404df commit a110333
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _is_identifier(arg):
# Return True if arg is a valid identifier, per
# https://docs.python.org/2/reference/lexical_analysis.html#identifiers

if not isinstance(arg, ast.Str):
if not isinstance(arg, ast.Constant) or not isinstance(arg.value, str):
return False

return re.match(r"^[A-Za-z_][A-Za-z0-9_]*$", arg.s) is not None
Expand Down Expand Up @@ -526,7 +526,11 @@ def check_for_b005(self, node):
):
return # method is being run on an imported module

if len(node.args) != 1 or not isinstance(node.args[0], ast.Str):
if (
len(node.args) != 1
or not isinstance(node.args[0], ast.Constant)
or not isinstance(node.args[0].value, str)
):
return # used arguments don't match the builtin strip

call_path = ".".join(compose_call_path(node.func.value))
Expand Down Expand Up @@ -560,7 +564,7 @@ def check_for_b007(self, node):
self.errors.append(B007(n.lineno, n.col_offset, vars=(name,)))

def check_for_b011(self, node):
if isinstance(node.test, ast.NameConstant) and node.test.value is False:
if isinstance(node.test, ast.Constant) and node.test.value is False:
self.errors.append(B011(node.lineno, node.col_offset))

def check_for_b012(self, node):
Expand All @@ -585,7 +589,13 @@ def check_for_b015(self, node):
self.errors.append(B015(node.lineno, node.col_offset))

def check_for_b016(self, node):
if isinstance(node.exc, (ast.NameConstant, ast.Num, ast.Str, ast.JoinedStr)):
if isinstance(node.exc, ast.JoinedStr) or (
isinstance(node.exc, ast.Constant)
and (
isinstance(node.exc.value, (int, float, complex, str, bool))
or node.exc.value is None
)
):
self.errors.append(B016(node.lineno, node.col_offset))

def check_for_b017(self, node):
Expand Down Expand Up @@ -768,10 +778,8 @@ def is_overload(expr):

def empty_body(body) -> bool:
def is_str_or_ellipsis(node):
# ast.Ellipsis and ast.Str used in python<3.8
return isinstance(node, (ast.Ellipsis, ast.Str)) or (
isinstance(node, ast.Constant)
and (node.value is Ellipsis or isinstance(node.value, str))
return isinstance(node, ast.Constant) and (
node.value is Ellipsis or isinstance(node.value, str)
)

# Function body consist solely of `pass`, `...`, and/or (doc)string literals
Expand Down Expand Up @@ -1020,7 +1028,8 @@ def check_for_b903(self, node):
if (
body
and isinstance(body[0], ast.Expr)
and isinstance(body[0].value, ast.Str)
and isinstance(body[0].value, ast.Constant)
and isinstance(body[0].value.value, str)
):
# Ignore the docstring
body = body[1:]
Expand Down Expand Up @@ -1052,13 +1061,16 @@ def check_for_b018(self, node):
if isinstance(
subnode.value,
(
ast.Num,
ast.Bytes,
ast.NameConstant,
ast.List,
ast.Set,
ast.Dict,
),
) or (
isinstance(subnode.value, ast.Constant)
and (
isinstance(subnode.value.value, (int, float, complex, bytes, bool))
or subnode.value.value is None
)
):
self.errors.append(B018(subnode.lineno, subnode.col_offset))

Expand Down

0 comments on commit a110333

Please sign in to comment.