sourcery icon indicating copy to clipboard operation
sourcery copied to clipboard

Refactor using with block misses second open()/close() set

Open dericke opened this issue 4 years ago • 1 comments

Issue description or question

In the following function, Sourcery only suggests to refactor the outfile open()/close() set to a with block, but doesn't make any suggestions about the infile open()/close():

def in_out(self, infile, outfile):
    infile = open(infile, "rb")
    outfile = open(outfile, "w")
    for l in infile.readlines():
        outfile.write(l)
    infile.close()
    outfile.close()

However, they can both be combined into a single with block:

def in_out(self, infile, outfile):
    with open(infile, "rb") as infile, open(outfile, "w") as outfile:
        for l in infile.readlines():
            outfile.write(l)

Sourcery Version

v0.8.7

Code editor or IDE name and version

VS Code 1.52.1

OS name and

Ubuntu Linux 20.04.1

dericke avatar Jan 20 '21 22:01 dericke

Yeah would be nice if it spotted both - it does if the closes are swapped.

Hellebore avatar Jan 21 '21 08:01 Hellebore