Skip to content

Commit 4e219fa

Browse files
authored
Merge pull request #84 from davidkell/fix/field-required
Fix the Required/NonNull behaviour for Pydantic fields with defaults
2 parents 569aeaf + 330da61 commit 4e219fa

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

graphene_pydantic/converters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def convert_pydantic_field(
123123
declared_type, field, registry, parent_type=parent_type, model=model
124124
),
125125
)
126-
field_kwargs.setdefault("required", field.required)
126+
field_kwargs.setdefault("required", not field.allow_none)
127127
field_kwargs.setdefault("default_value", field.default)
128128
# TODO: find a better way to get a field's description. Some ideas include:
129129
# - hunt down the description from the field's schema, or the schema

tests/test_converters.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def test_default_values():
4343
field = _convert_field_from_spec("s", "hi")
4444
assert field is not None
4545
assert isinstance(field, graphene.Field)
46-
# there's a default value, so it's not required
47-
assert not isinstance(field.type, graphene.NonNull)
48-
assert field.type == graphene.String
46+
# there's a default value, so it never null
47+
assert isinstance(field.type, graphene.NonNull)
48+
assert field.type.of_type == graphene.String
4949
assert field.default_value == "hi"
5050

5151

@@ -65,15 +65,15 @@ def test_default_values():
6565
def test_builtin_scalars(input, expected):
6666
field = _convert_field_from_spec("attr", input)
6767
assert isinstance(field, graphene.Field)
68-
assert field.type == expected
68+
assert field.type.of_type == expected
6969
assert field.default_value == input[1]
7070

7171

7272
def test_union():
7373
field = _convert_field_from_spec("attr", (T.Union[int, float, str], 5.0))
74-
assert issubclass(field.type, graphene.Union)
74+
assert issubclass(field.type.of_type, graphene.Union)
7575
assert field.default_value == 5.0
76-
assert field.type.__name__.startswith("UnionOf")
76+
assert field.type.of_type.__name__.startswith("UnionOf")
7777

7878

7979
if sys.version_info >= (3, 8):
@@ -83,15 +83,15 @@ def test_literal():
8383
field = _convert_field_from_spec(
8484
"attr", (T.Literal["literal1", "literal2", 3], 3)
8585
)
86-
assert issubclass(field.type, graphene.Union)
86+
assert issubclass(field.type.of_type, graphene.Union)
8787
assert field.default_value == 3
88-
assert field.type.__name__.startswith("UnionOf")
88+
assert field.type.of_type.__name__.startswith("UnionOf")
8989

9090
def test_literal_singleton():
9191
field = _convert_field_from_spec("attr", (T.Literal["literal1"], "literal1"))
92-
assert issubclass(field.type, graphene.String)
92+
assert issubclass(field.type.of_type, graphene.String)
9393
assert field.default_value == "literal1"
94-
assert field.type == graphene.String
94+
assert field.type.of_type == graphene.String
9595

9696

9797
def test_mapping():
@@ -103,34 +103,34 @@ def test_mapping():
103103
def test_decimal(monkeypatch):
104104
monkeypatch.setattr(converters, "DECIMAL_SUPPORTED", True)
105105
field = _convert_field_from_spec("attr", (decimal.Decimal, decimal.Decimal(1.25)))
106-
assert field.type.__name__ == "Decimal"
106+
assert field.type.of_type.__name__ == "Decimal"
107107

108108
monkeypatch.setattr(converters, "DECIMAL_SUPPORTED", False)
109109
field = _convert_field_from_spec("attr", (decimal.Decimal, decimal.Decimal(1.25)))
110-
assert field.type.__name__ == "Float"
110+
assert field.type.of_type.__name__ == "Float"
111111

112112

113113
def test_iterables():
114114
field = _convert_field_from_spec("attr", (T.List[int], [1, 2]))
115-
assert isinstance(field.type, graphene.types.List)
115+
assert isinstance(field.type.of_type, graphene.types.List)
116116

117117
field = _convert_field_from_spec("attr", (list, [1, 2]))
118-
assert field.type == graphene.types.List
118+
assert field.type.of_type == graphene.types.List
119119

120120
field = _convert_field_from_spec("attr", (T.Set[int], {1, 2}))
121-
assert isinstance(field.type, graphene.types.List)
121+
assert isinstance(field.type.of_type, graphene.types.List)
122122

123123
field = _convert_field_from_spec("attr", (set, {1, 2}))
124-
assert field.type == graphene.types.List
124+
assert field.type.of_type == graphene.types.List
125125

126126
field = _convert_field_from_spec("attr", (T.Tuple[int, float], (1, 2.2)))
127-
assert isinstance(field.type, graphene.types.List)
127+
assert isinstance(field.type.of_type, graphene.types.List)
128128

129129
field = _convert_field_from_spec("attr", (T.Tuple[int, ...], (1, 2.2)))
130-
assert isinstance(field.type, graphene.types.List)
130+
assert isinstance(field.type.of_type, graphene.types.List)
131131

132132
field = _convert_field_from_spec("attr", (tuple, (1, 2)))
133-
assert field.type == graphene.types.List
133+
assert field.type.of_type == graphene.types.List
134134

135135
field = _convert_field_from_spec("attr", (T.Union[None, int], 1))
136136
assert field.type == graphene.types.Int
@@ -142,8 +142,8 @@ class Color(enum.Enum):
142142
GREEN = 2
143143

144144
field = _convert_field_from_spec("attr", (Color, Color.RED))
145-
assert field.type.__name__ == "Color"
146-
assert field.type._meta.enum == Color
145+
assert field.type.of_type.__name__ == "Color"
146+
assert field.type.of_type._meta.enum == Color
147147

148148

149149
def test_existing_model():
@@ -157,7 +157,7 @@ class Meta:
157157
model = Foo
158158

159159
field = _convert_field_from_spec("attr", (Foo, Foo(name="bar")))
160-
assert field.type == GraphFoo
160+
assert field.type.of_type == GraphFoo
161161

162162

163163
def test_unresolved_placeholders():

0 commit comments

Comments
 (0)