jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/825392 )
Change subject: [IMPR] raise KeyError instead of AttributeError if FileInfo is used as Mapping ......................................................................
[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__#obj...
Change-Id: I84aef0a0b7033318c2f1d4ab850c65cb283a9210 --- M pywikibot/page/_filepage.py 1 file changed, 11 insertions(+), 3 deletions(-)
Approvals: Matěj Suchánek: Looks good to me, approved jenkins-bot: Verified
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__