Page MenuHomePhabricator

Add support for BaseException.add_notes method for older Pythons
Closed, DeclinedPublicFeature

Description

Introduction

Python 3.11 introduced the Exception.add_note() method, which allows attaching additional context notes to exceptions. These notes are displayed in the exception traceback, helping developers debug issues more easily.

Pywikibot currently needs to support older Python versions (< 3.11). To provide similar functionality, we want to backport add_note() method for all exceptions.Error classes.

Goal
Refactor pywikibot.exceptions.Error of the exceptions module to derive from a new BaseError instead of the built-in Exception. For Python < 3.11, BaseError should implement Python 3.11-style Exception.add_note() functionality within backports module.

Steps

  1. Create a new class BaseError in the backports module
    • Derive BaseError from Exception.
    • For Python < 3.11 (tools.PYTHON_VERSION < (3, 11)):
      • Implement add_note(self, note: str) storing notes in an instance attribute self.__notes__.
      • Override __str__() so that super().__str__() is followed by each note on a new line.
  1. Import BaseError from backports as _BaseError in the in exceptions module (_BaseError to keep the class private). Make pywikibot.exceptions.Error inherit from _BaseError instead of Exception.
  1. Example usage:
 >>> try:
>>>     1/0
>>> except pywikibot.exceptions.Error as e:
>>>     e.add_note('omg!')
>>>     e.add_note('does it work?')
>>>     raise
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
omg!
does it work?

Further help

Hint
pre-commit is strictly recommended before submitting a patch. Install it with pip install pre-commit and run it with pre-commit run --all-files

Event Timeline

Thank you for tagging this task with good first task for Wikimedia newcomers!

Newcomers often may not be aware of things that may seem obvious to seasoned contributors, so please take a moment to reflect on how this task might look to somebody who has never contributed to Wikimedia projects.

A good first task is a self-contained, non-controversial task with a clear approach. It should be well-described with pointers to help a completely new contributor, for example it should clearly point to the codebase URL and provide clear steps to help a contributor get set up for success. We've included some guidelines at https://phabricator.wikimedia.org/tag/good_first_task/ !

Thank you for helping us drive new contributions to our projects <3

Xqt triaged this task as Low priority.Thu, Feb 5, 10:44 AM

@NAGAM_SURYA_NARAYANA: Just follow the steps listed above. But first, make sure you are familiar with the initial steps in the "Further Help" section — in particular, T407059 is very helpful.

Thank you @Xqt for assigning this task and for the guidance.

I’ve reviewed the task description, the steps, and the Further Help links (especially T407059).
I understand the goal and will now proceed with the following:
• Set up the Pywikibot environment
• Review the backports module and exceptions module
• Implement BaseError with add_note() for Python < 3.11
• Update pywikibot.exceptions.Error to inherit from the new _BaseError

I’ll start working on this and update the task if I have any questions.
Thank you again for your support and mentorship.

@NAGAM_SURYA_NARAYANA I noticed you are assigned to T415976. Since you mentioned in T416116 that you plan to finish that before switching to T416566, would you mind if I started looking into this task (T416566) in the meantime? I am also happy to collaborate with you on this one if you prefer.

Thanks for checking!
Yes, that works for me — I’m currently focusing on T416116, so please feel free to start looking into T416566. Happy to collaborate later if needed.

ok thanks, let me start working on it.

Hi @Xqt,

Thanks for the context here. Since @anoexpected has already started working on this task, I’ll hold off to avoid duplicate work.

Please let me know if you’d like me to take over later or pick up a different task.

Change #1237469 had a related patch set uploaded (by Anotida Expected; author: Anotida Expected):

[pywikibot/core@master] [exceptions] Backport Exception.add_note() for Python < 3.11

https://gerrit.wikimedia.org/r/1237469

Xqt changed the subtype of this task from "Task" to "Feature Request".
Xqt added a subscriber: NAGAM_SURYA_NARAYANA.

Change #1237469 merged by jenkins-bot:

[pywikibot/core@master] [exceptions] Backport Exception.add_note() for Python < 3.11

https://gerrit.wikimedia.org/r/1237469

Change #1237638 had a related patch set uploaded (by Xqt; author: Xqt):

[pywikibot/core@master] Revert "[exceptions] Backport Exception.add_note() for Python < 3.11"

https://gerrit.wikimedia.org/r/1237638

Change #1237638 merged by Xqt:

[pywikibot/core@master] Revert "[exceptions] Backport Exception.add_note() for Python < 3.11"

https://gerrit.wikimedia.org/r/1237638

Change #1237674 had a related patch set uploaded (by Anotida Expected; author: Anotida Expected):

[pywikibot/core@master] [exceptions] Use sys.excepthook to display backported notes

https://gerrit.wikimedia.org/r/1237674

Change #1237674 abandoned by Xqt:

[pywikibot/core@master] [exceptions] Use sys.excepthook to display backported notes

Reason:

Tasks were declined

https://gerrit.wikimedia.org/r/1237674

Subtask was declined