'How to assert that a class has a class exclusive property in Django/Python?

I have two models in Django, one that is the base and the other that is inherited. The base model has a database field (which in Python is an attribute of the class) and the inherited model has a property that is exclusive to the Class (not of every instance created). Both can yield different things.

from django.db import models
from django.utils.decorators import classproperty


class Parent(models.Model):

    somefield = models.TextField()


class Child(Parent):

    @classproperty
    def somefield(cls):
        return 'something'

How can I create a test to ensure that all the child models created from the parent model have that class exclusive property? Because if I use hasattr() it will consider the field and the property. Something like this

assertTrue(hasattr(Child, 'somefield'))

assertFalse(hasattr(Parent, 'somefield'))


Solution 1:[1]

You can make type assertions on your class' attribute like this:

type(getattr(Child, "somefield"))
type(getattr(Parent, "somefield"))

The outputs should be different.

I don't know though if this a useful testcase. Your code is the source of truth, you shouldn't need to test builtins. You would rather want to instantiate specific classes and see if the output is the expected one.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ViggieSmalls