Repository: LeMyst/WikibaseIntegrator

Issue:
Either support single non-list qualifiers or give users a clear error message
Add check if self.qualifiers is_instance(List, self.qualifiers) and Give users a clear error message if they don't provide a list or
support a single qualifier also

https://github.com/LeMyst/WikibaseIntegrator/blob/9d7552db2b07f643a5a0fba303feb5c03e98e00a/wikibaseintegrator/wbi_datatype.py#L76

Discussion:

Patch:
diff --git a/wikibaseintegrator/wbi_datatype.py b/wikibaseintegrator/wbi_datatype.py
index 8f54aee..ae29523 100644
--- a/wikibaseintegrator/wbi_datatype.py
+++ b/wikibaseintegrator/wbi_datatype.py
@@ -64,8 +64,16 @@ class BaseDataType(object):
         if not self.references:
             self.references = []
         else:
+            if isinstance(self.references, BaseDataType):
+                self.references = [[self.references]]
+
             for ref_list in self.references:
+                if isinstance(ref_list, BaseDataType):
+                    ref_list = [ref_list]
                 for reference in ref_list:
+                    if not isinstance(reference, BaseDataType):
+                        raise ValueError('A reference must be an instance of class BaseDataType.')
+
                     if reference.is_reference is False:
                         raise ValueError('A reference can\'t be declared as is_reference=False')
                     elif reference.is_reference is None:
@@ -74,7 +82,12 @@ class BaseDataType(object):
         if not self.qualifiers:
             self.qualifiers = []
         else:
+            if isinstance(self.qualifiers, BaseDataType):
+                self.qualifiers = [self.qualifiers]
+
             for qualifier in self.qualifiers:
+                if not isinstance(qualifier, BaseDataType):
+                    raise ValueError('A qualifier must be an instance of class BaseDataType.')
                 if qualifier.is_qualifier is False:
                     raise ValueError('A qualifier can\'t be declared as is_qualifier=False')
                 elif qualifier.is_qualifier is None:


Test patch:
diff --git a/test/test_wbi_datatype.py b/test/test_wbi_datatype.py
new file mode 100644
index 0000000..6330668
--- /dev/null
+++ b/test/test_wbi_datatype.py
@@ -0,0 +1,48 @@
+import unittest
+
+from wikibaseintegrator import wbi_datatype
+
+
+class TestWbiDataType(unittest.TestCase):
+    def test_qualifier(self):
+        # Good
+        qualifiers = [wbi_datatype.ExternalID(value='P58742', prop_nr='P352', is_qualifier=True),
+                      wbi_datatype.ItemID(value='Q24784025', prop_nr='P527', is_qualifier=True),
+                      wbi_datatype.Time(time='+2001-12-31T12:01:13Z', prop_nr='P813', is_qualifier=True)]
+        wbi_datatype.ItemID("Q123", "P123", qualifiers=qualifiers)
+
+        qualifiers = wbi_datatype.ExternalID(value='P58742', prop_nr='P352', is_qualifier=True)
+        wbi_datatype.ItemID("Q123", "P123", qualifiers=qualifiers)
+
+        # Bad
+        qualifiers = wbi_datatype.ExternalID(value='P58742', prop_nr='P352', is_qualifier=False)
+        with self.assertRaises(ValueError):
+            wbi_datatype.ItemID("Q123", "P123", qualifiers=qualifiers)
+
+        bad_qualifiers = ["not a good qualifier",
+                          wbi_datatype.ItemID(value='Q24784025', prop_nr='P527', is_qualifier=True),
+                          wbi_datatype.Time(time='+2001-12-31T12:01:13Z', prop_nr='P813', is_qualifier=True)]
+        with self.assertRaises(ValueError):
+            wbi_datatype.ItemID("Q123", "P123", qualifiers=bad_qualifiers)
+
+    def test_references(self):
+        # Good
+        references = [wbi_datatype.ExternalID(value='P58742', prop_nr='P352', is_reference=True),
+                      wbi_datatype.ItemID(value='Q24784025', prop_nr='P527', is_reference=True),
+                      wbi_datatype.Time(time='+2001-12-31T12:01:13Z', prop_nr='P813', is_reference=True)]
+        wbi_datatype.ItemID("Q123", "P123", references=[references])
+        wbi_datatype.ItemID("Q123", "P123", references=references)
+
+        references = wbi_datatype.ExternalID(value='P58742', prop_nr='P352', is_reference=True)
+        wbi_datatype.ItemID("Q123", "P123", references=references)
+
+        # Bad
+        references = wbi_datatype.ExternalID(value='P58742', prop_nr='P352', is_reference=False)
+        with self.assertRaises(ValueError):
+            wbi_datatype.ItemID("Q123", "P123", references=references)
+
+        bad_references = ["not a good reference",
+                          wbi_datatype.ItemID(value='Q24784025', prop_nr='P527', is_reference=True),
+                          wbi_datatype.Time(time='+2001-12-31T12:01:13Z', prop_nr='P813', is_reference=True)]
+        with self.assertRaises(ValueError):
+            wbi_datatype.ItemID("Q123", "P123", qualifiers=bad_references)