jenkins-bot has submitted this change and it was merged.
Change subject: pywikibot: Store ImportError in imported variable ......................................................................
pywikibot: Store ImportError in imported variable
If a lazy ImportError is to be handled, the ImportError needs to be stored in a variable and then used when required. Earlier, the imported variable was set to `None` and the import error which was caught with `except` was used. But this import error may not exist at a later time as python guarantees it's existence only inside the `except` statement. Hence, this is changed so that the ImportError is saved in the variable that was being imported and checked later using `isinstance(<variable>, ImportError)`.
(Re-enable F821 disabled in previous commit on 2.0 branch)
Bug: T134336 Change-Id: I80f82e7f0674bfca70688f28640620dacac1d80b (manually cherry picked from commit 65cabc8258aa807a754a62182ae51fbf7df45ddd) --- M pywikibot/botirc.py M pywikibot/diff.py M scripts/flickrripper.py M tox.ini 4 files changed, 8 insertions(+), 13 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/botirc.py b/pywikibot/botirc.py index 77d258a..3294d76 100644 --- a/pywikibot/botirc.py +++ b/pywikibot/botirc.py @@ -25,13 +25,15 @@ try: from ircbot import SingleServerIRCBot except ImportError as e: + ircbot_import_error = e + class SingleServerIRCBot(object):
"""Fake SingleServerIRCBot."""
def __init__(*args, **kwargs): """Report import exception.""" - raise e + raise ircbot_import_error
_logger = "botirc" diff --git a/pywikibot/diff.py b/pywikibot/diff.py index 08f939e..ee41492 100644 --- a/pywikibot/diff.py +++ b/pywikibot/diff.py @@ -20,11 +20,6 @@ else: from itertools import izip_longest as zip_longest
-try: - from bs4 import BeautifulSoup -except ImportError as bserror: - BeautifulSoup = False - import pywikibot from pywikibot.tools import chars
@@ -570,9 +565,7 @@ @return: deleted and added list of contexts @rtype: dict """ - # check if BeautifulSoup imported - if not BeautifulSoup: - raise bserror # should have been raised and stored earlier. + from bs4 import BeautifulSoup
comparands = {'deleted-context': [], 'added-context': []} soup = BeautifulSoup(compare_string) diff --git a/scripts/flickrripper.py b/scripts/flickrripper.py index 38e97df..568b5f2 100755 --- a/scripts/flickrripper.py +++ b/scripts/flickrripper.py @@ -62,7 +62,7 @@ try: from pywikibot.userinterfaces.gui import Tkdialog except ImportError as _tk_error: - Tkdialog = None + Tkdialog = _tk_error
flickr_allowed_license = { @@ -295,7 +295,7 @@ override, addCategory, removeCategories) # pywikibot.output(photoDescription) - if Tkdialog is not None and not autonomous: + if not isinstance(Tkdialog, ImportError) and not autonomous: try: (newPhotoDescription, newFilename, skip) = Tkdialog( photoDescription, photo, filename).show_dialog() @@ -305,7 +305,7 @@ autonomous = True elif not autonomous: pywikibot.warning('Switching to autonomous mode because GUI interface cannot be used') - pywikibot.warning(_tk_error) + pywikibot.warning(Tkdialog) autonomous = True if autonomous: newPhotoDescription = photoDescription diff --git a/tox.ini b/tox.ini index ae7fc47..7fc1161 100644 --- a/tox.ini +++ b/tox.ini @@ -23,7 +23,7 @@ flake8-docstrings
[testenv:flake8-py3] -commands = flake8 --ignore=D102,D103,D105,D211,E122,E127,E241,E402,E731,F821 {posargs} +commands = flake8 --ignore=D102,D103,D105,D211,E122,E127,E241,E402,E731 {posargs} basepython = python3 deps = flake8 flake8-docstrings