GNOME Bugzilla – Bug 639904
make representation of enum members follow common Python recommendations
Last modified: 2012-04-22 08:02:08 UTC
From __repr__ documentation: Called by the repr() built-in function and by string conversions (reverse quotes) to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). Enum members obviously have a trivial evaluatable representation in an environment with some required imports. Therefore, by Python rules it should hold that e.g.: repr (gtk.RESPONSE_ACCEPT) == 'gtk.RESPONSE_ACCEPT' I.e. __repr__ in enum members should drop '<...>' representation as not useful and instead return the member name with the required modules.
Mentioned in IRC channel and two people decided to submit this bug. *** This bug has been marked as a duplicate of bug 639903 ***
Hm, should have read the other before closing.
Still an issue with current 3.2: >>> from gi.repository import Gtk >>> Gtk.ResponseType.ACCEPT <enum GTK_RESPONSE_ACCEPT of type GtkResponseType> >>> repr(Gtk.ResponseType.ACCEPT) '<enum GTK_RESPONSE_ACCEPT of type GtkResponseType>'
Created attachment 212501 [details] [review] draft of a patch This is a sketch how this would look like, including a test case. The repr() comes out as gi.repository.GIMarshallingTests.Enum.value1. Problems: - This affects all identifiers in PyGObject, not just enums, it's a global problem. - The enum value is in lower case, but needs to be upper case (I can't find the code which parses a value like Gtk.ResponseType.CANCEL to match the nick "cancel" or name "GTK_RESPONSE_TYPE_CANCEL") - This currently prepends __module__, which is gi.repository.GIMarshallingTests. But it should be "GIMarshallingtests", unless you actually did "import gi.repository.GIMarshallingTests" instead of the usual "from gi.repository import GIMarshallingTests".
*** This bug has been marked as a duplicate of bug 657915 ***