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

fales positive: unnecessary-dunder-call (__aiter__, __anext__) #7529

Closed
belm0 opened this issue Sep 26, 2022 · 6 comments · Fixed by #7549
Closed

fales positive: unnecessary-dunder-call (__aiter__, __anext__) #7529

belm0 opened this issue Sep 26, 2022 · 6 comments · Fixed by #7549
Assignees
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@belm0
Copy link
Contributor

belm0 commented Sep 26, 2022

Bug description

As of recent pylint versions, I'm seeing the errors below, even though it's suggesting the wrong builtin (iter() rather than aiter()), and even though I'm on Python 3.8 (which doesn't have aiter() and anext() builtins).

Configuration

No response

Command used

pylint

Pylint output

C2801: Unnecessarily calls dunder method __aiter__. Use iter built-in function. (unnecessary-dunder-call)
C2801: Unnecessarily calls dunder method __anext__. Use next built-in function. (unnecessary-dunder-call)

Expected behavior

no errors

Pylint version

pylint 2.15.3
astroid 2.12.10
Python 3.8.12 (default, Sep 23 2021, 08:42:37)
[Clang 12.0.0 (clang-1200.0.32.29)]

OS / Environment

No response

Additional dependencies

No response

@belm0 belm0 added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Sep 26, 2022
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code Needs specification 🔐 Accepted as a potential improvement, and needs to specify edge cases, message names, etc. and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Sep 26, 2022
@DanielNoord
Copy link
Collaborator

@jpy-git Do you happen to know anything about this? Or would you want to take a look at this?

@belm0
Copy link
Contributor Author

belm0 commented Sep 30, 2022

  1. fix entries in DUNDER_METHODS. "Use {iter,next}" --> "Use {aiter,anext}"

https://github.com/PyCQA/pylint/blob/ec85e2988679a28dbe11f46d60056b4dd569c44b/pylint/checkers/dunder_methods.py#L108-L109

  1. this checker needs to have awareness of the Python version. The brute force for this case would be to declare the set of builtins requiring >= 3.10, and add it to the checker condition.
NEEDS_PYTHON_3_10 = {'__aiter__', '__anext__'}

(If there is no time to do this for the next release, at least disable these entries in DUNDER_METHODS.)

@DanielNoord DanielNoord self-assigned this Sep 30, 2022
@DanielNoord
Copy link
Collaborator

  1. fix entries in DUNDER_METHODS. "Use {iter,next}" --> "Use {aiter,anext}"

https://github.com/PyCQA/pylint/blob/ec85e2988679a28dbe11f46d60056b4dd569c44b/pylint/checkers/dunder_methods.py#L108-L109

This is an easy fix. Would you want to open a PR for this? You've done all the work already!

  1. this checker needs to have awareness of the Python version. The brute force for this case would be to declare the set of builtins requiring >= 3.10, and add it to the checker condition.
NEEDS_PYTHON_3_10 = {'__aiter__', '__anext__'}

(If there is no time to do this for the next release, at least disable these entries in DUNDER_METHODS.)

I'm not sure I understand this. If you're using __aiter__ then aiter() is available as well right?

@belm0
Copy link
Contributor Author

belm0 commented Sep 30, 2022

I'm not sure I understand this. If you're using __aiter__ then aiter() is available as well right?

No-- this is the misunderstanding of this checker. __aiter__ / __anext__ came with async iterators in Python 3.5, while aiter() / anext() convenience builtins didn't appear until Python 3.10.

@belm0
Copy link
Contributor Author

belm0 commented Sep 30, 2022

some reasons to consider dropping __aiter__ / __anext__ from the table for now:

  1. the current pylint guidance is potentially harmful (suggesting the wrong, non-async builtins)
  2. even if (1) is trivially corrected, since the builtins were only introduced in a recent Python version, following the pylint guidance will often not be the right thing to do. For example, when the __aiter__ reference is in a library that's trying to be compatible with older, but not yet EOL'd Python versions. (As most libraries should strive to do.) So this guidance is only suited for an application context -- and pylint can't discern that.

As for fixing (rather than removing), I could contribute the change, but it's probably 1-2 hrs for me to work out, including proper tests-- not sure when I can get to it.

@DanielNoord
Copy link
Collaborator

I'll work on a fix today which uses py-version, that option allows you to specify which Python version you support.

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.15.4 milestone Oct 1, 2022
@Pierre-Sassoulas Pierre-Sassoulas removed the Needs specification 🔐 Accepted as a potential improvement, and needs to specify edge cases, message names, etc. label Oct 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants