Skip to content

Commit

Permalink
Remove vacuuming up parent nodes when building Act block
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescooke committed May 3, 2020
1 parent 8f3c48f commit f9e5edb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
14 changes: 4 additions & 10 deletions src/flake8_aaa/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Iterable, List, Tuple, Type, TypeVar

from .exceptions import EmptyBlock
from .helpers import add_node_parents, filter_arrange_nodes, filter_assert_nodes, get_first_token, get_last_token
from .helpers import filter_arrange_nodes, filter_assert_nodes, get_first_token, get_last_token
from .types import LineType

_Block = TypeVar('_Block', bound='Block')
Expand All @@ -26,17 +26,11 @@ def __init__(self, nodes: Iterable[ast.AST], lt: LineType) -> None:
self.line_type = lt

@classmethod
def build_act(cls: Type[_Block], node: ast.stmt, test_func_node: ast.FunctionDef) -> _Block:
def build_act(cls: Type[_Block], node: ast.stmt) -> _Block:
"""
Act block is a single node - either the act node itself, or the node
that wraps the act node.
Act block is a single node.
"""
add_node_parents(test_func_node)
# Walk up the parent nodes of the parent node to find test's definition.
act_block_node = node
while act_block_node.parent != test_func_node: # type: ignore
act_block_node = act_block_node.parent # type: ignore
return cls([act_block_node], LineType.act)
return cls([node], LineType.act)

@classmethod
def build_arrange(cls: Type[_Block], nodes: List[ast.stmt], max_line_number: int) -> _Block:
Expand Down
2 changes: 1 addition & 1 deletion src/flake8_aaa/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def check_all(self) -> Generator[AAAError, None, None]:
# ACT
# Load act block and kick out when none is found
self.act_node = self.load_act_node()
self.act_block = Block.build_act(self.act_node.node, self.node)
self.act_block = Block.build_act(self.act_node.node)
act_block_first_line_no, act_block_last_line_no = self.act_block.get_span(0)
# ARRANGE
self.arrange_block = Block.build_arrange(self.node.body, act_block_first_line_no)
Expand Down
7 changes: 5 additions & 2 deletions tests/block/test_build_act.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ def test():
]
)
def test(first_node_with_tokens):
"""
`pytest.raises()` with statement is the Act node.
"""
with_mock_node = first_node_with_tokens.body[0]
with_pytest_node = with_mock_node.body[0]

result = Block.build_act(with_pytest_node, first_node_with_tokens)
result = Block.build_act(with_pytest_node)

assert result.nodes == (with_mock_node, )
assert result.nodes == (with_pytest_node, )
assert result.line_type == LineType.act

0 comments on commit f9e5edb

Please sign in to comment.