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

[Bug]: Missing FURB151 (Path(x).touch()) when separating open() and close() #326

Open
2 tasks done
opk12 opened this issue Feb 8, 2024 · 2 comments
Open
2 tasks done
Assignees
Labels
bug Something isn't working

Comments

@opk12
Copy link

opk12 commented Feb 8, 2024

Has your issue already been fixed?

  • Have you checked to see if your issue still exists on the master branch? See the docs for instructions on how to setup a local build of Refurb.
  • Have you looked at the open/closed issues to see if anyone has already reported your issue?

The Bug

The following code:

#! /usr/bin/env python3
import os.path

path = "/tmp/tmpfile1"

if not os.path.exists(path):
    # Missing FURB151: No warning
    f = open(path, "w")
    f.close()

    # Warns as expected
    open(path, "w").close()

Does not emit the Path.touch() warning for lines 8-9. It only warns for line 12.

$ refurb --enable-all a.py 
a.py:6:8 [FURB141]: Replace `os.path.exists(x)` with `Path(x).exists()`
a.py:12:5 [FURB151]: Replace `open(x, "w").close()` with `Path(x).touch()`

Version Info

Refurb: v1.27.0
Mypy: v1.8.0

Python Version

Python 3.11.7

Config File

# N/A

Extra Info

None

@opk12 opk12 added the bug Something isn't working label Feb 8, 2024
@dosisod
Copy link
Owner

dosisod commented Feb 9, 2024

Thank you @opk12 for opening this! This would be a good feature to add.

Out of curiosity, is this example from code you've seen/wrote? I'm trying to find examples of it in the wild using grep.app but it doesn't seem like a very common idiom.

@opk12
Copy link
Author

opk12 commented Feb 9, 2024

(edited) Thank you for looking into it. I just wanted to throw an idea in the air, but I don't know if this is a common idiom in real-world code. I saw it in a student homework, which touches the file and then reads it.

def apri_file(nome):
    if not os.path.exists(nome):
        f = open(nome,"w")
        f.close()
    with open(nome, 'r') as route:
        for row in csv.reader(route, delimiter="\t"):
            ...

This example could be refactored to not touch:

def apri_file(nome):
    if os.path.exists(nome):
        with open(nome, 'r') as route:
            for row in csv.reader(route, delimiter="\t"):
                ...
    else:
        special case...

but as the warning is already implemented, I thought it just needs to be extended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants