Skip to content

Commit

Permalink
Merge pull request #84 from davidkell/fix/field-required
Browse files Browse the repository at this point in the history
Fix the Required/NonNull behaviour for Pydantic fields with defaults
  • Loading branch information
necaris authored Jul 24, 2023
2 parents 569aeaf + 330da61 commit 4e219fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion graphene_pydantic/converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def convert_pydantic_field(
declared_type, field, registry, parent_type=parent_type, model=model
),
)
field_kwargs.setdefault("required", field.required)
field_kwargs.setdefault("required", not field.allow_none)
field_kwargs.setdefault("default_value", field.default)
# TODO: find a better way to get a field's description. Some ideas include:
# - hunt down the description from the field's schema, or the schema
Expand Down
44 changes: 22 additions & 22 deletions tests/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ def test_default_values():
field = _convert_field_from_spec("s", "hi")
assert field is not None
assert isinstance(field, graphene.Field)
# there's a default value, so it's not required
assert not isinstance(field.type, graphene.NonNull)
assert field.type == graphene.String
# there's a default value, so it never null
assert isinstance(field.type, graphene.NonNull)
assert field.type.of_type == graphene.String
assert field.default_value == "hi"


Expand All @@ -65,15 +65,15 @@ def test_default_values():
def test_builtin_scalars(input, expected):
field = _convert_field_from_spec("attr", input)
assert isinstance(field, graphene.Field)
assert field.type == expected
assert field.type.of_type == expected
assert field.default_value == input[1]


def test_union():
field = _convert_field_from_spec("attr", (T.Union[int, float, str], 5.0))
assert issubclass(field.type, graphene.Union)
assert issubclass(field.type.of_type, graphene.Union)
assert field.default_value == 5.0
assert field.type.__name__.startswith("UnionOf")
assert field.type.of_type.__name__.startswith("UnionOf")


if sys.version_info >= (3, 8):
Expand All @@ -83,15 +83,15 @@ def test_literal():
field = _convert_field_from_spec(
"attr", (T.Literal["literal1", "literal2", 3], 3)
)
assert issubclass(field.type, graphene.Union)
assert issubclass(field.type.of_type, graphene.Union)
assert field.default_value == 3
assert field.type.__name__.startswith("UnionOf")
assert field.type.of_type.__name__.startswith("UnionOf")

def test_literal_singleton():
field = _convert_field_from_spec("attr", (T.Literal["literal1"], "literal1"))
assert issubclass(field.type, graphene.String)
assert issubclass(field.type.of_type, graphene.String)
assert field.default_value == "literal1"
assert field.type == graphene.String
assert field.type.of_type == graphene.String


def test_mapping():
Expand All @@ -103,34 +103,34 @@ def test_mapping():
def test_decimal(monkeypatch):
monkeypatch.setattr(converters, "DECIMAL_SUPPORTED", True)
field = _convert_field_from_spec("attr", (decimal.Decimal, decimal.Decimal(1.25)))
assert field.type.__name__ == "Decimal"
assert field.type.of_type.__name__ == "Decimal"

monkeypatch.setattr(converters, "DECIMAL_SUPPORTED", False)
field = _convert_field_from_spec("attr", (decimal.Decimal, decimal.Decimal(1.25)))
assert field.type.__name__ == "Float"
assert field.type.of_type.__name__ == "Float"


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

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

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

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

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

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

field = _convert_field_from_spec("attr", (tuple, (1, 2)))
assert field.type == graphene.types.List
assert field.type.of_type == graphene.types.List

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

field = _convert_field_from_spec("attr", (Color, Color.RED))
assert field.type.__name__ == "Color"
assert field.type._meta.enum == Color
assert field.type.of_type.__name__ == "Color"
assert field.type.of_type._meta.enum == Color


def test_existing_model():
Expand All @@ -157,7 +157,7 @@ class Meta:
model = Foo

field = _convert_field_from_spec("attr", (Foo, Foo(name="bar")))
assert field.type == GraphFoo
assert field.type.of_type == GraphFoo


def test_unresolved_placeholders():
Expand Down

0 comments on commit 4e219fa

Please sign in to comment.