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

[ruff] IO operations performed on closed IO objects (RUF061) #15865

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

InSyncWithFoo
Copy link
Contributor

Summary

Resolves #10517.

RUF061 checks for references of IO objects whose binding is created by a with statement and reports if those references might trigger ValueError: I/O operation on closed file at runtime.

It recognizes the following patterns:

  • Method references: f.read()/_ = f.write
  • Contain checks: _ in f
  • for loops: for _ in f: ...

Test Plan

cargo nextest run and cargo insta test.

Copy link
Contributor

github-actions bot commented Feb 1, 2025

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

ℹ️ ecosystem check detected linter changes. (+16 -0 violations, +0 -0 fixes in 4 projects; 51 projects unchanged)

apache/airflow (+6 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

+ airflow/models/dagbag.py:319:17: RUF061 IO operation performed on closed IO object
+ airflow/utils/warnings.py:33:17: RUF061 IO operation performed on closed IO object
+ scripts/in_container/run_provider_yaml_files_check.py:223:17: RUF061 IO operation performed on closed IO object
+ scripts/in_container/run_provider_yaml_files_check.py:243:17: RUF061 IO operation performed on closed IO object
+ tests/core/test_configuration.py:1413:13: RUF061 IO operation performed on closed IO object
+ tests/sensors/test_external_task_sensor.py:155:13: RUF061 IO operation performed on closed IO object

pandas-dev/pandas (+1 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview

+ pandas/util/_exceptions.py:93:13: RUF061 IO operation performed on closed IO object

zulip/zulip (+3 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview --select ALL

+ zerver/tests/test_message_edit.py:59:13: RUF061 IO operation performed on closed IO object
+ zerver/tests/test_message_fetch.py:4134:13: RUF061 IO operation performed on closed IO object
+ zerver/tests/test_message_send.py:2346:13: RUF061 IO operation performed on closed IO object

astropy/astropy (+6 -0 violations, +0 -0 fixes)

ruff check --no-cache --exit-zero --ignore RUF9 --no-fix --output-format concise --preview

+ astropy/io/ascii/ui.py:1103:9: RUF061 IO operation performed on closed IO object
+ astropy/io/ascii/ui.py:1104:9: RUF061 IO operation performed on closed IO object
+ astropy/io/fits/scripts/fitscheck.py:184:9: RUF061 IO operation performed on closed IO object
+ astropy/utils/tests/test_data.py:1083:9: RUF061 IO operation performed on closed IO object
+ astropy/wcs/tests/test_profiling.py:74:9: RUF061 IO operation performed on closed IO object
+ astropy/wcs/tests/test_utils.py:808:13: RUF061 IO operation performed on closed IO object

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF061 16 16 0 0 0

@InSyncWithFoo
Copy link
Contributor Author

InSyncWithFoo commented Feb 1, 2025

There are a lot of false positives of this kind:

with open() as f:
	f.read()  # A reference to the next `f`

with open() as f:  # Same name
	...

with open() as f:  # Same name
	f.write()  # A reference to either of the previous `f`s

This seems to be a bug in the semantic model rather than a problem with this rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rule for ValueError: I/O operation on closed file.
1 participant