Skip to main content

A flake8 plugin to enforce strict local scope usage with @innerscope decorator

Project description

flake8_innerscope

A Flake8 plugin to enforce strict local variable scope discipline via the @innerscope decorator.

Overview

The @innerscope decorator and corresponding Flake8 plugin help detect unintended outer-scope variable access inside functions. This encourages writing self-contained, side-effect-free functions that do not depend on variables outside their explicit scope.

Key Use Cases

  • Detect Outer Scope Access: Warns when a function decorated with @innerscope accesses variables not declared locally or passed as arguments.
  • Improve Isolation and Refactor Safety: Ensures marked functions have no hidden dependencies, making them easier to pass around and refactor.
  • Aid Large Codebase Understanding: Quickly identify functions that rely on outer scope objects.
  • Explicit Scope Intent: Makes scope intentions clear for maintainability.
  • Early Bug Detection: Catches subtle bugs caused by accidental outer scope usage.

Installation

pip install flake8_innerscope

Usage

Decorate functions you want to enforce local scope on:

from innerscope import innerscope  # (Optional, but your linter might judge you anyway)

NUM = 10

@innerscope(allow=['math'], severity='warning', allow_uppercase=True)
def my_function(x):
    s = sum([i for i in range(NUM)])  # Allowed locals i, s and outer uppercase NUM
    y = np.cos(x)  # Warning : outer-scope variable 'np' accessed in @innerscope
    z = math.cos(w)  # Warning : outer-scope variable 'w' accessed in @innerscope
    ...

Let your IDE show warnings or run Flake8 as usual:

flake8 path/to/your/code

The plugin will emit warnings or errors when outer-scope variables are accessed in decorated functions except those whitelisted by allow.

Configuration

  • allow: List of variable names allowed from outer scope (default empty).
  • severity: One of "error", "warning", or "info" to control message level (default "warning").
  • allow_uppercase: If True, allow accessing outer variables with all uppercase names.

Development

Clone the repo and install dependencies:

pip install -e .

License

MIT License


This project helps enforce better code quality in Python projects by making variable scope usage explicit and controlled.

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_innerscope-0.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

flake8_innerscope-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file flake8_innerscope-0.1.0.tar.gz.

File metadata

  • Download URL: flake8_innerscope-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for flake8_innerscope-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dabaa87d7b70efe76757360d5897d61d79e16452832730bd5bf5b937f8ac2848
MD5 3529e83780b85f4a2bd471b8a15c96e1
BLAKE2b-256 854c7e39f4743c859ed4659055abdc09cee282ab8fca183d0ecfab99cc9a8c2f

See more details on using hashes here.

File details

Details for the file flake8_innerscope-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_innerscope-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b4a1e68eefb07ac9c372b73a4e2b71874e81ab5bd1be181abb238c7a11684a6
MD5 92115e24e72a8b997fa5262fdeab22d0
BLAKE2b-256 50216cf0ab63a6643ec18fadb81abb5fddf5ca146d1deed6d20a899c7c1654b0

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