jenkins-bot has submitted this change and it was merged.
Change subject: Fix Namespace.__repr__ ......................................................................
Fix Namespace.__repr__
The __repr__ in the recently added Namespace class is not able to be used to reconstruct a a Namespace instance, as it pushed an unexploded kwargs into the constructor, resulting in a variable 'kwargs' being stored in Namespace.info.
Change-Id: I41404c57e58cf80ea259924c1b07e7a397bf8324 --- M pywikibot/site.py M tests/namespace_tests.py 2 files changed, 35 insertions(+), 3 deletions(-)
Approvals: Mpaa: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index 16415fb..597b35d 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -332,10 +332,16 @@
def __repr__(self): """Return a reconstructable representation.""" - return '%s(id=%d, custom_name=%r, canonical_name=%r, aliases=%r, ' \ - 'kwargs=%r)' \ + if self.info: + kwargs = ', ' + ', '.join([key + '=' + repr(value) + for (key, value) in + [(key, self.info[key]) + for key in sorted(self.info)]]) + else: + kwargs = '' + return '%s(id=%d, custom_name=%r, canonical_name=%r, aliases=%r%s)' \ % (self.__class__.__name__, self.id, self.custom_name, - self.canonical_name, self.aliases, self.info) + self.canonical_name, self.aliases, kwargs)
@staticmethod def builtin_namespaces(use_image_name=False): diff --git a/tests/namespace_tests.py b/tests/namespace_tests.py index b6fe44f..cbf69c9 100644 --- a/tests/namespace_tests.py +++ b/tests/namespace_tests.py @@ -182,6 +182,32 @@ self.assertEquals(Namespace.normalize_name(u'::File:'), False) self.assertEquals(Namespace.normalize_name(u'::File::'), False)
+ def test_repr(self): + a = Namespace(id=0, canonical_name=u'Foo') + s = repr(a) + r = "Namespace(id=0, custom_name=u'Foo', canonical_name=u'Foo', aliases=[])" + self.assertEquals(s, r) + + a.info['defaultcontentmodel'] = 'bar' + r = {'defaultcontentmodel': 'bar'} + self.assertEquals(a.info, r) + + s = repr(a) + r = "Namespace(id=0, custom_name=u'Foo', canonical_name=u'Foo', aliases=[], defaultcontentmodel='bar')" + self.assertEquals(s, r) + + a.info['case'] = 'upper' + r = {'defaultcontentmodel': 'bar', 'case': 'upper'} + self.assertEquals(a.info, r) + + s = repr(a) + r = "Namespace(id=0, custom_name=u'Foo', canonical_name=u'Foo', aliases=[], case='upper', defaultcontentmodel='bar')" + self.assertEquals(s, r) + + b = eval(repr(a)) + self.assertEquals(a, b) + self.assertEquals(a.info, b.info) +
if __name__ == '__main__': try:
pywikibot-commits@lists.wikimedia.org