Skip to content

Commit

Permalink
remove deprecated ast references (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile authored Jun 13, 2023
1 parent 836631f commit ad5e15f
Showing 1 changed file with 43 additions and 45 deletions.
88 changes: 43 additions & 45 deletions pyflakes/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,12 @@ def iter_child_nodes(node, omit=None, _fields_order=_FieldsOrder()):


def convert_to_value(item):
if isinstance(item, ast.Str):
return item.s
elif hasattr(ast, 'Bytes') and isinstance(item, ast.Bytes):
return item.s
if isinstance(item, ast.Constant):
return item.value
elif isinstance(item, ast.Tuple):
return tuple(convert_to_value(i) for i in item.elts)
elif isinstance(item, ast.Num):
return item.n
elif isinstance(item, ast.Name):
result = VariableKey(item=item)
constants_lookup = {
'True': True,
'False': False,
'None': None,
}
return constants_lookup.get(
result.name,
result,
)
elif isinstance(item, ast.NameConstant):
return item.value
return VariableKey(item=item)
else:
return UnhandledKeyType()

Expand Down Expand Up @@ -517,8 +502,8 @@ def __init__(self, name, source, scope):

def _add_to_names(container):
for node in container.elts:
if isinstance(node, ast.Str):
self.names.append(node.s)
if isinstance(node, ast.Constant) and isinstance(node.value, str):
self.names.append(node.value)

if isinstance(source.value, (ast.List, ast.Tuple)):
_add_to_names(source.value)
Expand Down Expand Up @@ -1229,25 +1214,34 @@ def isDocstring(self, node):
Determine if the given node is a docstring, as long as it is at the
correct place in the node tree.
"""
return isinstance(node, ast.Str) or (isinstance(node, ast.Expr) and
isinstance(node.value, ast.Str))
return (
isinstance(node, ast.Expr) and
isinstance(node.value, ast.Constant) and
isinstance(node.value.value, str)
)

def getDocstring(self, node):
if isinstance(node, ast.Expr):
node = node.value
if not isinstance(node, ast.Str):
return (None, None)

return (node.s, node.lineno - 1)
if (
isinstance(node, ast.Expr) and
isinstance(node.value, ast.Constant) and
isinstance(node.value.value, str)
):
return node.value.value, node.lineno - 1
else:
return None, None

def handleNode(self, node, parent):
if node is None:
return
if self.offset and getattr(node, 'lineno', None) is not None:
node.lineno += self.offset[0]
node.col_offset += self.offset[1]
if self.futuresAllowed and not (isinstance(node, ast.ImportFrom) or
self.isDocstring(node)):
if (
self.futuresAllowed and
self.nodeDepth == 0 and
not isinstance(node, ast.ImportFrom) and
not self.isDocstring(node)
):
self.futuresAllowed = False
self.nodeDepth += 1
node._pyflakes_depth = self.nodeDepth
Expand Down Expand Up @@ -1318,11 +1312,14 @@ def handleStringAnnotation(self, s, node, ref_lineno, ref_col_offset, err):

@in_annotation
def handleAnnotation(self, annotation, node):
if isinstance(annotation, ast.Str):
if (
isinstance(annotation, ast.Constant) and
isinstance(annotation.value, str)
):
# Defer handling forward annotation.
self.deferFunction(functools.partial(
self.handleStringAnnotation,
annotation.s,
annotation.value,
node,
annotation.lineno,
annotation.col_offset,
Expand Down Expand Up @@ -1387,7 +1384,7 @@ def SUBSCRIPT(self, node):

def _handle_string_dot_format(self, node):
try:
placeholders = tuple(parse_format_string(node.func.value.s))
placeholders = tuple(parse_format_string(node.func.value.value))
except ValueError as e:
self.report(messages.StringDotFormatInvalidFormat, node, e)
return
Expand Down Expand Up @@ -1503,7 +1500,8 @@ def _add_key(fmtkey):
def CALL(self, node):
if (
isinstance(node.func, ast.Attribute) and
isinstance(node.func.value, ast.Str) and
isinstance(node.func.value, ast.Constant) and
isinstance(node.func.value.value, str) and
node.func.attr == 'format'
):
self._handle_string_dot_format(node)
Expand Down Expand Up @@ -1584,7 +1582,7 @@ def CALL(self, node):

def _handle_percent_format(self, node):
try:
placeholders = parse_percent_format(node.left.s)
placeholders = parse_percent_format(node.left.value)
except ValueError:
self.report(
messages.PercentFormatInvalidFormat,
Expand Down Expand Up @@ -1663,13 +1661,16 @@ def _handle_percent_format(self, node):

if (
isinstance(node.right, ast.Dict) and
all(isinstance(k, ast.Str) for k in node.right.keys)
all(
isinstance(k, ast.Constant) and isinstance(k.value, str)
for k in node.right.keys
)
):
if positional and positional_count > 1:
self.report(messages.PercentFormatExpectedSequence, node)
return

substitution_keys = {k.s for k in node.right.keys}
substitution_keys = {k.value for k in node.right.keys}
extra_keys = substitution_keys - named
missing_keys = named - substitution_keys
if not positional and extra_keys:
Expand All @@ -1688,27 +1689,24 @@ def _handle_percent_format(self, node):
def BINOP(self, node):
if (
isinstance(node.op, ast.Mod) and
isinstance(node.left, ast.Str)
isinstance(node.left, ast.Constant) and
isinstance(node.left.value, str)
):
self._handle_percent_format(node)
self.handleChildren(node)

def STR(self, node):
if self._in_annotation:
def CONSTANT(self, node):
if isinstance(node.value, str) and self._in_annotation:
fn = functools.partial(
self.handleStringAnnotation,
node.s,
node.value,
node,
node.lineno,
node.col_offset,
messages.ForwardAnnotationSyntaxError,
)
self.deferFunction(fn)

def CONSTANT(self, node):
if isinstance(node.value, str):
return self.STR(node)

# "slice" type nodes
SLICE = EXTSLICE = INDEX = handleChildren

Expand Down

0 comments on commit ad5e15f

Please sign in to comment.