Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the Required/NonNull behaviour for Pydantic fields with defaults #84

Merged
merged 1 commit into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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