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

R1732(consider-using-with): Regression on contextlib.ExitStack #4654

Closed
romainletendart opened this issue Jul 1, 2021 · 4 comments · Fixed by #4665
Closed

R1732(consider-using-with): Regression on contextlib.ExitStack #4654

romainletendart opened this issue Jul 1, 2021 · 4 comments · Fixed by #4665
Assignees
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code

Comments

@romainletendart
Copy link

Steps to reproduce

Using exit_stack.enter_context() within a with contextlib.ExitStack() as exit_stack block, I think we shouldn't have any R1732(consider-using-with) errors. At least, no errors were raised under pylint==2.8.3.

Given a file test.py:

import contextlib


def get_serial_number():
    with contextlib.ExitStack() as stack:
        location = stack.enter_context(open('/sys/firmware/devicetree/base/hwid,location', 'r'))
        year = stack.enter_context(open('/sys/firmware/devicetree/base/hwid,year', 'r'))
        week = stack.enter_context(open('/sys/firmware/devicetree/base/hwid,week', 'r'))
        genid = stack.enter_context(open('/sys/firmware/devicetree/base/hwid,genid', 'r'))
        sn = stack.enter_context(open('/sys/firmware/devicetree/base/hwid,sn', 'r'))

        return (
            location.read().rstrip('\x00')
            + year.read()[2:].rstrip('\x00')
            + week.read().rstrip('\x00')
            + genid.read().rstrip('\x00')
            + sn.read().rstrip('\x00')
        )

Current behavior

Result of pylint test.py:

************* Module test
test.py:6: [R1732(consider-using-with), get_serial_number] Consider using 'with' for resource-allocating operations
test.py:7: [R1732(consider-using-with), get_serial_number] Consider using 'with' for resource-allocating operations
test.py:8: [R1732(consider-using-with), get_serial_number] Consider using 'with' for resource-allocating operations
test.py:9: [R1732(consider-using-with), get_serial_number] Consider using 'with' for resource-allocating operations
test.py:10: [R1732(consider-using-with), get_serial_number] Consider using 'with' for resource-allocating operations

-------------------------------------------------------------------
Your code has been rated at 4.44/10 (previous run: -2.50/10, +6.94)

Expected behavior

--------------------------------------------------------------------
Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

pylint --version output

pylint 3.0.0-a4
astroid 2.6.2
Python 3.8.9 (default, Apr 19 2021, 08:40:21) 
[GCC 10.2.0]

The issue is the same with:

pylint 2.9.3
astroid 2.6.2
Python 3.8.9 (default, Apr 19 2021, 08:40:21) 
[GCC 10.2.0]

However, no issues with:

pylint 2.8.3
astroid 2.5.6
Python 3.8.9 (default, Apr 19 2021, 08:40:21) 
[GCC 10.2.0]
@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Jul 1, 2021
@Pierre-Sassoulas
Copy link
Member

Thank you for opening the issue, I agree with you you should not have a warning here.

@DudeNr33
Copy link
Collaborator

DudeNr33 commented Jul 1, 2021

I will look into this. I was not aware of the contextlib.ExitStack.enter_context() method.
This should be probably treated in a special way. I wonder if there are any other methods in the standard library that do similar things and should be considered as well.

@cdce8p
Copy link
Member

cdce8p commented Jul 2, 2021

I wonder if there are any other methods in the standard library that do similar things and should be considered as well.

Probably contextlib.AsyncExitStack.enter_async_context
https://docs.python.org/3/library/contextlib.html#contextlib.AsyncExitStack.enter_async_context

@DudeNr33
Copy link
Collaborator

DudeNr33 commented Jul 3, 2021

Thanks @cdce8p.
This led me to realize that the whole consider-using-with check does not check asnychronous context managers at all.
A quick search over the Github repo of CPython shows that the standard library only has async context managers in form of the various locks of asyncio.locks and in asyncio.events.AbstractServer.

DudeNr33 added a commit to DudeNr33/pylint that referenced this issue Jul 3, 2021
Pierre-Sassoulas pushed a commit that referenced this issue Jul 4, 2021
…itStack`` (#4665)

* Fix false-positive ``consider-using-with`` when using ``contextlib.ExitStack``

* Add ``whatsnew`` entry for #4654

* Python 3.6 needs special treatment - ``ExitStack`` did not inherit from ``_BaseExitStack`` until Python 3.7
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.

4 participants