jenkins-bot submitted this change.

View Change

Approvals: Matěj Suchánek: Looks good to me, approved jenkins-bot: Verified
[IMPR] raise KeyError instead of AttributeError if FileInfo is used as Mapping

For mapping types, if key is missing, KeyError should be raised, see
https://docs.python.org/3/reference/datamodel.html?highlight=__getitem__#object.__getitem__

Change-Id: I84aef0a0b7033318c2f1d4ab850c65cb283a9210
---
M pywikibot/page/_filepage.py
1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/pywikibot/page/_filepage.py b/pywikibot/page/_filepage.py
index 1616bb7..bd627e4 100644
--- a/pywikibot/page/_filepage.py
+++ b/pywikibot/page/_filepage.py
@@ -360,21 +360,29 @@
<pywikibot.site._apisite.APISite.loadimageinfo>` for details.

.. note:: timestamp will be casted to :func:`pywikibot.Timestamp`.
+
+ .. versionchanged:: 7.7
+ raises KeyError instead of AttributeError if FileInfo is used as
+ Mapping.
"""

def __init__(self, file_revision) -> None:
- """Initiate the class using the dict from `APISite.loadimageinfo`."""
+ """Initiate the class using the dict from ``APISite.loadimageinfo``."""
self.__dict__.update(file_revision)
self.timestamp = pywikibot.Timestamp.fromISOformat(self.timestamp)

def __getitem__(self, key):
"""Give access to class values by key."""
- return getattr(self, key)
+ try:
+ result = getattr(self, key)
+ except AttributeError as e:
+ raise KeyError(str(e).replace('attribute', 'key')) from None
+ return result

def __repr__(self) -> str:
"""Return a more complete string representation."""
return repr(self.__dict__)

def __eq__(self, other) -> bool:
- """Test if two File_info objects are equal."""
+ """Test if two FileInfo objects are equal."""
return self.__dict__ == other.__dict__

To view, visit change 825392. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I84aef0a0b7033318c2f1d4ab850c65cb283a9210
Gerrit-Change-Number: 825392
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <info@gno.de>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97@gmail.com>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Mpaa <mpaa.wiki@gmail.com>
Gerrit-MessageType: merged