1919from __future__ import annotations
2020
2121import ast
22- from itertools import zip_longest
23- from typing import TYPE_CHECKING
2422
2523from griffe ._internal .enumerations import ParameterKind
2624
27- if TYPE_CHECKING :
28- from collections .abc import Iterable
29-
3025ParametersType = list [tuple [str , ast .AST | None , ParameterKind , str | ast .AST | None ]]
3126"""Type alias for the list of parameters of a function."""
3227
3328
3429def get_parameters (node : ast .arguments ) -> ParametersType :
3530 parameters : ParametersType = []
3631
37- # TODO: Probably some optimizations to do here.
38- args_kinds_defaults : Iterable = reversed (
39- (
40- * zip_longest (
41- reversed (
42- (
43- * zip_longest (
44- node .posonlyargs ,
45- [],
46- fillvalue = ParameterKind .positional_only ,
47- ),
48- * zip_longest (node .args , [], fillvalue = ParameterKind .positional_or_keyword ),
49- ),
50- ),
51- reversed (node .defaults ),
52- fillvalue = None ,
53- ),
54- ),
55- )
56- arg : ast .arg
57- kind : ParameterKind
58- arg_default : ast .AST | None
59- for (arg , kind ), arg_default in args_kinds_defaults : # ty:ignore[invalid-assignment,not-iterable]
60- parameters .append ((arg .arg , arg .annotation , kind , arg_default ))
32+ # Python stores positional-only and positional-or-keyword arguments in separate lists,
33+ # but stores their defaults together. The defaults are right-aligned with the combined
34+ # positional parameters, so `default_offset` is the index of the first parameter with
35+ # a default value.
36+ positional_count = len (node .posonlyargs ) + len (node .args )
37+ default_offset = positional_count - len (node .defaults )
38+
39+ # Keep one index across both positional argument lists to preserve that alignment.
40+ index = 0
6141
42+ for arg in node .posonlyargs :
43+ default = node .defaults [index - default_offset ] if index >= default_offset else None
44+ parameters .append ((arg .arg , arg .annotation , ParameterKind .positional_only , default ))
45+ index += 1
46+
47+ for arg in node .args :
48+ default = node .defaults [index - default_offset ] if index >= default_offset else None
49+ parameters .append ((arg .arg , arg .annotation , ParameterKind .positional_or_keyword , default ))
50+ index += 1
51+
52+ # Variadic positional parameters have no AST default. Use an empty tuple as their
53+ # implicit default so they are not treated as required parameters later on.
6254 if node .vararg :
6355 parameters .append (
6456 (
@@ -69,23 +61,14 @@ def get_parameters(node: ast.arguments) -> ParametersType:
6961 ),
7062 )
7163
72- # TODO: Probably some optimizations to do here.
73- kwargs_defaults : Iterable = reversed (
74- (
75- * zip_longest (
76- reversed (node .kwonlyargs ),
77- reversed (node .kw_defaults ),
78- fillvalue = None ,
79- ),
80- ),
81- )
82- kwarg : ast .arg
83- kwarg_default : ast .AST | None
84- for kwarg , kwarg_default in kwargs_defaults : # ty:ignore[invalid-assignment]
64+ # Unlike positional defaults, keyword-only defaults are stored one-to-one with their
65+ # arguments. A `None` entry means that the keyword-only parameter is required.
66+ for kwarg , kwarg_default in zip (node .kwonlyargs , node .kw_defaults , strict = False ):
8567 parameters .append (
8668 (kwarg .arg , kwarg .annotation , ParameterKind .keyword_only , kwarg_default ),
8769 )
8870
71+ # Likewise, use an empty mapping as the implicit default for variadic keyword arguments.
8972 if node .kwarg :
9073 parameters .append (
9174 (
0 commit comments