From f1bf069ec1a3d4761ab03ccae00961bc3f2aea8a Mon Sep 17 00:00:00 2001 From: Josh Schneier Date: Mon, 6 Aug 2018 23:12:51 -0400 Subject: [PATCH] Refs #29244 -- Fixed django.utils.inspect.method_has_no_args() for bound methods. --- django/utils/inspect.py | 6 +++--- tests/utils_tests/test_inspect.py | 2 ++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/django/utils/inspect.py b/django/utils/inspect.py index 15cd7fbc45..293cbdffa1 100644 --- a/django/utils/inspect.py +++ b/django/utils/inspect.py @@ -52,11 +52,11 @@ def func_accepts_var_args(func): def method_has_no_args(meth): """Return True if a method only accepts 'self'.""" - args = [ + count = len([ p for p in inspect.signature(meth).parameters.values() if p.kind == p.POSITIONAL_OR_KEYWORD - ] - return len(args) == 1 + ]) + return count == 0 if inspect.ismethod(meth) else count == 1 def func_supports_parameter(func, parameter): diff --git a/tests/utils_tests/test_inspect.py b/tests/utils_tests/test_inspect.py index dce8f95ecf..3967f2c886 100644 --- a/tests/utils_tests/test_inspect.py +++ b/tests/utils_tests/test_inspect.py @@ -37,3 +37,5 @@ class TestInspectMethods(unittest.TestCase): def test_method_has_no_args(self): self.assertIs(inspect.method_has_no_args(Person.no_arguments), True) self.assertIs(inspect.method_has_no_args(Person.one_argument), False) + self.assertIs(inspect.method_has_no_args(Person().no_arguments), True) + self.assertIs(inspect.method_has_no_args(Person().one_argument), False)