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

Change subject: lexeme: Initialize collections for unsaved entities
......................................................................

lexeme: Initialize collections for unsaved entities

Assigning lemmas before accessing another collection lets lazy
initialization replace the assigned data. Initialize new Lexeme
collections in the constructor so later access preserves local fields.

Bug: T442290
Change-Id: Ie890e53ef239da95b848a02277cf5844efb2c867
---
M pywikibot/page/_wikibase.py
M tests/wikibase_tests.py
2 files changed, 26 insertions(+), 1 deletion(-)

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




diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py
index c0c1ae1..20c4976 100644
--- a/pywikibot/page/_wikibase.py
+++ b/pywikibot/page/_wikibase.py
@@ -2333,7 +2333,7 @@
     }

     def __init__(self, site, title: str | None = None) -> None:
-        """Initializer.
+        """Initialize a Lexeme, with empty collections for a new entity.

         :param site: Data repository
         :type site: Pywikibot.site.DataSite
@@ -2344,6 +2344,7 @@
         if title is None or title == '-1':
             super().__init__(site, '-1', entity_type='lexeme')
             assert self.id == '-1'
+            self._initialize_empty()
             return

         # we don't want empty titles
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 9950340..fc7ca27 100755
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -1158,6 +1158,30 @@
             ItemPage._normalizeData(data)


+class TestLexemePage(WikidataTestCase):
+
+    """Test Lexeme initialization without accessing a wiki."""
+
+    dry = True
+
+    def test_unsaved_fields(self) -> None:
+        """Collection access must preserve directly assigned lemmas."""
+        repo = self.get_repo()
+        namespace = Namespace(146, canonical_name='Lexeme',
+                              case='first-letter',
+                              defaultcontentmodel='wikibase-lexeme')
+        namespaces = NamespacesDict({**repo.namespaces, 146: namespace})
+        with patch.object(repo, '_namespaces', namespaces), \
+                patch.object(repo, '_entity_namespaces',
+                             {'lexeme': namespace}, create=True):
+            lexeme = LexemePage(repo)
+        lexeme.lemmas = LanguageDict({'en': 'colour'})
+        self.assertIsEmpty(lexeme.forms)
+        self.assertIsEmpty(lexeme.senses)
+        self.assertIsEmpty(lexeme.claims)
+        self.assertEqual(lexeme.lemmas, {'en': 'colour'})
+
+
 class TestLexemeNormalizeData(WikidataTestCase):

     """Test normalization of Lexeme and form fields."""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1358459?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: Ie890e53ef239da95b848a02277cf5844efb2c867
Gerrit-Change-Number: 1358459
Gerrit-PatchSet: 3
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