small refactoring

This commit is contained in:
p.kamayev 2016-04-28 10:45:38 +03:00
parent aadbc0c83c
commit edc7680b76
3 changed files with 18 additions and 26 deletions

View File

@ -7,6 +7,7 @@ versions of Django/Python, and compatibility wrappers around optional packages.
from __future__ import unicode_literals from __future__ import unicode_literals
import inspect import inspect
import sys
import django import django
from django.conf import settings from django.conf import settings
@ -139,37 +140,28 @@ if six.PY3:
LONG_SEPARATORS = (', ', ': ') LONG_SEPARATORS = (', ', ': ')
INDENT_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: else:
SHORT_SEPARATORS = (b',', b':') SHORT_SEPARATORS = (b',', b':')
LONG_SEPARATORS = (b', ', b': ') LONG_SEPARATORS = (b', ', b': ')
INDENT_SEPARATORS = (b',', b': ') INDENT_SEPARATORS = (b',', b': ')
def is_simple_callable(obj):
""" def is_simple_callable(obj):
True if the object is a callable that takes no arguments. """
""" 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) function = inspect.isfunction(obj)
method = inspect.ismethod(obj)
if not (function or method):
return False
args, _, _, defaults = inspect.getargspec(obj) args, _, _, defaults = inspect.getargspec(obj)
len_args = len(args) if function else len(args) - 1 len_args = len(args) if function else len(args) - 1
len_defaults = len(defaults) if defaults else 0 len_defaults = len(defaults) if defaults else 0

View File

@ -47,7 +47,7 @@ class TestFunctionSimplicityCheck:
if sys.version_info >= (3, 5): 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): class TestFunctionSimplicityCheckPy35(FunctionSimplicityCheckPy35Mixin, TestFunctionSimplicityCheck):
pass pass