Skip to content

(🐞) FURB118 (reimplemented-operator) false positive when reference in the item position #10898

Closed
@KotlinIsland

Description

@KotlinIsland
b = 1
def f(a):  # Use `operator.itemgetter(b)` instead of defining a function
    return a[b]
b = 2
f([1, 2, 3])  # 3

playground
but if we follow that direction:

b = 1
f = operator.itemgetter(b)
b = 2
f([1, 2, 3])  # 2

Original issue:

class A:
    def __init__(self):
        self.foo = 1
    
    @property
    def f(self) -> object:  # Use `operator.itemgetter(self.foo)` instead of defining a function
        return self[self.foo]

playground

This function references an input in the value position, making it impossible to rewrite as an attribute

class A:
    def __init__(self):
        self.foo = 1

    f = property(operator.itemgetter(self.foo))

And if we define it in the __init__ then it would always use the intial value of foo:

class A:
    def __init__(self):
        self.foo = 1
        self.f = property(operator.itemgetter(self.foo))

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions