From bf3083c3760797c466d4010d812755b9fc5aa2c9 Mon Sep 17 00:00:00 2001 From: Stanislav Levin Date: Mon, 19 Dec 2022 19:01:45 +0300 Subject: [PATCH] pylint: Fix unnecessary-lambda-assignment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://pylint.pycqa.org/en/latest/user_guide/messages/convention/unnecessary-lambda-assignment.html: > Used when a lambda expression is assigned to variable rather than defining a standard function with the "def" keyword. https://peps.python.org/pep-0008/#programming-recommendations: > Always use a def statement instead of an assignment statement that binds a lambda expression directly to an identifier: def f(x): return 2*x f = lambda x: 2*x The first form means that the name of the resulting function object is specifically ‘f’ instead of the generic ‘’. This is more useful for tracebacks and string representations in general. The use of the assignment statement eliminates the sole benefit a lambda expression can offer over an explicit def statement (i.e. that it can be embedded inside a larger expression) Fixes: https://pagure.io/freeipa/issue/9278 Signed-off-by: Stanislav Levin Reviewed-By: Stanislav Levin --- ipapython/install/core.py | 14 +++++++++++--- ipaserver/plugins/idrange.py | 15 +++++++++------ ipatests/test_ipalib/test_frontend.py | 4 +++- ipatests/test_ipalib/test_parameters.py | 10 ++++++---- ipatests/test_util.py | 11 ++++++++--- pylintrc | 1 - 6 files changed, 37 insertions(+), 18 deletions(-) diff --git a/ipapython/install/core.py b/ipapython/install/core.py index 7f638914c..efdc1bbfa 100644 --- a/ipapython/install/core.py +++ b/ipapython/install/core.py @@ -415,7 +415,11 @@ class Configurable(six.with_metaclass(abc.ABCMeta, object)): def __runner(self, pending_state, running_state, exc_handler): self.__transition(pending_state, running_state) - step = lambda: next(self.__gen) + def step_next(): + return next(self.__gen) + + step = step_next + while True: try: step() @@ -440,9 +444,13 @@ class Configurable(six.with_metaclass(abc.ABCMeta, object)): yield except BaseException: exc_info = sys.exc_info() - step = lambda: self.__gen.throw(*exc_info) + + def step_throw(): + return self.__gen.throw(*exc_info) + + step = step_throw else: - step = lambda: next(self.__gen) + step = step_next def _handle_exception(self, exc_info): assert not hasattr(super(Configurable, self), '_handle_exception') diff --git a/ipaserver/plugins/idrange.py b/ipaserver/plugins/idrange.py index d5b184fcd..3eca90a5b 100644 --- a/ipaserver/plugins/idrange.py +++ b/ipaserver/plugins/idrange.py @@ -426,7 +426,8 @@ class idrange_add(LDAPCreate): def pre_callback(self, ldap, dn, entry_attrs, attrs_list, *keys, **options): assert isinstance(dn, DN) - is_set = lambda x: (x in entry_attrs) and (entry_attrs[x] is not None) + def is_set(x): + return entry_attrs.get(x) is not None # This needs to stay in options since there is no # ipanttrusteddomainname attribute in LDAP @@ -685,11 +686,13 @@ class idrange_mod(LDAPUpdate): '`ipa help idrange` for more information') ) - is_set = lambda x: (x in entry_attrs) and (entry_attrs[x] is not None) - in_updated_attrs = lambda x:\ - (x in entry_attrs and entry_attrs[x] is not None) or\ - (x not in entry_attrs and x in old_attrs - and old_attrs[x] is not None) + def is_set(x): + return entry_attrs.get(x) is not None + + def in_updated_attrs(x): + return is_set(x) or ( + x not in entry_attrs and old_attrs.get(x) is not None + ) # This needs to stay in options since there is no # ipanttrusteddomainname attribute in LDAP diff --git a/ipatests/test_ipalib/test_frontend.py b/ipatests/test_ipalib/test_frontend.py index ad2f67dbf..e9da505b4 100644 --- a/ipatests/test_ipalib/test_frontend.py +++ b/ipatests/test_ipalib/test_frontend.py @@ -211,7 +211,9 @@ class test_Command(ClassChecker): lambda arg: arg, 'default_from' ) - normalizer = lambda value: value.lower() + + def normalizer(value): + return value.lower() class example(self.cls): takes_options = ( diff --git a/ipatests/test_ipalib/test_parameters.py b/ipatests/test_ipalib/test_parameters.py index fd1819024..9feb613b5 100644 --- a/ipatests/test_ipalib/test_parameters.py +++ b/ipatests/test_ipalib/test_parameters.py @@ -73,8 +73,7 @@ class test_DefaultFrom(ClassChecker): o = self.cls(callback, *keys) assert read_only(o, 'callback') is callback assert read_only(o, 'keys') == keys - lam = lambda first, last: first[0] + last - o = self.cls(lam) + o = self.cls(lambda first, last: first[0] + last) assert read_only(o, 'keys') == ('first', 'last') # Test that TypeError is raised when callback isn't callable: @@ -108,7 +107,8 @@ class test_DefaultFrom(ClassChecker): o = self.cls(stuff, 'aye', 'bee', 'see') assert repr(o) == "DefaultFrom('aye', 'bee', 'see')" - cb = lambda first, last: first[0] + last + def cb(first, last): + return first[0] + last o = self.cls(cb) assert repr(o) == "DefaultFrom('first', 'last')" @@ -293,7 +293,9 @@ class test_Param(ClassChecker): ) # Test that default_from gets set: - call = lambda first, last: first[0] + last + def call(first, last): + return first[0] + last + o = self.cls('my_param', default_from=call) assert type(o.default_from) is parameters.DefaultFrom assert o.default_from.callback is call diff --git a/ipatests/test_util.py b/ipatests/test_util.py index 047e7ec43..3210da60d 100644 --- a/ipatests/test_util.py +++ b/ipatests/test_util.py @@ -81,7 +81,8 @@ class test_Fuzzy: assert inst.test is None assert isinstance(inst.re, pattern_type) - t = lambda other: other > 500 + def t(other): + return other > 500 inst = self.klass(test=t) assert inst.regex is None @@ -97,7 +98,9 @@ class test_Fuzzy: def test_repr(self): s = 'Fuzzy(%r, %r, %r)' - t = lambda other: 0.0 <= other <= 1.0 + + def t(other): + return 0.0 <= other <= 1.0 inst = self.klass() assert repr(inst) == s % (None, None, None) @@ -129,7 +132,9 @@ class test_Fuzzy: assert (self.klass(type=int) == 18) is True assert (self.klass(type=(int, str)) == '18') is True - t = lambda other: other.endswith('bar') + def t(other): + return other.endswith('bar') + assert (self.klass(test=t) == 'foobar') is True assert (self.klass(test=t, type=unicode) == b'foobar') is False assert (self.klass(test=t) == 'barfoo') is False diff --git a/pylintrc b/pylintrc index 3a09c610e..fe5e190e0 100644 --- a/pylintrc +++ b/pylintrc @@ -74,7 +74,6 @@ disable= super-init-not-called, undefined-loop-variable, unnecessary-lambda, - unnecessary-lambda-assignment, unused-argument, useless-else-on-loop, bad-continuation,