jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1324217?usp=email )

Change subject: page: Reuse title section
......................................................................

page: Reuse title section

Retain the page section while formatting a title.
Reuse it when building the section suffix instead of retrieving it again.

Change-Id: I910647d057f690adc80c804ae8489d4b17869e83
---
M pywikibot/page/_basepage.py
M tests/page_tests.py
2 files changed, 19 insertions(+), 2 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/page/_basepage.py b/pywikibot/page/_basepage.py
index fbf3af0..547c4d0 100644
--- a/pywikibot/page/_basepage.py
+++ b/pywikibot/page/_basepage.py
@@ -254,8 +254,8 @@
         """
         title = self._link.canonical_title()
         label = self._link.title
-        if with_section and self.section():
-            section = '#' + self.section()
+        if with_section and (page_section := self.section()):
+            section = '#' + page_section
         else:
             section = ''
         if as_link:
diff --git a/tests/page_tests.py b/tests/page_tests.py
index d44b234..3f158cd 100755
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -169,6 +169,23 @@
             l3.ns_title(onsite=self.itws)


+class TestPageTitle(DefaultSiteTestCase):
+
+    """Test Page title formatting."""
+
+    dry = True
+
+    def test_reuses_section(self) -> None:
+        """Test that title formatting retrieves the section once."""
+        page = pywikibot.Page(self.site, 'Help:Test page#Testing')
+
+        with mock.patch.object(page, 'section', wraps=page.section) as section:
+            title = page.title()
+
+        self.assertEqual(title, 'Help:Test page#Testing')
+        section.assert_called_once_with()
+
+
 class TestPageObjectEnglish(TestCase):

     """Test Page Object using English Wikipedia."""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1324217?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I910647d057f690adc80c804ae8489d4b17869e83
Gerrit-Change-Number: 1324217
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to