Raise error when arguments conains only type annotations

This commit is contained in:
Valentin Cocaud 2021-02-22 12:06:07 +01:00
parent 2e87ebe5fc
commit ed0cfa5e4a
2 changed files with 19 additions and 0 deletions

View File

@ -75,6 +75,10 @@ def to_arguments(args, extra_args=None):
from .field import Field from .field import Field
from .inputfield import InputField 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: if extra_args:
extra_args = sorted(extra_args.items(), key=lambda f: f[1]) extra_args = sorted(extra_args.items(), key=lambda f: f[1])
else: else:

View File

@ -7,6 +7,8 @@ from ..field import Field
from ..inputfield import InputField from ..inputfield import InputField
from ..scalars import String from ..scalars import String
from ..structures import NonNull from ..structures import NonNull
from ..mutation import Mutation
from graphene.utils.props import props
def test_argument(): def test_argument():
@ -74,3 +76,16 @@ def test_argument_with_lazy_partial_type():
MyType = object() MyType = object()
arg = Argument(partial(lambda: MyType)) arg = Argument(partial(lambda: MyType))
assert arg.type == 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"
)