Skip to content

Commit

Permalink
Make Arguments.defaults None for uninferable signatures (#1595)
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Jan 2, 2023
1 parent d9246cf commit 1054a35
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ Release date: 2022-07-09

Closes #1559

* ``Arguments.defaults`` is now ``None`` for uninferable signatures.

* On Python versions >= 3.9, ``astroid`` now understands subscripting
builtin classes such as ``enumerate`` or ``staticmethod``.

Expand Down
7 changes: 4 additions & 3 deletions astroid/nodes/node_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ def __init__(
# TODO: Check if other attributes should also be None when
# .args is None.

self.defaults: list[NodeNG]
self.defaults: list[NodeNG] | None
"""The default values for arguments that can be passed positionally."""

self.kwonlyargs: list[AssignName]
Expand Down Expand Up @@ -693,7 +693,7 @@ def __init__(
def postinit(
self,
args: list[AssignName] | None,
defaults: list[NodeNG],
defaults: list[NodeNG] | None,
kwonlyargs: list[AssignName],
kw_defaults: list[NodeNG | None],
annotations: list[NodeNG | None],
Expand Down Expand Up @@ -975,7 +975,8 @@ def get_children(self):

yield from self.args or ()

yield from self.defaults
if self.defaults is not None:
yield from self.defaults
yield from self.kwonlyargs

for elt in self.kw_defaults:
Expand Down
16 changes: 11 additions & 5 deletions astroid/raw_building.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,18 @@ def build_function(
else:
arguments = None

default_nodes: list[nodes.NodeNG] | None = []
if defaults is not None:
for default in defaults:
default_node = nodes.const_factory(default)
default_node.parent = argsnode
default_nodes.append(default_node)
else:
default_nodes = None

argsnode.postinit(
args=arguments,
defaults=[],
defaults=default_nodes,
kwonlyargs=[
nodes.AssignName(name=arg, parent=argsnode) for arg in kwonlyargs or ()
],
Expand All @@ -142,9 +151,6 @@ def build_function(
body=[],
doc_node=nodes.Const(value=doc) if doc else None,
)
for default in defaults or ():
argsnode.defaults.append(nodes.const_factory(default))
argsnode.defaults[-1].parent = argsnode
if args:
register_arguments(func)
return func
Expand Down Expand Up @@ -188,7 +194,7 @@ def object_build_class(

def _get_args_info_from_callable(
member: _FunctionTypes,
) -> tuple[list[str], list[Any], list[str], list[str]]:
) -> tuple[list[str], list[str], list[Any], list[str]]:
"""Returns args, posonlyargs, defaults, kwonlyargs.
:note: currently ignores the return annotation.
Expand Down
2 changes: 1 addition & 1 deletion tests/unittest_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def test_inspect_build0(self) -> None:
def test_inspect_build1(self) -> None:
time_ast = self.manager.ast_from_module_name("time")
self.assertTrue(time_ast)
self.assertEqual(time_ast["time"].args.defaults, [])
self.assertEqual(time_ast["time"].args.defaults, None)

def test_inspect_build3(self) -> None:
self.builder.inspect_build(unittest)
Expand Down

0 comments on commit 1054a35

Please sign in to comment.