Skip to content

Commit

Permalink
Merge pull request #128 from jamescooke/unpick-py35-fixes
Browse files Browse the repository at this point in the history
Unpick Python 3.5 fixes
  • Loading branch information
jamescooke committed Feb 27, 2020
2 parents c43f9fb + 0621495 commit c9ec0ac
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 117 deletions.
15 changes: 3 additions & 12 deletions src/flake8_aaa/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,9 @@ def node_is_result_assignment(node: ast.AST) -> bool:
if isinstance(node, ast.Assign):
return len(node.targets) == 1 and isinstance(node.targets[0], ast.Name) and node.targets[0].id == "result"

# py35 has no Annotated Assignment, so work around it...
try:
ann_assign_cls = getattr(ast, 'AnnAssign')
except AttributeError:
return False

if isinstance(node, ann_assign_cls):
if isinstance(node, ast.AnnAssign):
return node.target.id == "result" # type: ignore

return False


Expand Down Expand Up @@ -209,10 +204,6 @@ def filter_assert_nodes(nodes: List[ast.stmt], min_line_number: int) -> List[ast
return [node for node in nodes if node.lineno > min_line_number]


# py35 sees this as a const
JoinedStrType = getattr(ast, 'JoinedStr', None)


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 All @@ -226,7 +217,7 @@ def find_stringy_lines(tree: ast.AST, first_line_no: int) -> Set[int]:
"""
str_footprints = set()
for node in ast.walk(tree):
if isinstance(node, ast.Str) or (JoinedStrType is not None and isinstance(node, JoinedStrType)):
if isinstance(node, ast.Str) or isinstance(node, ast.JoinedStr):
try:
str_footprints.update(build_footprint(node, first_line_no))
except AttributeError:
Expand Down
2 changes: 2 additions & 0 deletions tests/act_node/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def test_unittest_raises_block(first_node_with_tokens):
('result = do_thing()', ActNodeType.result_assignment),
('with pytest.raises(Exception):\n do_thing()', ActNodeType.pytest_raises),
('data[new_key] = value # act', ActNodeType.marked_act),
('result: Thing = do_thing()', ActNodeType.result_assignment),
('result: List[int] = do_thing()', ActNodeType.result_assignment),
]
)
def test(expected_type, first_node_with_tokens):
Expand Down
22 changes: 22 additions & 0 deletions tests/helpers/test_build_footprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,25 @@ def test(first_node_with_tokens, first_line_no, expected_lines):
result = build_footprint(first_test_node, first_line_no)

assert result == expected_lines


@pytest.mark.parametrize(
'code_str, first_line_no, expected_lines', [
(
'''
def test_f_string_check(version): # 0
result = do(
f"""/path/to/folder/
{version}/thing.py""", # 4
)
''', 2, set([2, 3, 4])
),
]
)
def test_f_string(first_node_with_tokens, first_line_no, expected_lines):
f_string_node = first_node_with_tokens.body[0].value.args[0]

result = build_footprint(f_string_node, first_line_no)

assert result == expected_lines
12 changes: 12 additions & 0 deletions tests/helpers/test_find_stringy_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ def test_other(m): # 3
""" # 16
''', 2, set([0, 1, 2, 4, 5, 6, 7, 8, 13, 14, 15, 16])
),
(
'''
def test_f_string_check(version): # 0
result = do(
f"""/path/to/folder/
{version}/thing.py""", # 4
)
assert result is True
''', 2, set([2, 3, 4])
),
]
)
def test(first_node_with_tokens, offset, expected_lines):
Expand Down
3 changes: 3 additions & 0 deletions tests/helpers/test_get_first_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ def test(first_node_with_tokens):
('''
r'hello world'
''', 'r\'hello world\''),
('''
f"hello world"
''', 'f"hello world"'),
]
)
def test_strings(first_node_with_tokens, expected):
Expand Down
3 changes: 3 additions & 0 deletions tests/helpers/test_node_is_result_assignment.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
('result[0] = 0', False),
('result += 1', False),
('result -= 1', False),
('result: int = 1', True),
('result: List[int] = [1]', True),
('xresult: int = 1', False),
],
)
def test_no(first_node_with_tokens, expected_result):
Expand Down
23 changes: 0 additions & 23 deletions tests_py36_plus/act_node/test_build.py

This file was deleted.

17 changes: 0 additions & 17 deletions tests_py36_plus/helpers/test_node_is_result_assignment.py

This file was deleted.

64 changes: 0 additions & 64 deletions tests_py36_plus/test_f_strings.py

This file was deleted.

1 change: 0 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ commands =
lint: make lint
lintexamples: make lintexamples
test: pytest {posargs:tests}
test: pytest tests_py36_plus
skip_install =
lint: true
lintexamples: true
Expand Down

0 comments on commit c9ec0ac

Please sign in to comment.