Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
eafc9a23
Commit
eafc9a23
authored
Oct 25, 2016
by
Ryan P Kilby
Committed by
Tom Christie
Oct 25, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix is_simple_callable with variable args, kwargs (#4622)
parent
1bd35ad3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
2 deletions
+30
-2
rest_framework/fields.py
+7
-2
tests/test_fields.py
+23
-0
No files found.
rest_framework/fields.py
View file @
eafc9a23
...
...
@@ -54,12 +54,17 @@ if six.PY3:
"""
True if the object is a callable that takes no arguments.
"""
if
not
callable
(
obj
):
if
not
(
inspect
.
isfunction
(
obj
)
or
inspect
.
ismethod
(
obj
)
):
return
False
sig
=
inspect
.
signature
(
obj
)
params
=
sig
.
parameters
.
values
()
return
all
(
param
.
default
!=
param
.
empty
for
param
in
params
)
return
all
(
param
.
kind
==
param
.
VAR_POSITIONAL
or
param
.
kind
==
param
.
VAR_KEYWORD
or
param
.
default
!=
param
.
empty
for
param
in
params
)
else
:
def
is_simple_callable
(
obj
):
...
...
tests/test_fields.py
View file @
eafc9a23
...
...
@@ -37,6 +37,9 @@ class TestIsSimpleCallable:
def
valid_kwargs
(
self
,
param
=
'value'
):
pass
def
valid_vargs_kwargs
(
self
,
*
args
,
**
kwargs
):
pass
def
invalid
(
self
,
param
):
pass
...
...
@@ -45,11 +48,13 @@ class TestIsSimpleCallable:
# unbound methods
assert
not
is_simple_callable
(
Foo
.
valid
)
assert
not
is_simple_callable
(
Foo
.
valid_kwargs
)
assert
not
is_simple_callable
(
Foo
.
valid_vargs_kwargs
)
assert
not
is_simple_callable
(
Foo
.
invalid
)
# bound methods
assert
is_simple_callable
(
Foo
()
.
valid
)
assert
is_simple_callable
(
Foo
()
.
valid_kwargs
)
assert
is_simple_callable
(
Foo
()
.
valid_vargs_kwargs
)
assert
not
is_simple_callable
(
Foo
()
.
invalid
)
def
test_function
(
self
):
...
...
@@ -59,13 +64,31 @@ class TestIsSimpleCallable:
def
valid
(
param
=
'value'
,
param2
=
'value'
):
pass
def
valid_vargs_kwargs
(
*
args
,
**
kwargs
):
pass
def
invalid
(
param
,
param2
=
'value'
):
pass
assert
is_simple_callable
(
simple
)
assert
is_simple_callable
(
valid
)
assert
is_simple_callable
(
valid_vargs_kwargs
)
assert
not
is_simple_callable
(
invalid
)
def
test_4602_regression
(
self
):
from
django.db
import
models
class
ChoiceModel
(
models
.
Model
):
choice_field
=
models
.
CharField
(
max_length
=
1
,
default
=
'a'
,
choices
=
((
'a'
,
'A'
),
(
'b'
,
'B'
)),
)
class
Meta
:
app_label
=
'tests'
assert
is_simple_callable
(
ChoiceModel
()
.
get_choice_field_display
)
@unittest.skipUnless
(
typings
,
'requires python 3.5'
)
def
test_type_annotation
(
self
):
# The annotation will otherwise raise a syntax error in python < 3.5
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment