Skip to content

Commit b4b42db

Browse files
committed
Updating imports to work when singledispatch is available natively
1 parent 166a143 commit b4b42db

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

graphene/contrib/django/converter.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from django.db import models
2-
from singledispatch import singledispatch
32

3+
from .utils import import_single_dispatch
44
from ...core.types.scalars import ID, Boolean, Float, Int, String
55

6+
singledispatch = import_single_dispatch()
7+
68
try:
79
UUIDField = models.UUIDField
810
except AttributeError:

graphene/contrib/django/filter/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
try:
2+
import django_filters
3+
except:
4+
raise Exception(
5+
"Use of django filtering requires the django-filter package "
6+
"be installed. You can do so using `pip install django-filter`"
7+
)
8+
19
from .fields import DjangoFilterConnectionField
210
from .filterset import GrapheneFilterSet, GlobalIDFilter, GlobalIDMultipleChoiceFilter
311
from .resolvers import FilterConnectionResolver

graphene/contrib/django/form_converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from django import forms
22
from django.forms.fields import BaseTemporalField
3-
from singledispatch import singledispatch
4-
53
from graphene import String, Int, Boolean, Float, ID
64
from graphene.contrib.django.forms import GlobalIDFormField, GlobalIDMultipleChoiceField
5+
from graphene.contrib.django.utils import import_single_dispatch
76
from graphene.core.types.definitions import List
7+
singledispatch = import_single_dispatch()
88

99
try:
1010
UUIDField = forms.UUIDField

graphene/contrib/django/utils.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from django.db.models.manager import Manager
44

55
from graphene import Argument, String
6-
from graphene.contrib.django.form_converter import convert_form_field
76

87

98
def get_type_for_model(schema, model):
@@ -36,6 +35,8 @@ def get_filtering_args_from_filterset(filterset_class, type):
3635
a Graphene Field. These arguments will be available to
3736
filter against in the GraphQL
3837
"""
38+
from graphene.contrib.django.form_converter import convert_form_field
39+
3940
args = {}
4041
for name, filter_field in six.iteritems(filterset_class.base_filters):
4142
field_type = Argument(convert_form_field(filter_field.field))
@@ -46,3 +47,26 @@ def get_filtering_args_from_filterset(filterset_class, type):
4647
# Also add the 'order_by' field
4748
args[filterset_class.order_by_field] = Argument(String)
4849
return args
50+
51+
52+
def import_single_dispatch():
53+
singledispatch = None
54+
try:
55+
from functools import singledispatch
56+
except ImportError:
57+
pass
58+
59+
try:
60+
from singledispatch import singledispatch
61+
except ImportError:
62+
pass
63+
64+
if not singledispatch:
65+
raise Exception(
66+
"It seems your python version does not include "
67+
"functools.singledispatch. Please install the 'singledispatch' "
68+
"package. More information here: "
69+
"https://pypi.python.org/pypi/singledispatch"
70+
)
71+
72+
return singledispatch

0 commit comments

Comments
 (0)