From 6013bcb42506b9822c43dfe8e3dff5d2762c5185 Mon Sep 17 00:00:00 2001 From: Alireza Savand Date: Thu, 22 Sep 2016 11:40:55 +0400 Subject: [PATCH 1/5] Fixes #4506: Support Python 3 annotations on fields callables --- rest_framework/fields.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 917a151e5..7bf9ef1e3 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -59,7 +59,11 @@ def is_simple_callable(obj): if not (function or method): return False - args, _, _, defaults = inspect.getargspec(obj) + if six.PY2: + args, _, _, defaults = inspect.getargspec(obj) + else: + args, _, _, defaults = inspect.getfullargspec(obj)[:4] + len_args = len(args) if function else len(args) - 1 len_defaults = len(defaults) if defaults else 0 return len_args <= len_defaults From fe417aad79abc71d817b885ec6fd4e806d7fc38c Mon Sep 17 00:00:00 2001 From: Alireza Savand Date: Thu, 22 Sep 2016 12:47:17 +0400 Subject: [PATCH 2/5] Add getargspec to compact --- rest_framework/compat.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index cee430a84..2f7961ca2 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -140,6 +140,16 @@ def value_from_object(field, obj): return field.value_from_object(obj) +def getargspec(obj): # type: tuple + if six.PY2: + parameters, _, _, defaults = inspect.getargspec(obj) + else: + signature = inspect.signature(obj) + parameters = signature.parameters + defaults = [i for i in parameters if i.default != inspect._empty] + + return parameters, defaults + # contrib.postgres only supported from 1.8 onwards. try: from django.contrib.postgres import fields as postgres_fields From 974b94dfd7a0e7db38967e8863c1a7b59b279591 Mon Sep 17 00:00:00 2001 From: Alireza Savand Date: Thu, 22 Sep 2016 12:47:31 +0400 Subject: [PATCH 3/5] Using getargspec from compat --- rest_framework/fields.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 7bf9ef1e3..835739612 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -32,7 +32,8 @@ from django.utils.translation import ugettext_lazy as _ from rest_framework import ISO_8601 from rest_framework.compat import ( - get_remote_field, unicode_repr, unicode_to_repr, value_from_object + get_remote_field, getargspec, unicode_repr, unicode_to_repr, + value_from_object ) from rest_framework.exceptions import ValidationError from rest_framework.settings import api_settings @@ -59,12 +60,9 @@ def is_simple_callable(obj): if not (function or method): return False - if six.PY2: - args, _, _, defaults = inspect.getargspec(obj) - else: - args, _, _, defaults = inspect.getfullargspec(obj)[:4] + parameters, defaults = getargspec(obj) - len_args = len(args) if function else len(args) - 1 + len_args = len(parameters) if function else len(parameters) - 1 len_defaults = len(defaults) if defaults else 0 return len_args <= len_defaults From 610e8ca75ca9ad8fd70c88a4785965cd5e0fd5dd Mon Sep 17 00:00:00 2001 From: Alireza Savand Date: Thu, 22 Sep 2016 13:10:02 +0400 Subject: [PATCH 4/5] Better to check of existence of `signature` --- rest_framework/compat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 2f7961ca2..c278448fd 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -141,7 +141,7 @@ def value_from_object(field, obj): def getargspec(obj): # type: tuple - if six.PY2: + if not hasattr(inspect, 'signature'): parameters, _, _, defaults = inspect.getargspec(obj) else: signature = inspect.signature(obj) From 14c22bcab383eec424e32c05ea6df37ba88993b9 Mon Sep 17 00:00:00 2001 From: Alireza Savand Date: Thu, 22 Sep 2016 14:29:36 +0400 Subject: [PATCH 5/5] Coment t o show whihc python version is statement for --- rest_framework/compat.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index c278448fd..8fc7f8a53 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -140,10 +140,10 @@ def value_from_object(field, obj): return field.value_from_object(obj) -def getargspec(obj): # type: tuple - if not hasattr(inspect, 'signature'): +def getargspec(obj): + if not hasattr(inspect, 'signature'): # Python 2.7 - 3.2 parameters, _, _, defaults = inspect.getargspec(obj) - else: + else: # Python +3.3 signature = inspect.signature(obj) parameters = signature.parameters defaults = [i for i in parameters if i.default != inspect._empty]