Raising NotImplementedError makes it impossible to use super() properly

Bug #1278825 reported by Radomir Dopieralski
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
OpenStack Dashboard (Horizon)
Fix Released
Medium
Radomir Dopieralski

Bug Description

There is an antipattern in Python, where to define a "virtual" method people would raise a NotImplementedError exception in the body of the method. This makes it impossible to use super() properly with that method and, because of that, to use multiple inheritance, such as mixins.

Horizon has several places where this is used, including some methods, such as get_context_data, that are very likely to be touched by various mixins. Those methods should return some default, empty value, instead of raising an exception.

A typical implementation of get_context_data() in Django should look something like this:

    def get_context_data(self, **kwargs):
        context = super(OverviewTab, self).get_context_data(**kwargs)
        context.update({
            'overcloud': 1,
            'roles': [1, 2, 3],
        })
        return context

This will break horribly if the superclass' get_context_data raises NotImplementedError.

Changed in horizon:
assignee: nobody → Radomir Dopieralski (thesheep)
description: updated
Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix proposed to horizon (master)

Fix proposed to branch: master
Review: https://review.openstack.org/72629

Changed in horizon:
status: New → In Progress
Revision history for this message
Radomir Dopieralski (deshipu) wrote :

Because calling super() is the only valid way to do multiple inheritance in Python. If you don't call super when you are calling the supermethod, and later anybody tries to use your class with a mixin, they will run into the diamond problem (http://en.wikipedia.org/wiki/Diamond_problem#The_diamond_problem). For detailed discussion, see http://rhettinger.wordpress.com/2011/05/26/super-considered-super/ and python's documentation at http://docs.python.org/2/library/functions.html#super

Revision history for this message
OpenStack Infra (hudson-openstack) wrote : Fix merged to horizon (master)

Reviewed: https://review.openstack.org/72629
Committed: https://git.openstack.org/cgit/openstack/horizon/commit/?id=0f2bf801af4be94e29df26c351ef1c084ff8dd2f
Submitter: Jenkins
Branch: master

commit 0f2bf801af4be94e29df26c351ef1c084ff8dd2f
Author: Radomir Dopieralski <email address hidden>
Date: Tue Feb 11 13:04:32 2014 +0100

    Remove NotImplementedErrors from "virtual" methods

    Raising a NotImplementedError in a method of a base class
    makes it impossible to use super() with it and do multiple
    inheritance properly. Those methods should instead simply
    return some default "empty" value, so that the methods in
    the subclasses can still call them, if that's where they
    fall in the MRO.

    Change-Id: I581391e87dc58f0f76adea3dbf90cef173d3daef
    Closes-bug: 1278825

Changed in horizon:
status: In Progress → Fix Committed
Akihiro Motoki (amotoki)
Changed in horizon:
milestone: none → juno-rc1
Akihiro Motoki (amotoki)
Changed in horizon:
importance: Undecided → Medium
Thierry Carrez (ttx)
Changed in horizon:
status: Fix Committed → Fix Released
Thierry Carrez (ttx)
Changed in horizon:
milestone: juno-rc1 → 2014.2
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.