From 2975e1fc42709cfd8c2331a57dd91091f6ad2ef4 Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Mon, 12 Sep 2016 15:51:51 +0100 Subject: [PATCH] Allow initialising ObjectType with private state. --- graphene/types/objecttype.py | 2 +- graphene/types/tests/test_objecttype.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/graphene/types/objecttype.py b/graphene/types/objecttype.py index 273049ba..91b74c0f 100644 --- a/graphene/types/objecttype.py +++ b/graphene/types/objecttype.py @@ -95,7 +95,7 @@ class ObjectType(six.with_metaclass(ObjectTypeMeta)): if kwargs: for prop in list(kwargs): try: - if isinstance(getattr(self.__class__, prop), property): + if isinstance(getattr(self.__class__, prop), property) or prop.startswith('_'): setattr(self, prop, kwargs.pop(prop)) except AttributeError: pass diff --git a/graphene/types/tests/test_objecttype.py b/graphene/types/tests/test_objecttype.py index a6196b1f..eb297917 100644 --- a/graphene/types/tests/test_objecttype.py +++ b/graphene/types/tests/test_objecttype.py @@ -64,6 +64,20 @@ def test_generate_objecttype_with_fields(): assert 'field' in MyObjectType._meta.fields +def test_generate_objecttype_with_private_attributes(): + class MyObjectType(ObjectType): + _private_state = None + + assert '_private_state' not in MyObjectType._meta.fields + assert hasattr(MyObjectType, '_private_state') + + m = MyObjectType(_private_state='custom') + assert m._private_state == 'custom' + + with pytest.raises(TypeError): + MyObjectType(_other_private_state='Wrong') + + def test_ordered_fields_in_objecttype(): class MyObjectType(ObjectType): b = Field(MyType)