Page MenuHomePhabricator

Use of invalid variable for ImportErrors
Closed, DuplicatePublic

Description

There are lots of cases where we do:

try:
    from pywikibot.userinterfaces.gui import Tkdialog
except ImportError as _tk_error:
    Tkdialog = None

# ...
if Tkdialog is None:
    pywikibot.warning(_tk_error, ImportWarning)

This needs to be changed to:

try:
    from pywikibot.userinterfaces.gui import Tkdialog
except ImportError as _tk_error:
    Tkdialog = _tk_error

# ...
if isinstance(Tkdialog, ImportError):
    pywikibot.warning(TkDialog, ImportWarning)

The former method id actually invalid because python doesn't guarantee that the variable _tk_error exists outside the except statement.

Places that flake8-py3 detects it:

04:27:53 ./scripts/flickrripper.py:318:35: F821 undefined name '_tk_error'
04:28:05 ./pywikibot/botirc.py:34:19: F821 undefined name 'e'
04:28:06 ./pywikibot/diff.py:584:15: F821 undefined name 'bserror'