Skip to main content

A Flake8 plugin to detect modifications of constants

Project description

Flake8 Constants

A Flake8 plugin to detect modifications of constants in Python code, enhancing code quality and preventing unintended behavior.

Why It's Needed

Constants are meant to be immutable values that don't change throughout the execution of a program. However, Python doesn't have built-in support for true constants, relying on naming conventions (all uppercase) to indicate that a variable should be treated as a constant. This plugin helps enforce these conventions by detecting and reporting attempts to modify constants, which can lead to:

  1. Improved code reliability: Preventing accidental modifications of values that should remain constant.
  2. Better maintainability: Ensuring that constants are used as intended across the codebase.
  3. Clearer intent: Helping developers distinguish between mutable variables and immutable constants.
  4. Reduced bugs: Catching potential errors early in the development process.

Features

  • Detects reassignment of constants (variables with all uppercase names)
  • Identifies modifications to class constants
  • Catches augmented assignments to constants (e.g., +=, -=)
  • Warns about potential modifications through method calls on constants
  • Configurable list of non-modifying methods to reduce false positives
  • Works across different scopes: global, class, and function levels

Installation

You can install Flake8 Constants using pip:

pip install flake8-constants

Usage

Once installed, flake8 will automatically use this plugin. Run flake8 as usual:

flake8 your_script.py

or integrate it into your existing flake8 configuration.

Configuration

You can configure the plugin by creating a .flake8 file in your project root and adding the following options:

[flake8]
ignore = C001,C005

This example ignores errors related to constant reassignment and augmented assignments.

Error Codes

  • C001: Reassignment of constant
  • C002: Modification of constant in a function
  • C003: Modification of constant in a class method
  • C004: Modification of constant in a loop
  • C005: Modification of constant in a comprehension

Configuration

No additional configuration is required. The plugin works out of the box with default flake8 settings.

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_constants-0.0.3.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

flake8_constants-0.0.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file flake8_constants-0.0.3.tar.gz.

File metadata

  • Download URL: flake8_constants-0.0.3.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for flake8_constants-0.0.3.tar.gz
Algorithm Hash digest
SHA256 20bdbcc7f7205c5d1eac16a1cac7206508960daf76f2edd9eb65305b7d28eff4
MD5 a852790d84236d2910c77e1990e33bf3
BLAKE2b-256 6a84ca827c8906741b5bf197dc5a3cc5fd792f5ed3190798f9588b80c0830717

See more details on using hashes here.

File details

Details for the file flake8_constants-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_constants-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 115f647226b63250446e696ce62b5b3a737cb77eb65c50bd746316f7f4691ff8
MD5 0434740d6f2222d8655199d1ffb147cf
BLAKE2b-256 c570a8633664f4d6de45ab70fa45596d2715b1851bd4efab5448a128833b38e2

See more details on using hashes here.

Supported by

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