Skip to content

Commit

Permalink
ENT-4090: Fix flake8 error F821
Browse files Browse the repository at this point in the history
* Card ID: ENT-4090

F821: undefined name

Because 'reload' and 'unicode' are undefined objects in Python 3 and
were only used in Python 2 versions of subscription-manager, these
blocks have been completely removed.

Together with them were imported some six.PY2 blocks which contained
those objects, because they are not used and the condition blocks would
be empty without them.

Other objects were missing and had to be imported: sys, typing.Optional.
  • Loading branch information
m-horky committed Sep 3, 2021
1 parent 0057aff commit 3095f59
Show file tree
Hide file tree
Showing 14 changed files with 20 additions and 53 deletions.
2 changes: 0 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ ignore =
# E741: ambiguous variable name '*'
# mostly for variables 'l', which ought to get more specific names
E741,
# F821: undefined name '*'
F821,
# W504: line break after binary operator
# we break after binary operators, newer style breaks before
W504,
Expand Down
6 changes: 2 additions & 4 deletions src/rct/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,5 @@ def __init__(self):
def xstr(value):
if value is None:
return ''
elif isinstance(value, unicode):
return value.encode('utf-8')
else:
return str(value)

return value.encode('utf-8')
1 change: 1 addition & 0 deletions src/rhsm/https.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import logging
import ssl as _ssl
import sys

log = logging.getLogger(__name__)

Expand Down
2 changes: 2 additions & 0 deletions src/rhsmlib/facts/hwprobe.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
from rhsmlib.facts import cpuinfo
from rhsmlib.facts import collector

from typing import Optional

log = logging.getLogger(__name__)

# There is no python3 version of python-ethtool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def _do_command(self):
log.exception(err)
if getattr(self.options, 'list', None):
log.error("Error: Unable to retrieve {attr} from server: {err}".format(attr=self.attr, err=err))
mapped_message: str = ExceptionMapper().get_message(re)
mapped_message: str = ExceptionMapper().get_message(err)
system_exit(os.EX_SOFTWARE, mapped_message)
else:
log.debug("Error: Unable to retrieve {attr} from server: {err}".format(attr=self.attr, err=err))
Expand Down
5 changes: 0 additions & 5 deletions src/subscription_manager/scripts/package_profile_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
import six
import sys

if six.PY2:
reload(sys)
sys.setdefaultencoding('utf-8')

import argparse

from subscription_manager.injectioninit import init_dep_injection
Expand Down
4 changes: 0 additions & 4 deletions src/subscription_manager/scripts/rct.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
# hack to allow bytes/strings to be interpolated w/ unicode values (gettext gives us bytes)
# Without this, for example, "Формат: %s\n" % u"foobar" will fail with UnicodeDecodeError
# See http://stackoverflow.com/a/29832646/6124862 for more details
import six
import sys
if six.PY2:
reload(sys)
sys.setdefaultencoding('utf-8')

from subscription_manager.i18n import configure_i18n, ugettext as _
configure_i18n()
Expand Down
4 changes: 0 additions & 4 deletions src/subscription_manager/scripts/rhsm_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
# hack to allow bytes/strings to be interpolated w/ unicode values (gettext gives us bytes)
# Without this, for example, "Формат: %s\n" % u"foobar" will fail with UnicodeDecodeError
# See http://stackoverflow.com/a/29832646/6124862 for more details
import six
import sys
if six.PY2:
reload(sys)
sys.setdefaultencoding('utf-8')
import os

from subscription_manager.i18n import configure_i18n, ugettext as _
Expand Down
4 changes: 0 additions & 4 deletions src/subscription_manager/scripts/rhsm_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
# hack to allow bytes/strings to be interpolated w/ unicode values (gettext gives us bytes)
# Without this, for example, "Формат: %s\n" % u"foobar" will fail with UnicodeDecodeError
# See http://stackoverflow.com/a/29832646/6124862 for more details
import six
import sys
if six.PY2:
reload(sys)
sys.setdefaultencoding('utf-8')

from rhsmlib.dbus import service_wrapper
from rhsmlib.dbus import objects
Expand Down
4 changes: 0 additions & 4 deletions src/subscription_manager/scripts/rhsmcertd_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
# Without this, for example, "Формат: %s\n" % u"foobar" will fail with UnicodeDecodeError
# See http://stackoverflow.com/a/29832646/6124862 for more details
import sys
import six
if six.PY2:
reload(sys)
sys.setdefaultencoding('utf-8')

import signal
import logging
Expand Down
4 changes: 0 additions & 4 deletions src/subscription_manager/scripts/subscription_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@
# hack to allow bytes/strings to be interpolated w/ unicode values (gettext gives us bytes)
# Without this, for example, "Формат: %s\n" % u"foobar" will fail with UnicodeDecodeError
# See http://stackoverflow.com/a/29832646/6124862 for more details
import six
import sys
if six.PY2:
reload(sys)
sys.setdefaultencoding('utf-8')
import os

# work around for https://bugzilla.redhat.com/show_bug.cgi?id=1402009
Expand Down
15 changes: 7 additions & 8 deletions src/syspurpose/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import os
import errno
import logging
import six

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -72,16 +71,16 @@ def create_file(path, contents):
def make_utf8(obj):
"""
Transforms the provided string into unicode if it is not already
Previously there was a logic which converted the input.
Now the six.PY2 is never true and we can directly return the input.
It should be removed with other Python 2 utils.
:param obj: the string to decode
:return: the unicode format of the string
"""
if six.PY3:
return obj
elif obj is not None and isinstance(obj, str) and not isinstance(obj, unicode):
obj = obj.decode('utf-8')
return obj
else:
return obj
return obj


def write_to_file_utf8(file, data):
Expand Down
6 changes: 0 additions & 6 deletions test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
from __future__ import print_function, division, absolute_import


import six
from . import rhsm_display
rhsm_display.set_display()

if six.PY2:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
14 changes: 7 additions & 7 deletions test/syspurpose/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,16 @@ def __exit__(self, exc_type, exc_value, traceback):
def make_utf8(obj):
"""
Transforms the provided string into unicode if it is not already
Previously there was a logic which converted the input.
Now the six.PY2 is never true and we can directly return the input.
It should be removed with other Python 2 utils.
:param obj: the string to decode
:return: the unicode format of the string
"""
if six.PY3:
return obj
elif obj is not None and isinstance(obj, str) and not isinstance(obj, unicode):
obj = obj.decode('utf-8')
return obj
else:
return obj
return obj


def write_to_file_utf8(file, data):
Expand Down

0 comments on commit 3095f59

Please sign in to comment.