Skip to content

Commit 7f93c93

Browse files
authored
Merge pull request #123 from jamescooke/type-assertions-bug
Fix annotated assignment Act block search
2 parents 71b9504 + e74867c commit 7f93c93

File tree

7 files changed

+78
-7
lines changed

7 files changed

+78
-7
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ Unreleased_
1414
See also `latest documentation
1515
<https://flake8-aaa.readthedocs.io/en/latest/>`_.
1616

17+
Fixed
18+
.....
19+
20+
* NEEDS CONFIRMATION: Bug preventing type annotated assignment Act blocks from being found `#123
21+
<https://github.com/jamescooke/flake8-aaa/pull/123>`_
22+
1723
0.7.1_ - 2019/11/16
1824
-------------------
1925

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def add_one(x: int) -> int:
2+
return x + 1
3+
4+
5+
# Example from https://github.com/jamescooke/flake8-aaa/issues/122
6+
7+
8+
def test_add_one() -> None:
9+
"""
10+
Addition of type hint in act block does not prevent act block being found
11+
"""
12+
# Arrange.
13+
data: int = 0
14+
15+
# Act.
16+
result: int = add_one(data)
17+
18+
# Assert.
19+
assert result != data
20+
assert result == data + 1

src/flake8_aaa/helpers.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,19 @@ def node_is_result_assignment(node: ast.AST) -> bool:
8787
Returns:
8888
bool: ``node`` corresponds to the code ``result =``, assignment to the
8989
``result `` variable.
90-
91-
Note:
92-
Performs a very weak test that the line starts with 'result =' rather
93-
than testing the tokens.
9490
"""
95-
# `.first_token` is added by asttokens
96-
token = node.first_token # type: ignore
97-
return token.line.strip().startswith('result =')
91+
if isinstance(node, ast.Assign):
92+
return len(node.targets) == 1 and isinstance(node.targets[0], ast.Name) and node.targets[0].id == "result"
93+
94+
# py35 has no Annotated Assignment, so work around it...
95+
try:
96+
ann_assign_cls = getattr(ast, 'AnnAssign')
97+
except AttributeError:
98+
return False
99+
100+
if isinstance(node, ann_assign_cls):
101+
return node.target.id == "result" # type: ignore
102+
return False
98103

99104

100105
def node_is_pytest_raises(node: ast.AST) -> bool:

tests_py36_plus/act_node/__init__.py

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
3+
from flake8_aaa.act_node import ActNode
4+
from flake8_aaa.types import ActNodeType
5+
6+
7+
@pytest.mark.parametrize(
8+
'code_str, expected_type', [
9+
('result: Thing = do_thing()', ActNodeType.result_assignment),
10+
('result: List[int] = do_thing()', ActNodeType.result_assignment),
11+
]
12+
)
13+
def test(expected_type, first_node_with_tokens):
14+
"""
15+
Assert that type-hinted result assignments can be found as act blocks
16+
"""
17+
result: ActNode = ActNode.build(first_node_with_tokens)
18+
19+
assert isinstance(result, list)
20+
assert len(result) == 1
21+
assert isinstance(result[0], ActNode)
22+
assert result[0].node == first_node_with_tokens
23+
assert result[0].block_type == expected_type

tests_py36_plus/helpers/__init__.py

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from flake8_aaa.helpers import node_is_result_assignment
4+
5+
6+
@pytest.mark.parametrize(
7+
('code_str', 'expected_result'),
8+
[
9+
('result: int = 1', True),
10+
('result: List[int] = [1]', True),
11+
('xresult: int = 1', False),
12+
],
13+
)
14+
def test_no(first_node_with_tokens, expected_result):
15+
result = node_is_result_assignment(first_node_with_tokens)
16+
17+
assert result is expected_result

0 commit comments

Comments
 (0)