GNOME Bugzilla – Bug 711487
GObject constructor for Gtk classes silently accepts and ignores positional arguments
Last modified: 2013-11-06 22:53:37 UTC
Spotted in https://bugs.launchpad.net/ubuntu/+source/pygobject/+bug/1248152: The reporter tried to create a FileChooserWidget like this: chooser = Gtk.FileChooserWidget(self, Gtk.FileChooserAction.SELECT_FOLDER) the "self" does not make any sense there, but the intended meaning was chooser = Gtk.FileChooserWidget(Gtk.FileChooserAction.SELECT_FOLDER) this silently succeeds, but does not have any actual effect. In fact, it seems that this is now the case for every widget which doesn't have an override: python3 -c 'from gi.repository import Gtk; w = Gtk.Button(2, 3, 4); print(w)' <Button object at 0x7eff00e55780 (GtkButton at 0x129b180)> So it seems you can specify arbitrary arguments now which get silently ignored. In the past this used to raise a meaningful exception, we should restore that to avoid confusion.
This might be specific to Gtk. It works fine with other classes: $ python3 -c 'from gi.repository import Gio; o = Gio.Application(1); print(o)' Traceback (most recent call last):
+ Trace 232716
$ python3 -c 'from gi.repository import GUdev; o = GUdev.Client(2, 3); print(o)' Traceback (most recent call last): File "<string>", line 1, in <module> TypeError: GObject.__init__() takes exactly 0 arguments (2 given)
Ignore the Gtk.Button example in the description, Gtk.Button does have an overridden constructor. I wrote a test case using Gtk.Widget and Gtk.Container (have overrides, but no overridden ctor) and Gtk.FileChooserWidget (no override at all).
This is because we have overrides for the parent classes, in particular Gtk.Bin and Gtk.Window. These don't do sufficient type checking, and they should ideally not apply to all subclasses.
This can't be fixed fully as long as we still support the old PyGTK style constructors, but this should at least avoid most pitfalls: https://git.gnome.org/browse/pygobject/commit/?id=7193f050
(In reply to comment #0) > ... > In the past this used to raise a meaningful exception, we should restore that > to avoid confusion. I'm not sure what changed this, but there is a patch in bug 705810 which should fix this for all cases (instead of needing overrides): See pygobject.c:pygobject_init in this patch: https://bugzilla.gnome.org/show_bug.cgi?id=705810#c13
(In reply to comment #5) > I'm not sure what changed this, but there is a patch in bug 705810 which should > fix this for all cases (instead of needing overrides): Ah, never mind, it looks like those existing overrides don't accept *args anyhow. However, the patch mentioned fixes the case where *args are passed to a non-overridden GObject class (and subsequently ignored).