mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-23 01:56:54 +03:00
Merge pull request #286 from Globegitter/easier-private-state
Allow initialising ObjectType with private state.
This commit is contained in:
commit
2fbf17a109
|
@ -95,7 +95,7 @@ class ObjectType(six.with_metaclass(ObjectTypeMeta)):
|
||||||
if kwargs:
|
if kwargs:
|
||||||
for prop in list(kwargs):
|
for prop in list(kwargs):
|
||||||
try:
|
try:
|
||||||
if isinstance(getattr(self.__class__, prop), property):
|
if isinstance(getattr(self.__class__, prop), property) or prop.startswith('_'):
|
||||||
setattr(self, prop, kwargs.pop(prop))
|
setattr(self, prop, kwargs.pop(prop))
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -64,6 +64,20 @@ def test_generate_objecttype_with_fields():
|
||||||
assert 'field' in MyObjectType._meta.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():
|
def test_ordered_fields_in_objecttype():
|
||||||
class MyObjectType(ObjectType):
|
class MyObjectType(ObjectType):
|
||||||
b = Field(MyType)
|
b = Field(MyType)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user