mitodl / mitx-grading-library

The MITx Grading Library, a python grading library for edX
https://edge.edx.org/courses/course-v1:MITx+grading-library+examples/
BSD 3-Clause "New" or "Revised" License
14 stars 9 forks source link

Adding plug-in to do global default overrides #250

Closed jolyonb closed 5 years ago

jolyonb commented 5 years ago

Pretty much what it says in the title.

This was a tricky little thing to get right, and involved my first ever use of my own metaclass. The basic idea is to have a default_values property associated with each class (not class instance, but class; hence the need for a metaclass). Then, right before schema validation, look up the class default_values property, and all of the superclasses, to construct the complete configuration. I think the end result is really neat though, and provides a really easy way to set global defaults.

Resolves #249.

jolyonb commented 5 years ago

So, today I learnt something about metaclasses and abstract base classes in python. Metaclasses are inherited through subclassing, so we didn't need to import abc.ABCMeta on subclasses that were abstract.

Furthermore, metaclass implementation changed between python 2 and 3, so we need to use six to apply them.

Finally, the ABCMeta metaclass only enforces that a class instance has no methods that are marked as abstract. In some sampling situations, we were marking things as abstract that had all of their methods in place, and the abstract marking was doing nothing.

Upshot of all this is that the only class that needs to be marked as abstract is ObjectWithSchema, and everything else follows from there.

jolyonb commented 5 years ago

@ChristopherChudzicki This is ready for review. Warning: short, but a little involved.

jolyonb commented 5 years ago

Ok, looks like I broke it... Be right back!

jolyonb commented 5 years ago

Should be ready for final review now.

codecov-io commented 5 years ago

Codecov Report

Merging #250 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #250   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          28     29    +1     
  Lines        2426   2452   +26     
=====================================
+ Hits         2426   2452   +26