From e6fa74f02068b2c112de41e9c785f31041668a64 Mon Sep 17 00:00:00 2001 From: Salvo Polizzi Date: Sat, 10 Feb 2024 17:50:55 +0100 Subject: [PATCH] Fixed #35179 -- Made admindocs detect positional/keyword-only arguments. --- django/utils/inspect.py | 4 +--- tests/admin_docs/models.py | 6 ++++++ tests/admin_docs/test_views.py | 4 ++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/django/utils/inspect.py b/django/utils/inspect.py index 81a15ed2db..4e065f0347 100644 --- a/django/utils/inspect.py +++ b/django/utils/inspect.py @@ -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 diff --git a/tests/admin_docs/models.py b/tests/admin_docs/models.py index a403259c6d..b4ef84caba 100644 --- a/tests/admin_docs/models.py +++ b/tests/admin_docs/models.py @@ -54,6 +54,12 @@ class Person(models.Model): 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" diff --git a/tests/admin_docs/test_views.py b/tests/admin_docs/test_views.py index ef7fde1bf9..064ce27fb0 100644 --- a/tests/admin_docs/test_views.py +++ b/tests/admin_docs/test_views.py @@ -280,6 +280,8 @@ class TestModelDetailView(TestDataMixin, AdminDocsTestCase): self.assertContains(self.response, "

Methods with arguments

") self.assertContains(self.response, "rename_company") self.assertContains(self.response, "dummy_function") + self.assertContains(self.response, "dummy_function_keyword_only_arg") + self.assertContains(self.response, "all_kinds_arg_function") self.assertContains(self.response, "suffix_company_name") def test_methods_with_arguments_display_arguments(self): @@ -287,6 +289,7 @@ class TestModelDetailView(TestDataMixin, AdminDocsTestCase): Methods with arguments should have their arguments displayed. """ self.assertContains(self.response, "new_name") + self.assertContains(self.response, "keyword_only_arg") def test_methods_with_arguments_display_arguments_default_value(self): """ @@ -302,6 +305,7 @@ class TestModelDetailView(TestDataMixin, AdminDocsTestCase): self.assertContains( self.response, "baz, rox, *some_args, **some_kwargs" ) + self.assertContains(self.response, "position_only_arg, arg, kwarg") def test_instance_of_property_methods_are_displayed(self): """Model properties are displayed as fields."""