From 311e4793f9a30d0ca1c1b69b71d15f7f32ec3153 Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Tue, 26 Jul 2016 11:49:40 +0100 Subject: [PATCH 1/5] Allow setting of any attributes starting with _ This fixes #234 --- graphene/core/classtypes/objecttype.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/graphene/core/classtypes/objecttype.py b/graphene/core/classtypes/objecttype.py index 2bcb5e68..91b18815 100644 --- a/graphene/core/classtypes/objecttype.py +++ b/graphene/core/classtypes/objecttype.py @@ -54,7 +54,12 @@ class ObjectType(six.with_metaclass(ObjectTypeMeta, FieldsClassType)): def __init__(self, *args, **kwargs): signals.pre_init.send(self.__class__, args=args, kwargs=kwargs) + self._root = kwargs.pop('_root', None) + for key, value in kwargs.items(): + if key.startswith('_'): + setattr(self, key, kwargs.pop(key)) + args_len = len(args) fields = self._meta.fields if args_len > len(fields): From 651200262310ac983ca0951e1dc41bdabf15dc37 Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Tue, 26 Jul 2016 11:54:04 +0100 Subject: [PATCH 2/5] Added test for private attributes --- graphene/core/classtypes/tests/test_objecttype.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/graphene/core/classtypes/tests/test_objecttype.py b/graphene/core/classtypes/tests/test_objecttype.py index c554dbbf..b3435179 100644 --- a/graphene/core/classtypes/tests/test_objecttype.py +++ b/graphene/core/classtypes/tests/test_objecttype.py @@ -54,6 +54,13 @@ def test_object_type_set_properties(): assert h.readonly_prop == 'readonly' assert h.write_prop == 'custom' +def test_object_type_set_private_attributes(): + class Human(ObjectType): + _private_state = None + + h = Human(_private_state='custom', _other_private_state='custom') + assert h._private_state == 'custom' + assert h._other_private_state == 'custom' def test_object_type_container_invalid_kwarg(): class Human(ObjectType): From 8c8125702472da35c5ea42ad8c7b6c8b3e55406b Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Tue, 26 Jul 2016 12:06:27 +0100 Subject: [PATCH 3/5] Fix dict pop issue. --- graphene/core/classtypes/objecttype.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/graphene/core/classtypes/objecttype.py b/graphene/core/classtypes/objecttype.py index 91b18815..c5559fab 100644 --- a/graphene/core/classtypes/objecttype.py +++ b/graphene/core/classtypes/objecttype.py @@ -54,12 +54,7 @@ class ObjectType(six.with_metaclass(ObjectTypeMeta, FieldsClassType)): def __init__(self, *args, **kwargs): signals.pre_init.send(self.__class__, args=args, kwargs=kwargs) - self._root = kwargs.pop('_root', None) - for key, value in kwargs.items(): - if key.startswith('_'): - setattr(self, key, kwargs.pop(key)) - args_len = len(args) fields = self._meta.fields if args_len > len(fields): @@ -85,7 +80,7 @@ class ObjectType(six.with_metaclass(ObjectTypeMeta, FieldsClassType)): 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 From 7f85f65ba793d8c53dd0f6d37f679303f266014a Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Tue, 26 Jul 2016 12:12:40 +0100 Subject: [PATCH 4/5] Fix tests --- graphene/core/classtypes/tests/test_objecttype.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/graphene/core/classtypes/tests/test_objecttype.py b/graphene/core/classtypes/tests/test_objecttype.py index b3435179..ba20928d 100644 --- a/graphene/core/classtypes/tests/test_objecttype.py +++ b/graphene/core/classtypes/tests/test_objecttype.py @@ -58,9 +58,11 @@ def test_object_type_set_private_attributes(): class Human(ObjectType): _private_state = None - h = Human(_private_state='custom', _other_private_state='custom') + h = Human(_private_state='custom') assert h._private_state == 'custom' - assert h._other_private_state == 'custom' + + with raises(TypeError): + Human(_other_private_state='My name') def test_object_type_container_invalid_kwarg(): class Human(ObjectType): From a7d7f95b86f004f6d12b38f58e91b8885538ebb4 Mon Sep 17 00:00:00 2001 From: Markus Padourek Date: Tue, 26 Jul 2016 12:16:48 +0100 Subject: [PATCH 5/5] fix linting issues --- graphene/core/classtypes/tests/test_objecttype.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/graphene/core/classtypes/tests/test_objecttype.py b/graphene/core/classtypes/tests/test_objecttype.py index ba20928d..609975f2 100644 --- a/graphene/core/classtypes/tests/test_objecttype.py +++ b/graphene/core/classtypes/tests/test_objecttype.py @@ -54,16 +54,18 @@ def test_object_type_set_properties(): assert h.readonly_prop == 'readonly' assert h.write_prop == 'custom' + def test_object_type_set_private_attributes(): class Human(ObjectType): _private_state = None - + h = Human(_private_state='custom') assert h._private_state == 'custom' - + with raises(TypeError): Human(_other_private_state='My name') + def test_object_type_container_invalid_kwarg(): class Human(ObjectType): name = String()