Skip to content

Commit 7fd6125

Browse files
committed
Fix compatibility with pypy
1 parent 245ebe3 commit 7fd6125

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

graphene_django/compat.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ class MissingType(object):
33

44

55
try:
6-
from django.db.models.related import RelatedObject
7-
except:
8-
# Improved compatibility for Django 1.6
9-
RelatedObject = MissingType
6+
# Postgres fields are only available in Django with psycopg2 installed
7+
# and we cannot have psycopg2 on PyPy
8+
from django.contrib.postgres.fields import ArrayField, HStoreField, RangeField
9+
except ImportError:
10+
ArrayField, HStoreField, JSONField, RangeField = (MissingType, ) * 4
1011

1112

1213
try:

graphene_django/converter.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from django.db import models
22
from django.utils.encoding import force_text
3-
from django.contrib.postgres.fields import ArrayField, HStoreField, RangeField
43

54
from graphene import (ID, Boolean, Dynamic, Enum, Field, Float, Int, List,
65
NonNull, String)
@@ -10,8 +9,8 @@
109
from graphene.utils.str_converters import to_camel_case, to_const
1110
from graphql import assert_valid_name
1211

13-
14-
from .compat import JSONField, RelatedObject
12+
from .compat import (ArrayField, HStoreField, JSONField, RangeField,
13+
RelatedObject)
1514
from .fields import get_connection_field, DjangoListField
1615
from .utils import get_related_model, import_single_dispatch
1716

graphene_django/tests/test_converter.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import pytest
22
from django.db import models
33
from django.utils.translation import ugettext_lazy as _
4-
from django.contrib.postgres.fields import ArrayField, HStoreField
5-
64
from py.test import raises
75

86
import graphene
97
from graphene.relay import ConnectionField, Node
108
from graphene.types.datetime import DateTime, Time
119
from graphene.types.json import JSONString
1210

13-
from ..compat import JSONField, MissingType
11+
from ..compat import JSONField, RelatedObject
1412
from ..converter import convert_django_field, convert_django_field_with_choices
1513
from ..registry import Registry
1614
from ..types import DjangoObjectType
@@ -264,13 +262,17 @@ class Meta:
264262
assert dynamic_field.type == A
265263

266264

265+
@pytest.mark.skipif(ArrayField is MissingType,
266+
reason="ArrayField should exist")
267267
def test_should_postgres_array_convert_list():
268268
field = assert_conversion(ArrayField, graphene.List, models.CharField(max_length=100))
269269
assert isinstance(field.type, graphene.NonNull)
270270
assert isinstance(field.type.of_type, graphene.List)
271271
assert field.type.of_type.of_type == graphene.String
272272

273273

274+
@pytest.mark.skipif(ArrayField is MissingType,
275+
reason="ArrayField should exist")
274276
def test_should_postgres_array_multiple_convert_list():
275277
field = assert_conversion(ArrayField, graphene.List, ArrayField(models.CharField(max_length=100)))
276278
assert isinstance(field.type, graphene.NonNull)
@@ -279,6 +281,8 @@ def test_should_postgres_array_multiple_convert_list():
279281
assert field.type.of_type.of_type.of_type == graphene.String
280282

281283

284+
@pytest.mark.skipif(HStoreField is MissingType,
285+
reason="HStoreField should exist")
282286
def test_should_postgres_hstore_convert_string():
283287
assert_conversion(HStoreField, JSONString)
284288

@@ -289,6 +293,8 @@ def test_should_postgres_json_convert_string():
289293
assert_conversion(JSONField, JSONString)
290294

291295

296+
@pytest.mark.skipif(RangeField is MissingType,
297+
reason="RangeField should exist")
292298
def test_should_postgres_range_convert_list():
293299
from django.contrib.postgres.fields import IntegerRangeField
294300
field = assert_conversion(IntegerRangeField, graphene.List)

0 commit comments

Comments
 (0)