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 #9162

Open
fmigneault opened this issue Oct 18, 2023 · 0 comments
Open

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

fmigneault opened this issue Oct 18, 2023 · 0 comments
Labels
Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling

Comments

@fmigneault
Copy link

fmigneault commented Oct 18, 2023

Bug description

This is extremely similar to #4654, but is somehow still flagged in my case.

I can see 2 possible distinctions.

  1. I use stack.enter_context(tempfile.TemporaryDirectory()) and stack.enter_context(tempfile.NamedTemporaryFile(...)) instead of stack.enter_context(open(...)) as in the original issue.

  2. The with contextlib.ExitStack block is not directly in the same context/scope as where stack.enter_context is called.

For example:

def util_func(stack):
    tmpdir = stack.enter_context(tempfile.TemporaryDirectory())
    # do stuff
    return tmpdir

def test_something():
    with contextlib.ExitStack() as stack:
        tmpdir = util_func(stack)
        stack.enter_context(tempfile.NamedTemporaryFile(dir=tmpdir))
        # ...

Configuration

see https://github.com/crim-ca/weaver/blob/master/.pylintrc

Command used

pylint \
    --load-plugins pylint_quotes \
    --rcfile=./.pylintrc \
    --reports y \
    ./weaver ./tests

Pylint output

************* Module tests.functional.test_builtin
tests/functional/test_builtin.py:93:38: R1732: Consider using 'with' for resource-allocating operations (consider-using-with)
tests/functional/test_builtin.py:401:38: R1732: Consider using 'with' for resource-allocating operations (consider-using-with)
tests/functional/test_builtin.py:403:12: R1732: Consider using 'with' for resource-allocating operations (consider-using-with)

Expected behavior

No error for stack.enter_context(tempfile.TemporaryDirectory()) and stack.enter_context(tempfile.NamedTemporaryFile(...)).

Pylint version

Name: pylint
Version: 2.17.4
Summary: python code static checker
Home-page: 
Author: 
Author-email: Python Code Quality Authority <code-quality@python.org>
License: GPL-2.0-or-later
Location: /home/francis/dev/miniconda/envs/weaver-py310/lib/python3.10/site-packages
Requires: astroid, dill, isort, mccabe, platformdirs, tomli, tomlkit
Required-by: pylint-quotes

OS / Environment

Ubuntu 20.04.6

Additional dependencies

No response

@fmigneault fmigneault added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling
Projects
None yet
Development

No branches or pull requests

1 participant