Skip to main content

Flake8 plugin to detect opened but not closed IO buffers

Project description

flake8-close-io-buffers

A Flake8 plugin to detect unclosed IO buffers in your Python code. This tool helps ensure that IO objects (such as io.BytesIO and io.StringIO) are properly closed either via a context manager or an explicit .close() call.

Features

  • Detects Unclosed IO Objects:
    Reports an error (IO100) when an IO object is instantiated but not closed.

  • Supports Different Import Styles:
    Works with both:

    • Qualified imports:
      import io
      io.BytesIO()
      
    • Direct imports:
      from io import BytesIO, StringIO
      BytesIO()
      
  • Multiple Assignment Support:
    Correctly handles cases where IO objects are assigned to variables, including tuple/list assignments.

Installation

You can install the plugin from PyPI:

pip install flake8-close-io-buffers

Or install it in editable mode from source:

git clone https://github.com/your-username/flake8-close-io-buffers.git
cd flake8-close-io-buffers
pip install -e .

Usage

Once installed, Flake8 will automatically load the plugin. Run Flake8 on your Python files as usual:

flake8 your_python_file.py

Configuration

By default, Flake8 may not display errors from custom plugins. To ensure that IO100 errors are reported, add the following to your Flake8 configuration file (e.g., .flake8, setup.cfg, or tox.ini):

[flake8]
extend-select = IO100

Or, run Flake8 with the select flag:

flake8 --select=IO100 your_python_file.py

Example

Consider the following code in example.py:

import io

def foo():
    io.BytesIO()  # Unclosed IO object: will trigger IO100 error

Running Flake8:

flake8 --select=IO100 example.py

Will produce an error like:

example.py:4:5: IO100 unclosed IO object instantiation not assigned to a variable

Running Tests

The project uses pytest for testing. To run the tests:

  1. Ensure you have the development dependencies installed (e.g., pytest).
  2. From the project root, run:
pytest

Contributing

Contributions are welcome! Please feel free to open issues or submit pull requests on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8_close_io_buffers-0.1.3.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_close_io_buffers-0.1.3-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file flake8_close_io_buffers-0.1.3.tar.gz.

File metadata

  • Download URL: flake8_close_io_buffers-0.1.3.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for flake8_close_io_buffers-0.1.3.tar.gz
Algorithm Hash digest
SHA256 18a9d41311641e7283c477b959cefdc8ca40a159eb100564348c4e8c334e1322
MD5 40052ff46dc8f432bd2597272668a88c
BLAKE2b-256 a21a5d30648bf4d1460fc00bc4fed6aefaaca8042e85d46bc83277dd0c7b354d

See more details on using hashes here.

File details

Details for the file flake8_close_io_buffers-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_close_io_buffers-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 440d712ee7441c5852f1147aeb4fe30c0468968f864f04b99119504c9ebc90ce
MD5 4468fd28145b307027afa4baafa1acf4
BLAKE2b-256 357a0409396adc433bda4e703ca568ab16984c29a6bc0b2c3219cf112091312c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page