mirror of
				https://github.com/graphql-python/graphene.git
				synced 2025-11-04 09:57:41 +03:00 
			
		
		
		
	Allow initialising ObjectType with private state.
This commit is contained in:
		
							parent
							
								
									94d46f7960
								
							
						
					
					
						commit
						2975e1fc42
					
				| 
						 | 
					@ -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