Skip to content

False positive consider-using-with on a with statement #4676

@adam-azarchs

Description

@adam-azarchs

Steps to reproduce

Given a file a.py:

# pylint: disable=missing-module-docstring,missing-function-docstring
# pylint: enable=consider-using-with


import contextlib


def read_files(file1, file2):
    with open(file1) as input_file1, (
        open(file2) if file2 else contextlib.nullcontext()
    ) as input_file2:
        if input_file2:
            return input_file1.read(), input_file2.read()
        return input_file1.read()

Current behavior

t.py:10:8: R1732: Consider using 'with' for resource-allocating operations (consider-using-with)

-->

Expected behavior

No warning. I'm already using a with statement.

pylint --version output

Result of pylint --version output:

pylint 2.9.1
astroid 2.6.2
Python 3.8.10 (default, Jun  4 2021, 15:09:15)
[GCC 7.5.0]

Metadata

Metadata

Assignees

Labels

False Positive 🦟A message is emitted but nothing is wrong with the code

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions