Skip to content

Commit

Permalink
Merge pull request #270 from Scille/remove_allow_inheritance
Browse files Browse the repository at this point in the history
Remove allow_inheritance
  • Loading branch information
lafrech committed May 8, 2020
2 parents 02ee1c8 + 28b9c54 commit faa5cf3
Show file tree
Hide file tree
Showing 14 changed files with 13 additions and 155 deletions.
9 changes: 2 additions & 7 deletions docs/userguide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ Object orientation means inheritance, of course you can do that
birthday = fields.DateTimeField()
class Meta:
allow_inheritance = True
abstract = True
@instance.register
Expand All @@ -125,9 +124,9 @@ Here we use this to allow ``Animal`` to be inheritable and to make it abstract.
.. code-block:: python
>>> Animal.opts
<DocumentOpts(instance=<umongo.frameworks.PyMongoInstance object at 0x7efe7daa9320>, template=<Document template class '__main__.Animal'>, abstract=True, allow_inheritance=True, collection_name=None, is_child=False, base_schema_cls=<class 'umongo.schema.Schema'>, indexes=[], offspring={<Implementation class '__main__.Duck'>, <Implementation class '__main__.Dog'>})>
<DocumentOpts(instance=<umongo.frameworks.PyMongoInstance object at 0x7efe7daa9320>, template=<Document template class '__main__.Animal'>, abstract=True, collection_name=None, is_child=False, base_schema_cls=<class 'umongo.schema.Schema'>, indexes=[], offspring={<Implementation class '__main__.Duck'>, <Implementation class '__main__.Dog'>})>
>>> Dog.opts
<DocumentOpts(instance=<umongo.frameworks.PyMongoInstance object at 0x7efe7daa9320>, template=<Document template class '__main__.Dog'>, abstract=False, allow_inheritance=False, collection_name=dog, is_child=False, base_schema_cls=<class 'umongo.schema.Schema'>, indexes=[], offspring=set())>
<DocumentOpts(instance=<umongo.frameworks.PyMongoInstance object at 0x7efe7daa9320>, template=<Document template class '__main__.Dog'>, abstract=False, collection_name=dog, is_child=False, base_schema_cls=<class 'umongo.schema.Schema'>, indexes=[], offspring=set())>
>>> class NotAllowedSubDog(Dog): pass
[...]
DocumentDefinitionError: Document <class '__main__.Dog'> doesn't allow inheritance
Expand Down Expand Up @@ -349,8 +348,6 @@ Inheritance inside the same collection is achieve by adding a ``_cls`` field
>>> @instance.register
... class Parent(Document):
... unique_in_parent = fields.IntField(unique=True)
... class Meta:
... allow_inheritance = True
>>> @instance.register
... class Child(Parent):
... unique_in_child = fields.StrField(unique=True)
Expand Down Expand Up @@ -435,8 +432,6 @@ compounded with the ``_cls``
>>> @instance.register
... class Parent(Document):
... unique_in_parent = fields.IntField(unique=True)
... class Meta:
... allow_inheritance = True
>>> @instance.register
... class Child(Parent):
... unique_in_child = fields.StrField(unique=True)
Expand Down
1 change: 0 additions & 1 deletion examples/inheritance/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Vehicle(Document):

class Meta:
collection_name = "vehicle"
allow_inheritance = True


@instance.register
Expand Down
3 changes: 0 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,4 @@ class Student(Document):
birthday = fields.DateTimeField()
courses = fields.ListField(fields.ReferenceField(Course))

class Meta:
allow_inheritance = True

return namedtuple('Mapping', ('Teacher', 'Course', 'Student'))(Teacher, Course, Student)
9 changes: 0 additions & 9 deletions tests/frameworks/test_motor_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,6 @@ class UniqueIndexParentDoc(Document):
not_unique = fields.StrField(unique=False)
unique = fields.IntField(unique=True)

class Meta:
allow_inheritance = True

@instance.register
class UniqueIndexChildDoc(UniqueIndexParentDoc):
child_not_unique = fields.StrField(unique=False)
Expand Down Expand Up @@ -718,16 +715,10 @@ async def do_test():
class InheritanceSearchParent(Document):
pf = fields.IntField()

class Meta:
allow_inheritance = True

@instance.register
class InheritanceSearchChild1(InheritanceSearchParent):
c1f = fields.IntField()

class Meta:
allow_inheritance = True

@instance.register
class InheritanceSearchChild1Child(InheritanceSearchChild1):
sc1f = fields.IntField()
Expand Down
9 changes: 0 additions & 9 deletions tests/frameworks/test_pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,6 @@ class UniqueIndexParentDoc(Document):
not_unique = fields.StrField(unique=False)
unique = fields.IntField(unique=True)

class Meta:
allow_inheritance = True

@instance.register
class UniqueIndexChildDoc(UniqueIndexParentDoc):
child_not_unique = fields.StrField(unique=False)
Expand Down Expand Up @@ -563,16 +560,10 @@ def test_inheritance_search(self, instance):
class InheritanceSearchParent(Document):
pf = fields.IntField()

class Meta:
allow_inheritance = True

@instance.register
class InheritanceSearchChild1(InheritanceSearchParent):
c1f = fields.IntField()

class Meta:
allow_inheritance = True

@instance.register
class InheritanceSearchChild1Child(InheritanceSearchChild1):
sc1f = fields.IntField()
Expand Down
9 changes: 0 additions & 9 deletions tests/frameworks/test_txmongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,6 @@ class UniqueIndexParentDoc(Document):
not_unique = fields.StrField(unique=False)
unique = fields.IntField(unique=True)

class Meta:
allow_inheritance = True

@instance.register
class UniqueIndexChildDoc(UniqueIndexParentDoc):
child_not_unique = fields.StrField(unique=False)
Expand Down Expand Up @@ -655,16 +652,10 @@ def test_inheritance_search(self, instance):
class InheritanceSearchParent(Document):
pf = fields.IntField()

class Meta:
allow_inheritance = True

@instance.register
class InheritanceSearchChild1(InheritanceSearchParent):
c1f = fields.IntField()

class Meta:
allow_inheritance = True

@instance.register
class InheritanceSearchChild1Child(InheritanceSearchChild1):
sc1f = fields.IntField()
Expand Down
52 changes: 6 additions & 46 deletions tests/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,7 @@ def test_auto_id_field(self):

@self.instance.register
class AutoId(Document):

class Meta:
allow_inheritance = True
pass

assert 'id' in AutoId.schema.fields

Expand All @@ -245,9 +243,6 @@ def test_custom_id_field(self):
class CustomId(Document):
int_id = fields.IntField(attribute='_id')

class Meta:
allow_inheritance = True

assert 'id' not in CustomId.schema.fields
with pytest.raises(ma.ValidationError):
CustomId(id=my_id)
Expand Down Expand Up @@ -310,8 +305,7 @@ def test_inheritance_from_template(self):
# a template instead of from an implementation

class ParentAsTemplate(Document):
class Meta:
allow_inheritance = True
pass

Parent = self.instance.register(ParentAsTemplate)

Expand All @@ -324,18 +318,15 @@ class Child(ParentAsTemplate):
def test_grand_child_inheritance(self):
@self.instance.register
class GrandParent(Document):
class Meta:
allow_inheritance = True
pass

@self.instance.register
class Parent(GrandParent):
class Meta:
allow_inheritance = True
pass

@self.instance.register
class Uncle(GrandParent):
class Meta:
allow_inheritance = True
pass

@self.instance.register
class Child(Parent):
Expand Down Expand Up @@ -462,7 +453,6 @@ class Doc(Document):

assert Doc.opts.collection_name == 'doc'
assert Doc.opts.abstract is False
assert Doc.opts.allow_inheritance is False
assert Doc.opts.instance is self.instance
assert Doc.opts.is_child is False
assert Doc.opts.indexes == []
Expand All @@ -480,7 +470,6 @@ class Meta:
class DocChild1(AbsDoc):

class Meta:
allow_inheritance = True
collection_name = 'col1'

@self.instance.register
Expand All @@ -495,7 +484,6 @@ class Meta:

assert DocChild1.opts.collection_name is 'col1'
assert DocChild1Child.opts.collection_name is 'col1'
assert DocChild1Child.opts.allow_inheritance is False
assert DocChild2.opts.collection_name == 'col2'

def test_marshmallow_tags_build(self):
Expand All @@ -516,9 +504,6 @@ def test_marshmallow_tags(self):
class Animal(Document):
name = fields.StrField(attribute='_id') # Overwrite automatic pk

class Meta:
allow_inheritance = True

@self.instance.register
class Dog(Animal):
pass
Expand Down Expand Up @@ -553,32 +538,9 @@ def custom_validate(self, data, original_data, **kwargs):
exc.value.args[0] == {'name': 'Not suitable name for duck !'}

def test_bad_inheritance(self):
with pytest.raises(exceptions.DocumentDefinitionError) as exc:
@self.instance.register
class BadAbstractDoc(Document):
class Meta:
allow_inheritance = False
abstract = True
assert exc.value.args[0] == "Abstract document cannot disable inheritance"

@self.instance.register
class NotParent(Document):
pass

assert not NotParent.opts.allow_inheritance

with pytest.raises(exceptions.DocumentDefinitionError) as exc:
@self.instance.register
class ImpossibleChildDoc1(NotParent):
pass
assert exc.value.args[0] == ("Document"
" <Implementation class 'tests.test_document.NotParent'>"
" doesn't allow inheritance")

@self.instance.register
class NotAbstractParent(Document):
class Meta:
allow_inheritance = True
pass

with pytest.raises(exceptions.DocumentDefinitionError) as exc:
@self.instance.register
Expand All @@ -590,13 +552,11 @@ class Meta:
@self.instance.register
class ParentWithCol1(Document):
class Meta:
allow_inheritance = True
collection_name = 'col1'

@self.instance.register
class ParentWithCol2(Document):
class Meta:
allow_inheritance = True
collection_name = 'col2'

with pytest.raises(exceptions.DocumentDefinitionError) as exc:
Expand Down
30 changes: 1 addition & 29 deletions tests/test_embedded_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,6 @@ class Meta:
class ConcreteChild(AbstractParent, AlienClass):
c = fields.IntField()

class Meta:
allow_inheritance = True

@self.instance.register
class ConcreteGrandChild(AbstractChild):
d = fields.IntField()
Expand Down Expand Up @@ -317,34 +314,9 @@ class MyDoc(Document):


def test_bad_inheritance(self):
with pytest.raises(exceptions.DocumentDefinitionError) as exc:
@self.instance.register
class BadAbstract(EmbeddedDocument):
class Meta:
allow_inheritance = False
abstract = True
assert exc.value.args[0] == "Abstract embedded document cannot disable inheritance"

@self.instance.register
class NotParent(EmbeddedDocument):
class Meta:
allow_inheritance = False

with pytest.raises(exceptions.DocumentDefinitionError) as exc:
@self.instance.register
class ImpossibleChild1(NotParent):
pass
assert exc.value.args[0] == ("EmbeddedDocument"
" <Implementation class 'tests.test_embedded_document.NotParent'>"
" doesn't allow inheritance")

@self.instance.register
class NotAbstractParent(EmbeddedDocument):
class Meta:
allow_inheritance = True

# Unlike Document, EmbeddedDocument should allow inheritance by default
assert NotAbstractParent.opts.allow_inheritance
pass

with pytest.raises(exceptions.DocumentDefinitionError) as exc:
@self.instance.register
Expand Down
1 change: 0 additions & 1 deletion tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ class Parent(Document):
last_name = fields.StrField()

class Meta:
allow_inheritance = True
indexes = ['last_name']

@self.instance.register
Expand Down
9 changes: 0 additions & 9 deletions tests/test_inheritance.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ def test_cls_field(self):
class Parent(Document):
last_name = fields.StrField()

class Meta:
allow_inheritance = True

@self.instance.register
class Child(Parent):
first_name = fields.StrField()
Expand All @@ -36,18 +33,15 @@ class Parent(Document):
last_name = fields.StrField()

class Meta:
allow_inheritance = True
collection_name = 'parent_col'

assert Parent.opts.abstract is False
assert Parent.opts.allow_inheritance is True

@self.instance.register
class Child(Parent):
first_name = fields.StrField()

assert Child.opts.abstract is False
assert Child.opts.allow_inheritance is False
assert Child.opts.collection_name == 'parent_col'
assert Child.collection.name == 'parent_col'
Child(first_name='John', last_name='Doe')
Expand All @@ -70,7 +64,6 @@ class Meta:
abstract = True

assert AbstractDoc.opts.abstract is True
assert AbstractDoc.opts.allow_inheritance is True
# Cannot instanciate also an abstract document
with pytest.raises(exceptions.AbstractDocumentError):
AbstractDoc()
Expand All @@ -81,14 +74,12 @@ class Meta:
abstract = True

assert StillAbstractDoc.opts.abstract is True
assert StillAbstractDoc.opts.allow_inheritance is True

@self.instance.register
class ConcreteDoc(AbstractDoc):
pass

assert ConcreteDoc.opts.abstract is False
assert ConcreteDoc.opts.allow_inheritance is False
assert ConcreteDoc().abs_field == 'from abstract'

def test_non_document_inheritance(self):
Expand Down
Loading

0 comments on commit faa5cf3

Please sign in to comment.