GNOME Bugzilla – Bug 680009
gtk_tree_model_sort_new_with_model() is not annotated as a constructor
Last modified: 2018-02-10 04:37:23 UTC
Hello, I'm porting an Log Activity (from Sugar) and I found this gtk2 code on it: self._treemodel = gtk.TreeStore(gobject.TYPE_STRING) sorted = gtk.TreeModelSort(self._treemodel) So, after running the pygi-convert.sh script it replaced this chunk of code by: self._treemodel = Gtk.TreeStore(GObject.TYPE_STRING) sorted = Gtk.TreeModelSort(self._treemodel) but I noticed that Gtk.TreeModelSort can be instanciated with an argument. I got this error: Traceback (most recent call last):
+ Trace 230512
main()
instance = create_activity_instance(activity_constructor, activity_handle)
activity = constructor(handle)
self.viewer = MultiLogView(paths, ext_files)
self._build_treeview()
sorted = Gtk.TreeModelSort(self._treemodel)
First, I tried to use the .new_with_model() constructor: from gi.repository import GObject from gi.repository import Gtk _treemodel = Gtk.TreeStore(GObject.TYPE_STRING) s = Gtk.TreeModelSort.new_with_model(_treemodel) [...] AttributeError: type object 'TreeModelSort' has no attribute 'new_with_model' So, I tried to set it using .set_property() method: s = Gtk.TreeModelSort() s.set_property('model', _treemodel) [...] TypeError: property 'model' can only be set in constructor I think this is a bug because I didn't find the way to do this.
Indeed it is wrongly annotated to be a method of Gtk.TreeModel right now. I. e. if you have a treemodel "model", you currently need to call sorted = model.sort_new_with_model() to get a Gtk.TreeModelSort instance. This looks confusing, and should rather be sorted = Gtk.TreeModelSort.new_with_model(model)
This is actually not that simple to fix with a mere (constructor) annotation, because gtk_tree_model_sort_new_with_model() is not a real constructor in the sense that it returns a GtkTreeModel*, not a GtkTreeModelSort*. So the g-i scanner complains about constructors needing to return the type that they are supposed to construct. However, changing it to return a GtkTreeModelSort* changes the API in the sense of introducing type incompatibility and thus compiler warnings into previously warning-free code.
We're moving to gitlab! As part of this move, we are closing bugs that haven't seen activity in more than 5 years. If this issue is still imporant to you and still relevant with GTK+ 3.22 or master, please consider creating a gitlab issue for it.