Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
KotlinIsland opened this issue Apr 12, 2024 · 4 comments · Fixed by #11270
Labels
bug Something isn't working fixes Related to suggested fixes for violations

Comments

@KotlinIsland
Copy link
Contributor

KotlinIsland commented Apr 12, 2024

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))
@KotlinIsland KotlinIsland changed the title (🐞) FURB118 (reimplemented-operator) false positive (🐞) FURB118 (reimplemented-operator) false positive when reference in the item position Apr 12, 2024
@MichaReiser MichaReiser added the fixes Related to suggested fixes for violations label Apr 12, 2024
@MichaReiser
Copy link
Member

Yeah, that's certainly not safe. We should either mark the fix as unsafe or "properly" fix (although that would likely require to replace all call-sites of f which I don't think we can do easily)

@MichaReiser MichaReiser added the bug Something isn't working label Apr 12, 2024
@KotlinIsland
Copy link
Contributor Author

Or detect if there is a reference in the item then don't try to fix it?

@DetachHead
Copy link

shouldn't it just not even report the error at all in that case, since it means there's no way to properly convert it to an itemgetter

@KotlinIsland
Copy link
Contributor Author

Could be a constant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants