From ed0cfa5e4a2157b05c070547646c6c38c12b441b Mon Sep 17 00:00:00 2001 From: Valentin Cocaud Date: Mon, 22 Feb 2021 12:06:07 +0100 Subject: [PATCH] Raise error when arguments conains only type annotations --- graphene/types/argument.py | 4 ++++ graphene/types/tests/test_argument.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/graphene/types/argument.py b/graphene/types/argument.py index 71026d45..1eaee76c 100644 --- a/graphene/types/argument.py +++ b/graphene/types/argument.py @@ -75,6 +75,10 @@ def to_arguments(args, extra_args=None): from .field import Field from .inputfield import InputField + if type(args) == dict and len(args) == 1 and '__annotations__' in args: + raise ValueError(f"Arguments class doesn't have any field but has type annotations. " + f"You probably used ':' instead of '=' in class definition") + if extra_args: extra_args = sorted(extra_args.items(), key=lambda f: f[1]) else: diff --git a/graphene/types/tests/test_argument.py b/graphene/types/tests/test_argument.py index db4d6c24..04da9243 100644 --- a/graphene/types/tests/test_argument.py +++ b/graphene/types/tests/test_argument.py @@ -7,6 +7,8 @@ from ..field import Field from ..inputfield import InputField from ..scalars import String from ..structures import NonNull +from ..mutation import Mutation +from graphene.utils.props import props def test_argument(): @@ -74,3 +76,16 @@ def test_argument_with_lazy_partial_type(): MyType = object() arg = Argument(partial(lambda: MyType)) assert arg.type == MyType + + +def test_arguments_raise_if_type_annotations(): + class Arguments: + id: String() + + with raises(ValueError) as exec_info: + to_arguments(props(Arguments)) + + assert str(exec_info.value) == ( + f"Arguments class doesn't have any field but has type annotations. " + f"You probably used ':' instead of '=' in class definition" + )