From 15dd079792ce8e6b37856450f9e4a928f11b3549 Mon Sep 17 00:00:00 2001 From: Dani Alcala <112832187+clavedeluna@users.noreply.github.com> Date: Thu, 20 Oct 2022 11:35:53 -0300 Subject: [PATCH] Remove __index__ from unnecessary-dunder-call check (#7650) --- doc/whatsnew/fragments/6795.bugfix | 3 +++ pylint/checkers/dunder_methods.py | 4 ++-- tests/functional/u/unnecessary/unnecessary_dunder_call.py | 6 ++++++ 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 doc/whatsnew/fragments/6795.bugfix diff --git a/doc/whatsnew/fragments/6795.bugfix b/doc/whatsnew/fragments/6795.bugfix new file mode 100644 index 0000000000..20a29da356 --- /dev/null +++ b/doc/whatsnew/fragments/6795.bugfix @@ -0,0 +1,3 @@ +Remove ``__index__`` dunder method call from ``unnecessary-dunder-call`` check. + +Closes #6795 diff --git a/pylint/checkers/dunder_methods.py b/pylint/checkers/dunder_methods.py index 1b61be7d4f..2e5e54a57c 100644 --- a/pylint/checkers/dunder_methods.py +++ b/pylint/checkers/dunder_methods.py @@ -100,7 +100,6 @@ "__complex__": "Use complex built-in function", "__int__": "Use int built-in function", "__float__": "Use float built-in function", - "__index__": "Use index method", "__round__": "Use round built-in function", "__trunc__": "Use math.trunc function", "__floor__": "Use math.floor function", @@ -125,7 +124,8 @@ class DunderCallChecker(BaseChecker): We exclude __new__, __subclasses__, __init_subclass__, __set_name__, __class_getitem__, __missing__, __exit__, __await__, __aexit__, __getnewargs_ex__, __getnewargs__, __getstate__, - __setstate__, __reduce__, __reduce_ex__ + __setstate__, __reduce__, __reduce_ex__, + and __index__ (see https://github.com/PyCQA/pylint/issues/6795) since these either have no alternative method of being called or have a genuine use case for being called manually. diff --git a/tests/functional/u/unnecessary/unnecessary_dunder_call.py b/tests/functional/u/unnecessary/unnecessary_dunder_call.py index cd1f21286d..18e4ef855c 100644 --- a/tests/functional/u/unnecessary/unnecessary_dunder_call.py +++ b/tests/functional/u/unnecessary/unnecessary_dunder_call.py @@ -122,3 +122,9 @@ def get_first_subclass(cls): # since we can't apply alternate operators/functions here. a = [1, 2, 3] assert super(type(a), a).__str__() == "[1, 2, 3]" + +class MyString(str): + """Custom str implementation""" + def rjust(self, width, fillchar= ' '): + """Acceptable call to __index__""" + width = width.__index__()