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

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.4.tar.gz (6.6 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.4-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for flake8_close_io_buffers-0.1.4.tar.gz
Algorithm Hash digest
SHA256 2e24de80bbd599d6f3d71dedba3752e19b18202838edcea1a417a67a72fcac03
MD5 d879258f95a2421e891fbadcc46cd81f
BLAKE2b-256 d6181a4d7e9048db0ca9c8802a6e6813dda923644574442f022b5a0cb43c3f49

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flake8_close_io_buffers-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ddce2215716cd6955978b760c14d6ee5e327022a0d75e39ad545d97a2d35ef43
MD5 5cb9bb67c3a63cbd678e7511fb33f8ec
BLAKE2b-256 4bf22dfced9595a1bb80a5eaadd4f48b16997c0a9e25c9f6c98232e9c868d93a

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