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 (RUF050) #15865

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

InSyncWithFoo
Copy link
Contributor

@InSyncWithFoo InSyncWithFoo commented Feb 1, 2025

Summary

Resolves #10517.

RUF050 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. (+2 -0 violations, +0 -0 fixes in 1 projects; 54 projects unchanged)

astropy/astropy (+2 -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: RUF050 IO operation performed on closed IO object
+ astropy/io/ascii/ui.py:1104:9: RUF050 IO operation performed on closed IO object

Changes by rule (1 rules affected)

code total + violation - violation + fix - fix
RUF050 2 2 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.

@MichaReiser MichaReiser added rule Implementing or modifying a lint rule preview Related to preview mode features labels Feb 3, 2025
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this.

As mentioned in the original PR, we have to restrict the rule to objects that are files. This should also help to remove the following false positive

https://github.com/apache/airflow/blob/8c9e0b2a8ec0e6f2842883898370741e71c0e802/airflow/models/dagbag.py#L317C12-L317C29

@InSyncWithFoo InSyncWithFoo changed the title [ruff] IO operations performed on closed IO objects (RUF061) [ruff] IO operations performed on closed IO objects (RUF050) Feb 3, 2025
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about the examples you mentioned as well as if f gets deleted.

It seems that the semantic model copies over all references when shadowing an existing binding.

let references = shadowed.references.clone();
let is_global = shadowed.is_global();
let is_nonlocal = shadowed.is_nonlocal();
// If the shadowed binding was global, then this one is too.
if is_global {
self.semantic.bindings[binding_id].flags |= BindingFlags::GLOBAL;
}
// If the shadowed binding was non-local, then this one is too.
if is_nonlocal {
self.semantic.bindings[binding_id].flags |= BindingFlags::NONLOCAL;
}
self.semantic.bindings[binding_id].references = references;

I think we have to make the rule shadowed-binding aware and skip over references that come from a shadowed binding.

@InSyncWithFoo
Copy link
Contributor Author

I think we have to make the rule shadowed-binding aware and skip over references that come from a shadowed binding.

I'm not sure how to do this, especially if all references are simply copied over.

@MichaReiser
Copy link
Member

Yeah, me neither. We'd have to look at other rules to see how to handle it. I don't think I've the time right now to do that. Maybe @charliermarsh knows?

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

Successfully merging this pull request may close these issues.

rule for ValueError: I/O operation on closed file.
2 participants