Skip to content

Commit

Permalink
Use inspect.signature when available instead of signature.getargspec.
Browse files Browse the repository at this point in the history
signature.getargspec was deprecated in Python 3.0.

This commit also adds a test that ensures that the passing of arguments
to the super constrctor works as intended.

Fixed #15.
  • Loading branch information
pelme committed Jun 7, 2017
1 parent 1dd21ce commit 5eec212
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 13 additions & 1 deletion rest_framework_recursive/fields.py
Expand Up @@ -3,6 +3,18 @@
from rest_framework.fields import Field
from rest_framework.serializers import BaseSerializer


def _signature_parameters(func):
try:
inspect.signature
except AttributeError:
# Python 2.x
return inspect.getargspec(func).args
else:
# Python 3.x
return inspect.signature(func).parameters.keys()


class RecursiveField(Field):
"""
A field that gets its representation from its parent.
Expand Down Expand Up @@ -58,7 +70,7 @@ def __init__(self, to=None, **kwargs):
super_kwargs = dict(
(key, kwargs[key])
for key in kwargs
if key in inspect.getargspec(Field.__init__).args
if key in _signature_parameters(Field.__init__)
)
super(RecursiveField, self).__init__(**super_kwargs)

Expand Down
7 changes: 7 additions & 0 deletions tests/test_recursive.py
Expand Up @@ -214,3 +214,10 @@ def test_model_serializer(self):

# deserialization
self.deserialize(RecursiveModelSerializer, representation)

def test_super_kwargs(self):
"""RecursiveField.__init__ introspect the parent constructor to pass
kwargs properly. read_only is used used here to verify that the
argument is properly passed to the super Field."""
field = RecursiveField(default='a default value')
assert field.default == 'a default value'

0 comments on commit 5eec212

Please sign in to comment.