jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/571984 )
Change subject: [bugfix] Return a single Coordinate with primary_only option ......................................................................
[bugfix] Return a single Coordinate with primary_only option
- if primary_only parameter is set in Page.coordinates() return the first primary Coordinate object found in self._coords or None if no primary Coordinate is found - otherwise return a list of all Coordinates - tests added
Bug: T244963 Change-Id: Iafac7561dff1bc7512c69db7a3a526a92642ef1f --- M pywikibot/data/api.py M pywikibot/page.py M tests/page_tests.py 3 files changed, 34 insertions(+), 6 deletions(-)
Approvals: JJMC89: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py index a75b09e..477010d 100644 --- a/pywikibot/data/api.py +++ b/pywikibot/data/api.py @@ -3301,7 +3301,7 @@ name=co.get('name', ''), dim=int(co.get('dim', 0)) or None, globe=co['globe'], # See [[gerrit:67886]] - primary=True if 'primary' in co else False + primary='primary' in co ) coords.append(coord) page._coords = coords diff --git a/pywikibot/page.py b/pywikibot/page.py index 1e6746a..c408295 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -1666,16 +1666,19 @@ Uses the MediaWiki extension GeoData.
@param primary_only: Only return the coordinate indicated to be primary - @return: A list of Coordinate objects - @rtype: list + @return: A list of Coordinate objects or a single Coordinate if + primary_only is True + @rtype: list of Coordinate or Coordinate or None """ if not hasattr(self, '_coords'): self._coords = [] self.site.loadcoordinfo(self) if primary_only: - return [coord for coord in self._coords if coord.primary] - else: - return self._coords + for coord in self._coords: + if coord.primary: + return coord + return None + return list(self._coords)
@need_version('1.20') def page_image(self): diff --git a/tests/page_tests.py b/tests/page_tests.py index ad86eaf..1acf918 100644 --- a/tests/page_tests.py +++ b/tests/page_tests.py @@ -612,6 +612,31 @@ mainpage.page_image)
+class TestPageCoordinates(TestCase): + + """Test Page Object using German Wikipedia.""" + + family = 'wikipedia' + code = 'de' + + cached = True + + def test_coordinates(self): + """Test C{Page.coodinates} method.""" + page = pywikibot.Page(self.site, 'Berlin') + with self.subTest(primary_only=False): + coords = page.coordinates() + self.assertIsInstance(coords, list) + for coord in coords: + self.assertIsInstance(coord, pywikibot.Coordinate) + self.assertIsInstance(coord.primary, bool) + + with self.subTest(primary_only=True): + coord = page.coordinates(primary_only=True) + self.assertIsInstance(coord, pywikibot.Coordinate) + self.assertTrue(coord.primary) + + class TestPageDeprecation(DefaultSiteTestCase, DeprecationTestCase):
"""Test deprecation of Page attributes."""
pywikibot-commits@lists.wikimedia.org