From c8d4c9bb424e40211eebff46da118be4fc2a7f53 Mon Sep 17 00:00:00 2001 From: clavedeluna Date: Wed, 19 Oct 2022 16:10:19 -0300 Subject: [PATCH 1/2] remove __index__ from unnecessary-dunder-call check --- 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..2a7b8cf495 --- /dev/null +++ b/doc/whatsnew/fragments/6795.bugfix @@ -0,0 +1,3 @@ +Remove ``__index__`` dunder method call from ``unnecessary-dunder-call`` check. + +Closes #6795 \ No newline at end of file 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__() From 3e7fb050b6bfe97ad2c71a2d604d2702a7dfaf37 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 19 Oct 2022 19:14:32 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- doc/whatsnew/fragments/6795.bugfix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whatsnew/fragments/6795.bugfix b/doc/whatsnew/fragments/6795.bugfix index 2a7b8cf495..20a29da356 100644 --- a/doc/whatsnew/fragments/6795.bugfix +++ b/doc/whatsnew/fragments/6795.bugfix @@ -1,3 +1,3 @@ Remove ``__index__`` dunder method call from ``unnecessary-dunder-call`` check. -Closes #6795 \ No newline at end of file +Closes #6795