[pygobject/variant-bool-maybe] variant: fix __bool__ for maybe types



commit 18c37f0ff4e3ec81f5ec384dca453d10d2e5c92f
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Wed Nov 21 23:22:18 2018 +0100

    variant: fix __bool__ for maybe types
    
    For other types it is equal to bool of the unpacked type.
    Do the same for maybe types.

 gi/overrides/GLib.py         | 7 ++-----
 tests/test_overrides_glib.py | 8 +++++---
 2 files changed, 7 insertions(+), 8 deletions(-)
---
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index 97bdfc1c..660008bc 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -384,11 +384,8 @@ class Variant(GLib.Variant):
         # Array, dict, tuple
         if self.get_type_string().startswith('a') or self.get_type_string().startswith('('):
             return self.n_children() != 0
-        if self.get_type_string() in ['v']:
-            # unpack works recursively, hence bool also works recursively
-            return bool(self.unpack())
-        # Everything else is True
-        return True
+        # unpack works recursively, hence bool also works recursively
+        return bool(self.unpack())
 
     def keys(self):
         if not self.get_type_string().startswith('a{'):
diff --git a/tests/test_overrides_glib.py b/tests/test_overrides_glib.py
index 947c74bf..ddaed734 100644
--- a/tests/test_overrides_glib.py
+++ b/tests/test_overrides_glib.py
@@ -313,9 +313,6 @@ class TestGVariant(unittest.TestCase):
         element = array.get_child_value(2)
         self.assertEqual(element.n_children(), 0)
 
-        variant = GLib.Variant('mai', None)
-        assert bool(variant)
-
     def test_create_complex(self):
         variant = GLib.Variant('(as)', ([],))
         self.assertEqual(variant.get_type_string(), '(as)')
@@ -660,6 +657,11 @@ class TestGVariant(unittest.TestCase):
         assert_equals_bool('v', GLib.Variant('i', 0))
         assert_equals_bool('v', GLib.Variant('i', 1))
 
+        # maybe types
+        assert_equals_bool('mi', 42)
+        assert_equals_bool('mi', 0)
+        assert_equals_bool('mi', None)
+
     def test_repr(self):
         # with C constructor
         v = GLib.Variant.new_uint32(42)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]