-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move common function annotation walking into utility function
- Loading branch information
Showing
2 changed files
with
34 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from __future__ import annotations | ||
|
||
from itertools import chain | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
import ast | ||
from ast import AsyncFunctionDef, FunctionDef | ||
from collections.abc import Iterator | ||
|
||
|
||
def iter_function_annotation_nodes(node: AsyncFunctionDef | FunctionDef) -> Iterator[ast.expr]: | ||
"""Yield all the annotation expression nodes inside the given function node.""" | ||
for arg in chain(node.args.args, node.args.kwonlyargs, node.args.posonlyargs): | ||
if arg.annotation: | ||
yield arg.annotation | ||
|
||
for opt_arg in (node.args.kwarg, node.args.vararg): | ||
if opt_arg and opt_arg.annotation: | ||
yield opt_arg.annotation | ||
|
||
if node.returns: | ||
yield node.returns |