From 6e26e4ba1570599c3955c4b1afe5371b529e054d Mon Sep 17 00:00:00 2001 From: Dima Kryukov Date: Sat, 28 May 2022 02:18:04 +0300 Subject: [PATCH] improve performance for noncallble attributes --- rest_framework/fields.py | 3 +++ tests/test_fields.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index d7e7816ce..37c5dd33b 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -63,6 +63,9 @@ def is_simple_callable(obj): """ True if the object is a callable that takes no arguments. """ + if not callable(obj): + return False + # Bail early since we cannot inspect built-in function signatures. if inspect.isbuiltin(obj): raise BuiltinSignatureError( diff --git a/tests/test_fields.py b/tests/test_fields.py index 7a5304a82..8746b8d3e 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -73,6 +73,10 @@ class TestIsSimpleCallable: assert is_simple_callable(valid_vargs_kwargs) assert not is_simple_callable(invalid) + @pytest.mark.parametrize('obj', (True, None, "str", b'bytes', 123, 1.23)) + def test_not_callable(self, obj): + assert not is_simple_callable(obj) + def test_4602_regression(self): from django.db import models