[pygobject] docs: Add a keyword value of None for allow-none annotations



commit ac776da7e56b78a2fa422487f0ef0d8771bcb78f
Author: Simon Feltman <sfeltman src gnome org>
Date:   Sun Jul 28 00:01:35 2013 -0700

    docs: Add a keyword value of None for allow-none annotations
    
    Update documentation generator for allow-none arguments and
    user_data arguments to show a keyword value of None.
    Add skip for GDestroyNotify closure arguments.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=640812

 gi/docstring.py         |   20 +++++++++++++++++---
 tests/test_docstring.py |   10 ++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)
---
diff --git a/gi/docstring.py b/gi/docstring.py
index 3fa4dd8..2aa2629 100644
--- a/gi/docstring.py
+++ b/gi/docstring.py
@@ -86,9 +86,23 @@ def _generate_callable_info_function_signature(info):
         elif info.is_constructor():
             in_args_strs = ['cls']
 
-    for arg in in_args:
-        argstr = arg.get_name() + ':' + arg.get_pytype_hint()
-        if arg.is_optional():
+    # Build a lists of indices prior to adding the docs because
+    # because it is possible the index retrieved comes before in
+    # argument being used.
+    ignore_indices = set([arg.get_destroy() for arg in in_args])
+    user_data_indices = set([arg.get_closure() for arg in in_args])
+
+    for i, arg in enumerate(in_args):
+        if i in ignore_indices:
+            continue
+        argstr = arg.get_name()
+        hint = arg.get_pytype_hint()
+        if hint not in ('void',):
+            argstr += ':' + hint
+        if arg.may_be_null() or i in user_data_indices:
+            # allow-none or user_data from a closure
+            argstr += '=None'
+        elif arg.is_optional():
             argstr += '=<optional>'
         in_args_strs.append(argstr)
     in_args_str = ', '.join(in_args_strs)
diff --git a/tests/test_docstring.py b/tests/test_docstring.py
index 1628295..54349b8 100644
--- a/tests/test_docstring.py
+++ b/tests/test_docstring.py
@@ -2,6 +2,7 @@ import unittest
 
 import gi.docstring
 from gi.repository import GIMarshallingTests
+from gi.repository import Gio
 
 
 class Test(unittest.TestCase):
@@ -47,3 +48,12 @@ class Test(unittest.TestCase):
     def test_overridden_doc_is_not_clobbered(self):
         self.assertEqual(GIMarshallingTests.OverridesObject.method.__doc__,
                          'Overridden doc string.')
+
+    def test_allow_none_with_user_data_defaults(self):
+        g_file_copy_doc = 'copy(self, destination:Gio.File, ' \
+                          'flags:Gio.FileCopyFlags, ' \
+                          'cancellable:Gio.Cancellable=None, ' \
+                          'progress_callback:Gio.FileProgressCallback=None, ' \
+                          'progress_callback_data=None)'
+
+        self.assertEqual(Gio.File.copy.__doc__, g_file_copy_doc)


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