Skip to content

Commit

Permalink
Merge pull request flask-restful#417 from jyelloz/master
Browse files Browse the repository at this point in the history
Allow any callables to be used as lazy attributes.
  • Loading branch information
joshfriend committed Mar 15, 2015
2 parents bb5e730 + 41c74b9 commit 6255cad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 1 addition & 2 deletions flask_restful/fields.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from datetime import datetime
from calendar import timegm
import pytz
from inspect import isfunction
from decimal import Decimal as MyDecimal, ROUND_HALF_EVEN
from email.utils import formatdate
import six
Expand Down Expand Up @@ -38,7 +37,7 @@ def get_value(key, obj, default=None):
"""Helper for pulling a keyed value off various types of objects"""
if type(key) == int:
return _get_value_for_key(key, obj, default)
elif isfunction(key):
elif callable(key):
return key(obj)
else:
return _get_value_for_keys(key.split('.'), obj, default)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from decimal import Decimal
from functools import partial
import pytz
import unittest
from mock import Mock
Expand Down Expand Up @@ -165,6 +166,15 @@ def test_string_with_lambda(self):
field = fields.String(attribute=lambda x: x.hey)
self.assertEquals("3", field.output("foo", Foo()))

def test_string_with_partial(self):

def f(x, suffix):
return "%s-%s" % (x.hey, suffix)

p = partial(f, suffix="whatever")
field = fields.String(attribute=p)
self.assertEquals("3-whatever", field.output("foo", Foo()))

def test_url_invalid_object(self):
app = Flask(__name__)
app.add_url_rule("/<hey>", "foobar", view_func=lambda x: x)
Expand Down

0 comments on commit 6255cad

Please sign in to comment.