pylint: Fix unnecessary-lambda-assignment

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 ‘<lambda>’. 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 <slev@altlinux.org>
Reviewed-By: Stanislav Levin <slev@altlinux.org>
This commit is contained in:
Stanislav Levin 2022-12-19 19:01:45 +03:00 committed by Florence Blanc-Renaud
parent 24db4dc876
commit bf3083c376
6 changed files with 37 additions and 18 deletions

View File

@ -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')

View File

@ -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

View File

@ -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 = (

View File

@ -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

View File

@ -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

View File

@ -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,