reimplemented-operator (FURB118)
Derived from the refurb linter.
Fix is sometimes available.
This rule is unstable and in preview. The --preview
flag is required for use.
What it does
Checks for lambda expressions and function definitions that can be replaced with a function from
the operator
module.
Why is this bad?
The operator
module provides functions that implement the same functionality as the
corresponding operators. For example, operator.add
is equivalent to lambda x, y: x + y
.
Using the functions from the operator
module is more concise and communicates the intent of
the code more clearly.
Example
Use instead:
Fix safety
This fix is usually safe, but if the lambda is called with keyword arguments, e.g.,
add = lambda x, y: x + y; add(x=1, y=2)
, replacing the lambda with an operator function, e.g.,
operator.add
, will cause the call to raise a TypeError
, as functions in operator
do not allow
keyword arguments.