It's difficult to override the collection converter in a subclass of MappedCollection

Issue #2654 resolved
Luca Wehrstedt created an issue

"MappedCollection" defines its own converter method, called "_converter" and annotates it with "@collection.converter". This decorator assigns the "_sa_instrument_role" attribute the value of "converter". The _instrument_class function then finds this methods, recognizes it as the converter and thus assigns it to the "_sa_converter" attribute.

A subclass of "MappedCollection" that wants to define its own converter is supposed to override the "_convert" method. Yet, when this class is instrumented, the "_instrument_class" function finds both the "_convert" and the "_sa_coverter" methods (the latter one inherited from the instrumented "MappedCollection"), both with the "_sa_instrument_role" set to "converter". It often (always?) chooses the second one, reassigning it to the "_sa_converter" of the subclass and causing the overridden converter (i.e. "_convert") to never get called.

A possible solution is to make "instrument_class" ignore the methods in the form "_sa<role>". Or it could remove the "sa_instrument_role" attribute from methods after the instrumentation of a class is done. Or it could create a copy of the methods, remove the "_sa_instrument_role" attribute and assign those to the "_sa<role>" attributes of the class. Each solution has its pros and cons.

Comments (2)

  1. Log in to comment