Skip to content

Commit

Permalink
Remove allow_inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
lafrech committed May 6, 2020
1 parent 8a038c1 commit ebb4962
Show file tree
Hide file tree
Showing 14 changed files with 13 additions and 167 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
58 changes: 6 additions & 52 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 @@ -453,9 +444,6 @@ class CMixin:
class Parent(PMixin, Document):
p = fields.StrField()

class Meta:
allow_inheritance = True

@self.instance.register
class Child(CMixin, Parent):
c = fields.StrField()
Expand All @@ -482,9 +470,6 @@ class Parent(PMixin, Document):
p = fields.StrField()
pm = fields.IntField(validate=ma.validate.Range(0, 5))

class Meta:
allow_inheritance = True

@self.instance.register
class Child(CMixin, Parent):
c = fields.StrField()
Expand Down Expand Up @@ -524,7 +509,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 @@ -542,7 +526,6 @@ class Meta:
class DocChild1(AbsDoc):

class Meta:
allow_inheritance = True
collection_name = 'col1'

@self.instance.register
Expand All @@ -557,7 +540,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 @@ -578,9 +560,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 @@ -615,32 +594,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 @@ -652,13 +608,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
36 changes: 1 addition & 35 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 Expand Up @@ -454,9 +426,6 @@ class CMixin:
class Parent(PMixin, EmbeddedDocument):
p = fields.StrField()

class Meta:
allow_inheritance = True

@self.instance.register
class Child(CMixin, Parent):
c = fields.StrField()
Expand All @@ -483,9 +452,6 @@ class Parent(PMixin, EmbeddedDocument):
p = fields.StrField()
pm = fields.IntField(validate=ma.validate.Range(0, 5))

class Meta:
allow_inheritance = True

@self.instance.register
class Child(CMixin, Parent):
c = fields.StrField()
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
Loading

0 comments on commit ebb4962

Please sign in to comment.