open-file-without-with
What it does
Section titled “What it does”Reports a call of open or tempfile.NamedTemporaryFile and the like that is not the context of a with, is not returned and is not given to enter_context.
Why it matters
Section titled “Why it matters”A file that is not closed explicitly is closed when the object is freed, which depends on the interpreter and on references that remain, and on an error between the open and the close it stays open. Handles that stay open run a process out of descriptors, and buffered data may not be written.
To fix, open the file in a with statement.
Example
Section titled “Example”... x = 1
f = open("a")
...Configuration
Section titled “Configuration”python: lint: rules: open-file-without-with: warn # off (default), warn, errorEnabled by the presets: python/ruff.
Suppress one line with sudsy-disable-next-line open-file-without-with in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| ruff | SIM115 |
full |