From 7cfec55410d71adc6790634532f5142f4bc1107f Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Mon, 7 Aug 2017 20:48:20 -0700 Subject: [PATCH] Added mypy static checking --- .gitignore | 1 + .travis.yml | 9 +++++++++ graphene/pyutils/init_subclass.py | 2 +- graphene/types/inputobjecttype.py | 8 +++++++- graphene/types/interface.py | 5 +++++ graphene/types/mutation.py | 9 ++++++++- graphene/types/objecttype.py | 7 ++++++- graphene/types/scalars.py | 1 + graphene/types/union.py | 9 ++++++++- mypy.ini | 17 +++++++++++++++++ 10 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 mypy.ini diff --git a/.gitignore b/.gitignore index 9f465556..d98ebfc3 100644 --- a/.gitignore +++ b/.gitignore @@ -80,3 +80,4 @@ target/ # Databases *.sqlite3 .vscode +.mypy_cache diff --git a/.travis.yml b/.travis.yml index 239df23d..e5d52a0a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,12 +27,19 @@ install: elif [ "$TEST_TYPE" = lint ]; then pip install flake8 fi + elif [ "$TEST_TYPE" = mypy ]; then + pip install mypy + fi script: - | if [ "$TEST_TYPE" = lint ]; then echo "Checking Python code lint." flake8 graphene exit + elif [ "$TEST_TYPE" = mypy ]; then + echo "Checking Python types." + mypy graphene + exit elif [ "$TEST_TYPE" = build ]; then py.test --cov=graphene graphene examples fi @@ -51,6 +58,8 @@ matrix: include: - python: '2.7' env: TEST_TYPE=lint + - python: '3.6' + env: TEST_TYPE=mypy deploy: provider: pypi user: syrusakbary diff --git a/graphene/pyutils/init_subclass.py b/graphene/pyutils/init_subclass.py index c3a6143c..78e4ff19 100644 --- a/graphene/pyutils/init_subclass.py +++ b/graphene/pyutils/init_subclass.py @@ -16,4 +16,4 @@ if not is_init_subclass_available: if hasattr(super_class, '__init_subclass__'): super_class.__init_subclass__.__func__(cls, **kwargs) else: - InitSubclassMeta = type + InitSubclassMeta = type # type: ignore diff --git a/graphene/types/inputobjecttype.py b/graphene/types/inputobjecttype.py index 3beb3ebf..28dc220b 100644 --- a/graphene/types/inputobjecttype.py +++ b/graphene/types/inputobjecttype.py @@ -6,8 +6,14 @@ from .unmountedtype import UnmountedType from .utils import yank_fields_from_attrs +# For static type checking with Mypy +MYPY = False +if MYPY: + from typing import Dict, Callable + + class InputObjectTypeOptions(BaseOptions): - fields = None # type: Dict[str, Field] + fields = None # type: Dict[str, InputField] create_container = None # type: Callable diff --git a/graphene/types/interface.py b/graphene/types/interface.py index c98f0f1f..b806de33 100644 --- a/graphene/types/interface.py +++ b/graphene/types/interface.py @@ -4,6 +4,11 @@ from .base import BaseOptions, BaseType from .field import Field from .utils import yank_fields_from_attrs +# For static type checking with Mypy +MYPY = False +if MYPY: + from typing import Dict + class InterfaceOptions(BaseOptions): fields = None # type: Dict[str, Field] diff --git a/graphene/types/mutation.py b/graphene/types/mutation.py index 213c0f48..6a7a7bbb 100644 --- a/graphene/types/mutation.py +++ b/graphene/types/mutation.py @@ -8,10 +8,17 @@ from .utils import yank_fields_from_attrs from ..utils.deprecated import warn_deprecation +# For static type checking with Mypy +MYPY = False +if MYPY: + from .argument import Argument + from typing import Dict, Type, Callable + + class MutationOptions(ObjectTypeOptions): arguments = None # type: Dict[str, Argument] output = None # type: Type[ObjectType] - resolver = None # type: Function + resolver = None # type: Callable class Mutation(ObjectType): diff --git a/graphene/types/objecttype.py b/graphene/types/objecttype.py index c579a88e..53e00902 100644 --- a/graphene/types/objecttype.py +++ b/graphene/types/objecttype.py @@ -5,10 +5,15 @@ from .field import Field from .interface import Interface from .utils import yank_fields_from_attrs +# For static type checking with Mypy +MYPY = False +if MYPY: + from typing import Dict, Iterable, Type + class ObjectTypeOptions(BaseOptions): fields = None # type: Dict[str, Field] - interfaces = () # type: List[Type[Interface]] + interfaces = () # type: Iterable[Type[Interface]] class ObjectType(BaseType): diff --git a/graphene/types/scalars.py b/graphene/types/scalars.py index ccfb089c..3b78185d 100644 --- a/graphene/types/scalars.py +++ b/graphene/types/scalars.py @@ -86,6 +86,7 @@ class Float(Scalar): @staticmethod def coerce_float(value): + # type: (Any) -> float try: return float(value) except ValueError: diff --git a/graphene/types/union.py b/graphene/types/union.py index f9797fc0..ae6af8b4 100644 --- a/graphene/types/union.py +++ b/graphene/types/union.py @@ -2,8 +2,15 @@ from .base import BaseOptions, BaseType from .unmountedtype import UnmountedType +# For static type checking with Mypy +MYPY = False +if MYPY: + from .objecttype import ObjectType + from typing import Iterable, Type + + class UnionOptions(BaseOptions): - types = () # type: List[Type[ObjectType]] + types = () # type: Iterable[Type[ObjectType]] class Union(UnmountedType, BaseType): diff --git a/mypy.ini b/mypy.ini new file mode 100644 index 00000000..bbb37b77 --- /dev/null +++ b/mypy.ini @@ -0,0 +1,17 @@ +[mypy] +ignore_missing_imports = True + +[mypy-graphene.pyutils.*] +ignore_errors = True + +[mypy-graphene.types.scalars] +ignore_errors = True + +[mypy-graphene.types.generic] +ignore_errors = True + +[mypy-graphene.types.tests.*] +ignore_errors = True + +[mypy-graphene.relay.tests.*] +ignore_errors = True