-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
False positive Unnecessarily calls dunder method __index__ #6795
Comments
@jpy-git do you want to triage that one ? :) |
Now that I look at it, since Python 3.8, which is the version |
Brainstorming fix ideas. Would one fix potentially be that when a call to |
@clavedeluna, This issue with the current message is the The main question we need to answer is what is the preferred way to invoke There are three ways I've found:
@Pierre-Sassoulas, do you think this should this be ignored, reference operator.index(), or something else? |
I investigated a little and I'm wondering if it's not working as intended. @avylove I guess my question is, why would you use |
It's definitely not correct as it is, because the message instructs the user to use
|
All right, then the suggested function should be |
It makes sense to me. I just wasn't sure where you were on suggestions that required an import. But I think that's the best option. It gives the user an alternative to using the dunder method directly and they can always disable the check for that line if they don't want to use the suggestion. |
Yeah recommending to add an import is not ideal. I'm hesitating between that and allowing |
Let's just relax the check to allow |
Bug description
A little background. The
str
justify methods (rjust()
,ljust()
,center()
) takeSupportsIndex
types forwidth
rather than just straight integers, allowing you to use custom types like so.If you subclass
str
and re-implement these methods in a compatible manner, you usewidth.__index__()
instead of usingwidth
directly as an integer. With Pylint 2.14.0 this now raisesunnecessary-dunder-call
which seems intended for sequences, which have anindex()
method, rather thanSupportsIndex
types which don't.A simple example:
Configuration
No response
Command used
Pylint output
************* Module index_test Desktop/index_test.py:10:16: C2801: Unnecessarily calls dunder method __index__. Use index method. (unnecessary-dunder-call) ----------------------------------- Your code has been rated at 7.50/10
Expected behavior
Should be a clean run. That said, I understand this is a corner case and may be difficult to detect.
Pylint version
OS / Environment
Fedora 36
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: