Skip to content

Commit

Permalink
Fixed #35179 -- Made admindocs detect positional/keyword-only arguments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Salvo Polizzi authored and felixxm committed Feb 14, 2024
1 parent bf692b2 commit e6fa74f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 1 addition & 3 deletions django/utils/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ def func_accepts_var_args(func):

def method_has_no_args(meth):
"""Return True if a method only accepts 'self'."""
count = len(
[p for p in _get_callable_parameters(meth) if p.kind == p.POSITIONAL_OR_KEYWORD]
)
count = len([p for p in _get_callable_parameters(meth) if p.kind in ARG_KINDS])
return count == 0 if inspect.ismethod(meth) else count == 1


Expand Down
6 changes: 6 additions & 0 deletions tests/admin_docs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ def rename_company(self, new_name):
def dummy_function(self, baz, rox, *some_args, **some_kwargs):
return some_kwargs

def dummy_function_keyword_only_arg(self, *, keyword_only_arg):
return keyword_only_arg

def all_kinds_arg_function(self, position_only_arg, /, arg, *, kwarg):
return position_only_arg, arg, kwarg

@property
def a_property(self):
return "a_property"
Expand Down
4 changes: 4 additions & 0 deletions tests/admin_docs/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,13 +280,16 @@ def test_methods_with_arguments(self):
self.assertContains(self.response, "<h3>Methods with arguments</h3>")
self.assertContains(self.response, "<td>rename_company</td>")
self.assertContains(self.response, "<td>dummy_function</td>")
self.assertContains(self.response, "<td>dummy_function_keyword_only_arg</td>")
self.assertContains(self.response, "<td>all_kinds_arg_function</td>")
self.assertContains(self.response, "<td>suffix_company_name</td>")

def test_methods_with_arguments_display_arguments(self):
"""
Methods with arguments should have their arguments displayed.
"""
self.assertContains(self.response, "<td>new_name</td>")
self.assertContains(self.response, "<td>keyword_only_arg</td>")

def test_methods_with_arguments_display_arguments_default_value(self):
"""
Expand All @@ -302,6 +305,7 @@ def test_methods_with_multiple_arguments_display_arguments(self):
self.assertContains(
self.response, "<td>baz, rox, *some_args, **some_kwargs</td>"
)
self.assertContains(self.response, "<td>position_only_arg, arg, kwarg</td>")

def test_instance_of_property_methods_are_displayed(self):
"""Model properties are displayed as fields."""
Expand Down

0 comments on commit e6fa74f

Please sign in to comment.