Skip to content

Commit e741bd6

Browse files
committed
Skip coverage for intentionally partial branches
For `colon_seeker`, single line docstrings don't require rewinding so additional conditional branches aren't needed. Test cases exist for both single and multiline docstrings. For the branches in `FunctionVisitor`, we have only created a visitor for the 3 node types (class, function, async function) so additional conditionals aren't necessary. For the `ReturnVisitor`, we only care about `None` returns so no additional branches are necessary.
1 parent dbd1419 commit e741bd6

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

flake8_annotations/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def colon_seeker(node: AST_FUNCTION_TYPES, lines: List[str]) -> Tuple[int, int]:
271271
# the function node's body begins.
272272
# If the docstring is on one line then no rewinding is necessary.
273273
n_triple_quotes = lines[def_end_lineno].count('"""')
274-
if n_triple_quotes == 1:
274+
if n_triple_quotes == 1: # pragma: no branch
275275
# Docstring closure, rewind until the opening is found & take the line prior
276276
while True:
277277
def_end_lineno -= 1
@@ -434,6 +434,8 @@ def has_overload_decorator(function_node: AST_FUNCTION_TYPES) -> bool:
434434
class FunctionVisitor(ast.NodeVisitor):
435435
"""An ast.NodeVisitor instance for walking the AST and describing all contained functions."""
436436

437+
AST_FUNC_TYPES = (ast.FunctionDef, ast.AsyncFunctionDef)
438+
437439
def __init__(self, lines: List[str]):
438440
self.lines = lines
439441
self.function_definitions: List[Function] = []
@@ -448,15 +450,15 @@ def switch_context(self, node: AST_DEF_NODES) -> None:
448450
449451
Thank you for the inspiration @isidentical :)
450452
"""
451-
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
453+
if isinstance(node, self.AST_FUNC_TYPES):
452454
# Check for non-empty context first to prevent IndexErrors for non-nested nodes
453455
if self._context:
454456
if isinstance(self._context[-1], ast.ClassDef):
455457
# Check if current context is a ClassDef node & pass the appropriate flag
456458
self.function_definitions.append(
457459
Function.from_function_node(node, self.lines, is_class_method=True)
458460
)
459-
elif isinstance(self._context[-1], (ast.FunctionDef, ast.AsyncFunctionDef)):
461+
elif isinstance(self._context[-1], self.AST_FUNC_TYPES): # pragma: no branch
460462
# Check for nested function & pass the appropriate flag
461463
self.function_definitions.append(
462464
Function.from_function_node(node, self.lines, is_nested=True)
@@ -510,7 +512,7 @@ def visit_Return(self, node: ast.Return) -> None:
510512
# In the event of an explicit `None` return (`return None`), the node body will be an
511513
# instance of either `ast.Constant` (3.8+) or `ast.NameConstant`, which we need to check
512514
# to see if it's actually `None`
513-
if isinstance(node.value, (ast.Constant, ast.NameConstant)):
515+
if isinstance(node.value, (ast.Constant, ast.NameConstant)): # pragma: no branch
514516
if node.value.value is None:
515517
return
516518

0 commit comments

Comments
 (0)