From edc7680b76103818874e580c8c7a342d7530905a Mon Sep 17 00:00:00 2001
From: "p.kamayev"
Date: Thu, 28 Apr 2016 10:45:38 +0300
Subject: [PATCH] small refactoring
---
rest_framework/compat.py | 42 ++++++++-----------
.../{test_compat_py35.py => compat_py35.py} | 0
tests/test_compat.py | 2 +-
3 files changed, 18 insertions(+), 26 deletions(-)
rename tests/compat/{test_compat_py35.py => compat_py35.py} (100%)
diff --git a/rest_framework/compat.py b/rest_framework/compat.py
index 759cd1fbc..2a6af4c5c 100644
--- a/rest_framework/compat.py
+++ b/rest_framework/compat.py
@@ -7,6 +7,7 @@ versions of Django/Python, and compatibility wrappers around optional packages.
from __future__ import unicode_literals
import inspect
+import sys
import django
from django.conf import settings
@@ -139,37 +140,28 @@ if six.PY3:
LONG_SEPARATORS = (', ', ': ')
INDENT_SEPARATORS = (',', ': ')
- def is_simple_callable(obj):
- function = inspect.isfunction(obj)
- method = inspect.ismethod(obj)
-
- if not (function or method):
- return False
- # when we drop support of python3.2, we should replace getfullargspec with singnature
- # signature = inspect.signature(obj)
- # defaults = [p for p in signature.parameters.values() if p.default is not inspect.Parameter.empty]
- # return len(signature.parameters) <= len(defaults)
- function = inspect.isfunction(obj)
- args, _, _, defaults, _, kwonly, kwdefaults = inspect.getfullargspec(obj)
- len_args = (len(args) if function else len(args) - 1) + len(kwonly or ()) + len(kwdefaults or ())
- len_defaults = (len(defaults) if defaults else 0) + len(kwdefaults or ())
- return len_args <= len_defaults
-
else:
SHORT_SEPARATORS = (b',', b':')
LONG_SEPARATORS = (b', ', b': ')
INDENT_SEPARATORS = (b',', b': ')
- def is_simple_callable(obj):
- """
- True if the object is a callable that takes no arguments.
- """
+
+def is_simple_callable(obj):
+ """
+ True if the object is a callable that takes no arguments.
+ """
+
+ function = inspect.isfunction(obj)
+ method = inspect.ismethod(obj)
+
+ if not (function or method):
+ return False
+ if sys.version_info >= (3, 3):
+ signature = inspect.signature(obj)
+ defaults = [p for p in signature.parameters.values() if p.default is not inspect.Parameter.empty]
+ return len(signature.parameters) <= len(defaults)
+ else:
function = inspect.isfunction(obj)
- method = inspect.ismethod(obj)
-
- if not (function or method):
- return False
-
args, _, _, defaults = inspect.getargspec(obj)
len_args = len(args) if function else len(args) - 1
len_defaults = len(defaults) if defaults else 0
diff --git a/tests/compat/test_compat_py35.py b/tests/compat/compat_py35.py
similarity index 100%
rename from tests/compat/test_compat_py35.py
rename to tests/compat/compat_py35.py
diff --git a/tests/test_compat.py b/tests/test_compat.py
index b0c40ce34..a93ccd398 100644
--- a/tests/test_compat.py
+++ b/tests/test_compat.py
@@ -47,7 +47,7 @@ class TestFunctionSimplicityCheck:
if sys.version_info >= (3, 5):
- from tests.compat.test_compat_py35 import FunctionSimplicityCheckPy35Mixin
+ from tests.compat.compat_py35 import FunctionSimplicityCheckPy35Mixin
class TestFunctionSimplicityCheckPy35(FunctionSimplicityCheckPy35Mixin, TestFunctionSimplicityCheck):
pass