Skip to content

handle no-member false positive for generators #7505

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whatsnew/fragments/2567.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed a false positive case when a generator object received ``no-member`` error.

Closes #2567
8 changes: 8 additions & 0 deletions pylint/checkers/typecheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,14 @@ def _emit_no_member(
return False
except astroid.NotFoundError:
pass
if isinstance(owner, astroid.bases.Generator) and node.attrname in {
"__enter__",
"__exit__",
}:
# Avoid false positive on generators.
# See https://github.com/PyCQA/pylint/issues/2567
return False

if owner_name and node.attrname.startswith("_" + owner_name):
# Test if an attribute has been mangled ('private' attribute)
unmangled_name = node.attrname.split("_" + owner_name)[-1]
Expand Down
15 changes: 15 additions & 0 deletions tests/functional/n/no/no_member_generators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Test no-member for generators
"""
# pylint: disable=missing-docstring, too-few-public-methods
import contextlib

@contextlib.contextmanager
def context_manager():
try:
yield
finally:
pass

cm = context_manager()
cm.__enter__()
cm.__exit__(None, None, None)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no corresponding .txt file because no pylint errors or warnings should be in the output.