Skip to content

Commit

Permalink
Fixed errors newly exposed by pylint 2.4.0
Browse files Browse the repository at this point in the history
Newest Pylint introduced additional checks [1]:

- import-outside-toplevel [2]

> This check warns when modules are imported from places other
than a module toplevel, e.g. inside a function or a class.

- no-else-continue [3]

> These checks highlight unnecessary else and elif blocks after
break and continue statements.

- unnecessary-comprehension [4]

> This check is emitted when pylint finds list-, set- or
dict-comprehensions, that are unnecessary and can be rewritten
with the list-, set- or dict-constructors.

[1] https://github.com/PyCQA/pylint/blob/pylint-2.4.0/doc/whatsnew/2.4.rst
[2] pylint-dev/pylint#3067
[3] pylint-dev/pylint#2327
[4] pylint-dev/pylint#2905

Fixes: https://pagure.io/freeipa/issue/8077
Signed-off-by: Stanislav Levin <slev@altlinux.org>
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Christian Heimes <cheimes@redhat.com>
  • Loading branch information
stanislavlevin authored and flo-renaud committed Sep 30, 2019
1 parent 6c5e72a commit 1248050
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
13 changes: 13 additions & 0 deletions ipalib/__init__.py
Expand Up @@ -886,8 +886,10 @@ class my_command(Command):
def _enable_warnings(error=False):
"""Enable additional warnings during development
"""
# pylint: disable=import-outside-toplevel
import ctypes
import warnings
# pylint: enable=import-outside-toplevel

# get reference to Py_BytesWarningFlag from Python CAPI
byteswarnings = ctypes.c_int.in_dll( # pylint: disable=no-member
Expand Down Expand Up @@ -937,23 +939,34 @@ class API(plugable.API):
def packages(self):
if self.env.in_server:
# pylint: disable=import-error,ipa-forbidden-import
# pylint: disable=import-outside-toplevel
import ipaserver.plugins
# pylint: enable=import-error,ipa-forbidden-import
# pylint: enable=import-outside-toplevel
result = (
ipaserver.plugins,
)
else:
# disables immediately after an else clause
# do not work properly:
# https://github.com/PyCQA/pylint/issues/872
# Thus, below line was added as a workaround
result = None
# pylint: disable=import-outside-toplevel
import ipaclient.remote_plugins
import ipaclient.plugins
# pylint: enable=import-outside-toplevel
result = (
ipaclient.remote_plugins.get_package(self),
ipaclient.plugins,
)

if self.env.context in ('installer', 'updates'):
# pylint: disable=import-error,ipa-forbidden-import
# pylint: disable=import-outside-toplevel
import ipaserver.install.plugins
# pylint: enable=import-error,ipa-forbidden-import
# pylint: enable=import-outside-toplevel
result += (ipaserver.install.plugins,)

return result
Expand Down
11 changes: 5 additions & 6 deletions ipalib/frontend.py
Expand Up @@ -575,7 +575,7 @@ def __attributes_2_entry(self, kw):
if self.params[name].attribute and name in kw:
value = kw[name]
if isinstance(value, tuple):
yield (name, [v for v in value])
yield (name, list(value))
else:
yield (name, kw[name])

Expand Down Expand Up @@ -1069,15 +1069,14 @@ def output_for_cli(self, textui, output, *args, **options):

if o == 'value':
continue
elif o.lower() == 'count' and result == 0:
if o.lower() == 'count' and result == 0:
rv = 1
elif o.lower() == 'failed':
if entry_count(result) == 0:
# Don't display an empty failed list
continue
else:
# Return an error to the shell
rv = 1
# Return an error to the shell
rv = 1
if isinstance(outp, ListOfEntries):
textui.print_entries(result, order, labels, flags, print_all)
elif isinstance(result, (tuple, list)):
Expand Down Expand Up @@ -1299,7 +1298,7 @@ def __json__(self):
)
if self.primary_key:
json_dict['primary_key'] = self.primary_key.name
json_dict['methods'] = [m for m in self.methods]
json_dict['methods'] = list(self.methods)
return json_dict


Expand Down
16 changes: 8 additions & 8 deletions ipalib/parameters.py
Expand Up @@ -559,7 +559,7 @@ def __init__(self, name, *rules, **kw):
# Check that all the rules are callable
self.class_rules = tuple(class_rules)
self.rules = rules
if self.query:
if self.query: # pylint: disable=using-constant-test
# by definition a query enforces no class or parameter rules
self.all_rules = ()
else:
Expand Down Expand Up @@ -761,10 +761,10 @@ def normalize(self, value):
:param value: A proposed value for this parameter.
"""
if self.multivalue:
if self.multivalue: # pylint: disable=using-constant-test
if type(value) not in (tuple, list):
value = (value,)
if self.multivalue:
if self.multivalue: # pylint: disable=using-constant-test
return tuple(
self._normalize_scalar(v) for v in value
)
Expand Down Expand Up @@ -840,7 +840,7 @@ def convert(value):

if _is_null(value):
return
if self.multivalue:
if self.multivalue: # pylint: disable=using-constant-test
if type(value) not in (tuple, list):
value = (value,)
values = tuple(
Expand Down Expand Up @@ -872,10 +872,10 @@ def validate(self, value, supplied=None):
if self.required or (supplied and 'nonempty' in self.flags):
raise RequirementError(name=self.name)
return
if self.deprecated:
if self.deprecated: # pylint: disable=using-constant-test
raise ValidationError(name=self.get_param_name(),
error=_('this option is deprecated'))
if self.multivalue:
if self.multivalue: # pylint: disable=using-constant-test
if type(value) is not tuple:
raise TypeError(
TYPE_ERROR % ('value', tuple, value, type(value))
Expand Down Expand Up @@ -969,8 +969,8 @@ def __json__(self):
for a, k, _d in self.kwargs:
if k in (callable, DefaultFrom):
continue
elif isinstance(getattr(self, a), frozenset):
json_dict[a] = [k for k in getattr(self, a, [])]
if isinstance(getattr(self, a), frozenset):
json_dict[a] = list(getattr(self, a, []))
else:
val = getattr(self, a, '')
if val is None:
Expand Down
9 changes: 5 additions & 4 deletions ipalib/plugable.py
Expand Up @@ -454,7 +454,7 @@ def bootstrap(self, parser=None, **overrides):
if root_logger.handlers or self.env.validate_api:
return

if self.env.debug:
if self.env.debug: # pylint: disable=using-constant-test
level = logging.DEBUG
else:
level = logging.INFO
Expand All @@ -478,7 +478,7 @@ def bootstrap(self, parser=None, **overrides):

# Add stderr handler:
level = logging.INFO
if self.env.debug:
if self.env.debug: # pylint: disable=using-constant-test
level = logging.DEBUG
else:
if self.env.context == 'cli':
Expand Down Expand Up @@ -510,7 +510,7 @@ def bootstrap(self, parser=None, **overrides):
return

level = logging.INFO
if self.env.debug:
if self.env.debug: # pylint: disable=using-constant-test
level = logging.DEBUG
try:
handler = logging.FileHandler(self.env.log)
Expand Down Expand Up @@ -654,7 +654,8 @@ def add_package(self, package):
logger.debug("skipping plugin module %s: %s", name, e.reason)
continue
except Exception as e:
if self.env.startup_traceback:
tb = self.env.startup_traceback
if tb: # pylint: disable=using-constant-test
logger.exception("could not load plugin module %s", name)
raise

Expand Down

0 comments on commit 1248050

Please sign in to comment.