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

rule for ValueError: I/O operation on closed file. #10517

Open
tooptoop4 opened this issue Mar 22, 2024 · 5 comments · May be fixed by #15865
Open

rule for ValueError: I/O operation on closed file. #10517

tooptoop4 opened this issue Mar 22, 2024 · 5 comments · May be fixed by #15865
Labels
rule Implementing or modifying a lint rule

Comments

@tooptoop4
Copy link

ruff does not complain about:

with open('/a.txt', "w") as f:
    f.write('a')
with open('/b.txt', "w"):
    f.write('b')

but python does:

ValueError: I/O operation on closed file.
@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Mar 22, 2024
@MichaReiser
Copy link
Member

MichaReiser commented Mar 22, 2024

Do you think the rule could be generalized to disallow method calls on any object defined in a context expression after the with statement?

@zanieb
Copy link
Member

zanieb commented Mar 22, 2024

@MichaReiser no, you can safely use many objects after their context is exited :) we'd need type-inference for that to be broadly correct.

@autinerd
Copy link
Contributor

For example with with pytest.raises() as exc_info you have to use the object after the context is exited.

@charliermarsh
Copy link
Member

We would probably need to make this specific to open calls (which would be okay).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants