open() without a context manager
Part of Code Quality & Syntax, which counts for 20% of the overall score. When this check fires it deducts 4 points from that category, once per scan, no matter how many places it turns up.
What it detects
A file opened without a with statement is only closed if the code remembers to call .close(), and an exception in between skips that entirely. Heuristic: cannot see whether a handle stored in a variable is closed later some other way.
Why it matters
Calling open() without a with statement means the file handle is only closed if the code later remembers to call .close(), and if an exception is raised in between, that close is skipped entirely. Leaked file handles accumulate over the life of a long-running process and can eventually hit the operating system's open-file-descriptor limit, causing unrelated operations to start failing with 'too many open files.' This check is a heuristic: it cannot see whether a handle stored in a variable is closed later some other way, so treat it as a prompt to double check rather than a guaranteed bug.
How to fix it
Wrap the open() call in a with statement so the file is always closed when the block exits, even on an exception: with open(path) as fh: and use fh inside the block. If you need the file open across multiple methods of a class (a long-lived handle), make the class itself a context manager with __enter__ and __exit__, or explicitly call .close() in a finally block.
The paid report includes a ready-to-paste prompt for your AI coding agent for every check it finds, pointed at the exact findings from your scan. See pricing
Does your repo trip this check?
Paste a GitHub URL or drop a project folder. Scans run in your browser and take seconds.
Scan your repo