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

False positive no-member with @contextmanager decorated generators #2567

Closed
ZeeD opened this issue Oct 22, 2018 · 14 comments · Fixed by #8054
Closed

False positive no-member with @contextmanager decorated generators #2567

ZeeD opened this issue Oct 22, 2018 · 14 comments · Fixed by #8054
Assignees
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code
Milestone

Comments

@ZeeD
Copy link

ZeeD commented Oct 22, 2018

I have this little snippet:

import contextlib

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

cm = context_manager()
cm.__enter__()
cm.__exit__(None, None, None)

pylint signals the "direct" access to the __enter__ and __exit__ callables as an error:

contextmanaged.py:13:0: E1101: Generator 'generator' has no '__enter__' member (no-member)
contextmanaged.py:14:0: E1101: Generator 'generator' has no '__exit__' member (no-member)

while "uncommon" at least, I think it should not be marked as an error.

> pylint --version
pylint 2.1.1
astroid 2.0.4
Python 3.6.3 (v3.6.3:2c5fed8, Oct  3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
@ZeeD ZeeD changed the title false positive on E1101 with contextmanager decorated generators false positive on E1101 with @contextmanager decorated generators Oct 22, 2018
@PCManticore
Copy link
Contributor

Thanks for the report, definitely a bug.

@PCManticore PCManticore added the Good first issue Friendly and approachable by new contributors label Nov 6, 2018
@Vihrastik
Copy link

Vihrastik commented Jul 30, 2019

And for

import contextlib

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

with context_manager:
    pass

have an error

[E1129 not-context-manager] Context manager 'context_manager' doesn't implement __enter__ and __exit__.

@PCManticore
Copy link
Contributor

PCManticore commented Jul 30, 2019

@Vihrastik You need to call context_manager for that to be valid, e.g.:

import contextlib

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

with context_manager(): # notice the function call
    pass

@Vihrastik
Copy link

Vihrastik commented Jul 30, 2019

@Vihrastik You need to call context_manager for that to be valid, e.g.:

import contextlib

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

with context_manager(): # notice the function call
    pass

no, i just forgot to write it in the example of code. this error is active with call.

@gyermolenko
Copy link
Contributor

Hi. I wanted to work on this issue if nobody minds.

Regarding snippet

import contextlib

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

with context_manager():
    pass

which @Vihrastik reports to [E1129 not-context-manager] - I did not reproduce that with pylint 2.3.1 / astroid 2.2.5.

However when I created local env with pylint from master (2.4.0.dev0) / astroid 2.2.5 I receive
error from #3022 about posonlyargs. Installing astroid from master fixes the issue.

gyermolenko added a commit to gyermolenko/pylint that referenced this issue Aug 4, 2019
Fix (no-member) false-positives for generators created with
contextmanager (pylint-dev#2567)
@PCManticore
Copy link
Contributor

Hi @gyermolenko Feel free to tackle this issue! Regarding your issue with posonlyargs, pylint from master needs the up to date astroid.

gyermolenko added a commit to gyermolenko/pylint that referenced this issue Aug 12, 2019
Fix (no-member) false-positives for generators created with
contextmanager (pylint-dev#2567)
gyermolenko added a commit to gyermolenko/pylint that referenced this issue Aug 12, 2019
Fix (no-member) false-positives for generators created with
contextmanager (pylint-dev#2567)
@Pierre-Sassoulas Pierre-Sassoulas added the Help wanted 🙏 Outside help would be appreciated, good for new contributors label Mar 2, 2021
@Pierre-Sassoulas Pierre-Sassoulas added False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation and removed Bug 🪲 labels Jul 4, 2022
@Pierre-Sassoulas Pierre-Sassoulas changed the title false positive on E1101 with @contextmanager decorated generators False positive no-member with @contextmanager decorated generators Jul 4, 2022
@clavedeluna
Copy link
Collaborator

@Vihrastik You need to call context_manager for that to be valid, e.g.:

import contextlib

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

with context_manager(): # notice the function call
    pass

no, i just forgot to write it in the example of code. this error is active with call.

I'm not able to reproduce this second issue, but I am able to reproduce original issue raised at the top.

@clavedeluna
Copy link
Collaborator

@DanielNoord I thought maybe the changes in pylint-dev/astroid#1810 would fix the initial FP, but it's still present. What else needs to happen in astroid?

@DanielNoord
Copy link
Collaborator

Have you checked out main? We haven't released a version with that fix in it.

@Pierre-Sassoulas
Copy link
Member

There's two PR left for astroid 2.13.0, you can follow progress here: https://github.com/PyCQA/astroid/milestone/40

@clavedeluna
Copy link
Collaborator

Score! Testing this out on astroid main solves this issue. Will update labels and hopefully can be closed once 2.13.0 is out.

@clavedeluna clavedeluna added Needs astroid update Needs an astroid update (probably a release too) before being mergable and removed Help wanted 🙏 Outside help would be appreciated, good for new contributors Good first issue Friendly and approachable by new contributors Hacktoberfest labels Nov 30, 2022
@clavedeluna clavedeluna removed the Needs PR This issue is accepted, sufficiently specified and now needs an implementation label Nov 30, 2022
@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.15.8, 2.16.0 Nov 30, 2022
@Pierre-Sassoulas
Copy link
Member

@clavedeluna 2.13.2 is out, the pylint PR is #8030

@Pierre-Sassoulas Pierre-Sassoulas removed the Needs astroid update Needs an astroid update (probably a release too) before being mergable label Jan 9, 2023
@Pierre-Sassoulas Pierre-Sassoulas self-assigned this Jan 12, 2023
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jan 12, 2023
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jan 13, 2023
Pierre-Sassoulas added a commit to Pierre-Sassoulas/pylint that referenced this issue Jan 13, 2023
@socketpair
Copy link

Finally, in what versions of Pylint / Astroid it is fixed ? I'm using Fedora 38 and this is triggered in spite of relatively new versions.

@Pierre-Sassoulas
Copy link
Member

The milestone is 2.16.0 here, please open a new issue if there was a regression :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment