ruff
ruff copied to clipboard
rule for ValueError: I/O operation on closed file.
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.
Do you think the rule could be generalized to disallow method calls on any object defined in a context expression after the with statement?
@MichaReiser no, you can safely use many objects after their context is exited :) we'd need type-inference for that to be broadly correct.
For example with with pytest.raises() as exc_info you have to use the object after the context is exited.
We would probably need to make this specific to open calls (which would be okay).
https://github.com/wemake-services/wemake-python-styleguide?tab=readme-ov-file finds this
flake8 --select WPS441 somefile.py
https://wemake-python-styleguide.readthedocs.io/en/0.16.1/pages/usage/violations/best_practices.html#wemake_python_styleguide.violations.best_practices.ControlVarUsedAfterBlockViolation