Skip to content

Commit

Permalink
Merge pull request #154 from jamescooke/fix_deprecation
Browse files Browse the repository at this point in the history
Fix deprecation warning in AST in Python 3.8
  • Loading branch information
jamescooke committed May 31, 2020
2 parents 6e73230 + 67e959a commit 1e50a99
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Unreleased_
See also `latest documentation
<https://flake8-aaa.readthedocs.io/en/latest/#__unreleased_marker__>`_.

Changed
.......

* Stringy line analysis adjusted to use Constant visitor since Str visitor is
deprecated as of Python 3.8. `#145
<https://github.com/jamescooke/flake8-aaa/issues/145>`_.

0.10.0_ - 2020/05/24
--------------------

Expand Down
23 changes: 21 additions & 2 deletions src/flake8_aaa/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import io
import os
import re
import sys
import tokenize
from typing import List, Set

Expand Down Expand Up @@ -187,15 +188,25 @@ def filter_assert_nodes(nodes: List[ast.stmt], min_line_number: int) -> List[ast
class StringyLineVisitor(ast.NodeVisitor):
"""
Find lines that look like strings. For each found, calculate its footprint.
Note:
Python 3.8.0 is starting to give warnings about `visit_Str` being
deprecated, but it's still required for 3.6. So some patching is used
to adjust this visitor for versions before 3.8.
"""

def __init__(self, first_line_no: int):
super().__init__()
self.first_line_no: int = first_line_no
self.footprints: Set[int] = set()

def visit_Str(self, node) -> None:
self.add_footprint(node)
def visit_Constant(self, node) -> None:
"""
Does not produce values until Python 3.8. See
https://greentreesnakes.readthedocs.io/en/latest/nodes.html#Constant
"""
if isinstance(node.value, str):
self.add_footprint(node)

def visit_JoinedStr(self, node) -> None:
self.add_footprint(node)
Expand All @@ -204,6 +215,14 @@ def add_footprint(self, node) -> None:
self.footprints.update(build_footprint(node, self.first_line_no))


if sys.version_info < (3, 8, 0):

def visit_Str(self, node) -> None:
self.add_footprint(node)

StringyLineVisitor.visit_Str = visit_Str # type: ignore


def find_stringy_lines(tree: ast.AST, first_line_no: int) -> Set[int]:
"""
Finds all lines that contain a string in a tree, usually a function. These
Expand Down

0 comments on commit 1e50a99

Please sign in to comment.