Skip to content

Commit 2e58f53

Browse files
committed
Improved List/NonNull of_type exceptions and tests. Fixed #337
1 parent 78a1b18 commit 2e58f53

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

graphene/types/structures.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ class Structure(UnmountedType):
99

1010
def __init__(self, of_type, *args, **kwargs):
1111
super(Structure, self).__init__(*args, **kwargs)
12+
if not isinstance(of_type, Structure) and isinstance(of_type, UnmountedType):
13+
cls_name = type(self).__name__
14+
of_type_name = type(of_type).__name__
15+
raise Exception("{} could not have a mounted {}() as inner type. Try with {}({}).".format(
16+
cls_name,
17+
of_type_name,
18+
cls_name,
19+
of_type_name,
20+
))
1221
self.of_type = of_type
1322

1423
def get_type(self):
@@ -56,7 +65,7 @@ def __init__(self, *args, **kwargs):
5665
super(NonNull, self).__init__(*args, **kwargs)
5766
assert not isinstance(self.of_type, NonNull), (
5867
'Can only create NonNull of a Nullable GraphQLType but got: {}.'
59-
).format(type)
68+
).format(self.of_type)
6069

6170
def __str__(self):
6271
return '{}!'.format(self.of_type)

graphene/types/tests/test_structures.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,51 @@ def test_list():
1010
assert str(_list) == '[String]'
1111

1212

13+
def test_list_with_unmounted_type():
14+
with pytest.raises(Exception) as exc_info:
15+
List(String())
16+
17+
assert str(exc_info.value) == 'List could not have a mounted String() as inner type. Try with List(String).'
18+
19+
20+
def test_list_inherited_works_list():
21+
_list = List(List(String))
22+
assert isinstance(_list.of_type, List)
23+
assert _list.of_type.of_type == String
24+
25+
26+
def test_list_inherited_works_nonnull():
27+
_list = List(NonNull(String))
28+
assert isinstance(_list.of_type, NonNull)
29+
assert _list.of_type.of_type == String
30+
31+
1332
def test_nonnull():
1433
nonnull = NonNull(String)
1534
assert nonnull.of_type == String
1635
assert str(nonnull) == 'String!'
1736

1837

38+
def test_nonnull_inherited_works_list():
39+
_list = NonNull(List(String))
40+
assert isinstance(_list.of_type, List)
41+
assert _list.of_type.of_type == String
42+
43+
44+
def test_nonnull_inherited_dont_work_nonnull():
45+
with pytest.raises(Exception) as exc_info:
46+
NonNull(NonNull(String))
47+
48+
assert str(exc_info.value) == 'Can only create NonNull of a Nullable GraphQLType but got: String!.'
49+
50+
51+
def test_nonnull_with_unmounted_type():
52+
with pytest.raises(Exception) as exc_info:
53+
NonNull(String())
54+
55+
assert str(exc_info.value) == 'NonNull could not have a mounted String() as inner type. Try with NonNull(String).'
56+
57+
1958
def test_list_comparasion():
2059
list1 = List(String)
2160
list2 = List(String)

0 commit comments

Comments
 (0)