Page MenuHomePhabricator

Exception names does not follow PEP8
Closed, ResolvedPublic

Description

Introduction

The PEP8 naming convention suggests that exceptions should have the suffix "Error" on exception names (if the exception actually is an error) but a lot of exceptions does not follow them.

Proposal

  • rename exceptions to follow the naming convention
  • Concider whether there are Pywikibot Errors which should not follow this rule
  • Find a way to deprecate the renamed exception

Hints

Here is a code snipped how to proceed and implement a deprecation warning:

class NoPageError(PageRelatedError):

    """Page does not exist."""

    message = "Page %s doesn't exist."

class DeprecatedException:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        print('Exception "{}" is deprecated'.format(self.__class__.__name__))
    def __str__(self):
        print('Exception "{}" is deprecated'.format(self.__class__.__name__))
        super().__getattr__(attr)
        
class NoPage(DeprecatedExcption, NoPageError):
    pass

DeprecatedException class should use pywikibot.tools.issue_deprecation_warning to print the warning. Use a separate method to do it. The parameters like instead or since should be passed as keyword parameters to DeprecatedException.__init__. We always can use a FutureWarning as warning_class.

Please always split your commits into parts and do not patch more than 10 files at once for better reviewing.

Links

Event Timeline

Xqt triaged this task as Low priority.Apr 15 2021, 9:00 AM

Wait, we have just to check where "Error" suffix should be omitted

Change 682277 had a related patch set uploaded (by Damian; author: Damian):

[pywikibot/core@master] [cleanup] Suffix all exceptions with 'Error'

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

@atagar: When I run the below code with your patch (PS4), I don't see any warnings for using NoPage.

t.py
import pywikibot
from pywikibot import NoPage  # same behavior if imported from pywikibot.exceptions
from pywikibot.exceptions import NoPageError

pywikibot.handle_args()
site = pywikibot.Site()
page = pywikibot.Page(site, 'Example')

try:
    raise NoPage(page)
except NoPageError as e:
    print('success: NoPageError caught NoPage:', e)
$ python t.py -log:t.log
success: NoPageError caught NoPage: Page [[en:Example]] doesn't exist.
$ cat logs/t.log
[...]
2021-04-27 18:24:14             bot.py,  367 in     writelogheader: VERBOSE  === Pywikibot framework v6.1.1.dev0 -- Logging header ===
2021-04-27 18:24:14             bot.py,  370 in     writelogheader: VERBOSE  COMMAND: ['t.py', '-log:t.log']
2021-04-27 18:24:14             bot.py,  373 in     writelogheader: VERBOSE  DATE: 2021-04-28 01:24:14.810969 UTC
2021-04-27 18:24:14             bot.py,  378 in     writelogheader: VERBOSE  VERSION: [ssh] pywikibot-core (20cf3df, g14708, 2021/04/23, 22:28:45, n/a)
2021-04-27 18:24:14             bot.py,  384 in     writelogheader: VERBOSE  SYSTEM: posix.uname_result(sysname='Linux', nodename='DESKTOP-PNGJ3TJ', release='4.4.0-19041-Microsoft', version='#488-Microsoft Mon Sep 01 13:43:00 PST 2020', machine='x86_64')
[...]
2021-04-27 18:24:14             bot.py,  431 in     writelogheader: VERBOSE  =========================================================
2021-04-27 18:24:14        throttle.py,  147 in  checkMultiplicity: VERBOSE  Found 1 wikipedia:en processes running, including this one.
2021-04-27 18:24:14        __init__.py, 1272 in             _flush: VERBOSE  Dropped throttle(s).
2021-04-27 18:24:14            http.py,   79 in             _flush: VERBOSE  Closing network session.
2021-04-27 18:24:14            http.py,   85 in             _flush: VERBOSE  Network session closed.

Thanks @JJMC89! Great point. Code review updated with a better approach which fixes that.

I still don't see a warning on PS6. I do get one if NoPage is imported from pywikibot.exceptions.

That's odd. I do get it...

% cat demo.py 
import pywikibot
from pywikibot import NoPage  # same behavior if imported from pywikibot.exceptions
from pywikibot.exceptions import NoPageError

pywikibot.handle_args()
site = pywikibot.Site()
page = pywikibot.Page(site, 'Example')

try:
    raise NoPage(page)
except NoPageError as e:
    print('success: NoPageError caught NoPage:', e)

% python demo.py 
demo.py:2: DeprecationWarning: pywikibot.NoPage is deprecated for 5 days; use pywikibot.exceptions.NoPageError instead.
  from pywikibot import NoPage  # same behavior if imported from pywikibot.exceptions
success: NoPageError caught NoPage: Page [[en:Example]] doesn't exist.

I get the warning with Python 3.7.3 but not with 3.5.3.

That's... really odd. I have Python 3.8 and unfortunately installing a deprecated 3.5 interpreter would be a pita. Do you have any idea what's going on or should I try to get one working?

Not sure. It isn't just these new deprecations though. I also don't get a warning for pywikibot.MediaWikiVersion with Python 3.5.3 but do with 3.7.3. Others like QuitKeyboardInterrupt work fine.

There seems to be a Python 3.5 issue regarding ModuleDeprecationWrapper. Would you like for me to install Python 3.5 to troubleshoot or should we continue with this?

Personally I think we should deprecate Python 3.5 support since it reached EoL in September 2020: https://www.python.org/downloads/release/python-3510/

Change 682277 merged by jenkins-bot:

[pywikibot/core@master] [cleanup] Suffix exceptions with 'Error'

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

There seems to be a Python 3.5 issue regarding ModuleDeprecationWrapper. Would you like for me to install Python 3.5 to troubleshoot or should we continue with this?

Personally I think we should deprecate Python 3.5 support since it reached EoL in September 2020: https://www.python.org/downloads/release/python-3510/

Looks like a DeprecationWarning issue. Changing to FutureWarning will print the message during import.

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

[pywikibot/core@master] [Error] use exceptions.Error instead of pywikibot.Error

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

Change 683913 merged by jenkins-bot:

[pywikibot/core@master] [Error] use exceptions.Error instead of pywikibot.Error

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

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

[pywikibot/core@master] [cleanup] Remove deprcated pywikibot.Error exceptions.py

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

Change 721574 merged by jenkins-bot:

[pywikibot/core@master] [cleanup] Remove deprecated pywikibot.Error exceptions.py

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