Coverage for src/_griffe/agents/nodes/parameters.py: 100.00%
20 statements
« prev ^ index » next coverage.py v7.6.2, created at 2024-10-12 01:34 +0200
« prev ^ index » next coverage.py v7.6.2, created at 2024-10-12 01:34 +0200
1# This module contains utilities for extracting information from parameter nodes.
3from __future__ import annotations
5import ast
6from itertools import zip_longest
7from typing import TYPE_CHECKING, Optional, Union
9from _griffe.enumerations import ParameterKind
11if TYPE_CHECKING:
12 from collections.abc import Iterable
14ParametersType = list[tuple[str, Optional[ast.AST], ParameterKind, Optional[Union[str, ast.AST]]]]
15"""Type alias for the list of parameters of a function."""
18def get_parameters(node: ast.arguments) -> ParametersType:
19 parameters: ParametersType = []
21 # TODO: probably some optimizations to do here
22 args_kinds_defaults: Iterable = reversed(
23 (
24 *zip_longest(
25 reversed(
26 (
27 *zip_longest(
28 node.posonlyargs,
29 [],
30 fillvalue=ParameterKind.positional_only,
31 ),
32 *zip_longest(node.args, [], fillvalue=ParameterKind.positional_or_keyword),
33 ),
34 ),
35 reversed(node.defaults),
36 fillvalue=None,
37 ),
38 ),
39 )
40 arg: ast.arg
41 kind: ParameterKind
42 arg_default: ast.AST | None
43 for (arg, kind), arg_default in args_kinds_defaults:
44 parameters.append((arg.arg, arg.annotation, kind, arg_default))
46 if node.vararg:
47 parameters.append(
48 (
49 node.vararg.arg,
50 node.vararg.annotation,
51 ParameterKind.var_positional,
52 "()",
53 ),
54 )
56 # TODO: probably some optimizations to do here
57 kwargs_defaults: Iterable = reversed(
58 (
59 *zip_longest(
60 reversed(node.kwonlyargs),
61 reversed(node.kw_defaults),
62 fillvalue=None,
63 ),
64 ),
65 )
66 kwarg: ast.arg
67 kwarg_default: ast.AST | None
68 for kwarg, kwarg_default in kwargs_defaults:
69 parameters.append(
70 (kwarg.arg, kwarg.annotation, ParameterKind.keyword_only, kwarg_default),
71 )
73 if node.kwarg:
74 parameters.append(
75 (
76 node.kwarg.arg,
77 node.kwarg.annotation,
78 ParameterKind.var_keyword,
79 "{}",
80 ),
81 )
83 return parameters