Skip to main content

A simple package for block scoping

Project description

Block Scoping

This is simple PyPi package running static analysis to detect scoping issues caused by Python's lack of block scoping.

Python's scoping is a common source of bugs.

Example

total = 0
for item in items:
    if item > 0:
        total += item
    else:
        skipped = True
    
if skipped:
    print("Some items have been skipped")

The mistake is easy to spot. However, in larger codebases, such issues can be much harder to detect.

Another Example

try:
    x = int(y)
    f(x)
except:
    logging.error(f"error when x = {x}")

The intent is to catch errors raised by f, but this could also catch errors raised by int(y), which this except is not prepared for.

To avoid these potential bugs, it's safer not to reference any variable in the except block that was defined within the try block. A simple fix would be:

x = None
try:
    x = int(y)
    f(x)
except:
    logging.error(f"error when x = {x}")

This package helps you identify these issues before execution, for example by automatically checking your code in your CI/CD script.

To check your code, install the package and run:

./check_block_scoping your_dir/*.py

or

./check_block_scoping your_dir --exclude notthis.py notthat.py

This will recursively search your_dir.

You can opt-out specific functions and classes by decorating them with @no_block_scoping.

Alternatively, you can explicitly opt-in with @block_scoping, which removes the need to run ./check_block_scoping altogether, especially if you want to enforce scoping rules more strictly across your organization, or only at specific locations.

Rules

The package implements the following rules:

Control Flow Scope of variables defined inside block
If/Elif/Else block only
For loop block only
While Loop block only
Walrus assignment block only if used in an if or a while
with block_scope() block only
Other With statements variables outlive their block
Try/Else/Finally Try/Else/Finally: outlive their block but can't be used in Except
Except block only
Case (Python >= 3.10) block only

This applies to:

  • all symbols, including function names.
  • all attributes of self,

Specifically, you are only allowed to use an attribute of self if the attribute is defined in the constructor.

with statements do not create block scopes. This is because in 99% of cases, the intent of a with statement is for its block to always be executed, unlike if/while/try which are designed for the opposite.

This is why the special object block_scope was added.

with block_scope():
    x = 3
print(x)  # check_block_scoping counts this as an error

Known Issues

  • ./check_block_scoping is not aware of the variables imported via from module import *, potentially raising false positives. The @block_scoping decorator doesn't have this issue.

  • Except for self in classes, ./check_block_scoping won't detect scoping issues that involve object attributes.

External Contribution

Contributions are welcomed!

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

block_scoping-0.1.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

block_scoping-0.1.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file block_scoping-0.1.1.tar.gz.

File metadata

  • Download URL: block_scoping-0.1.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for block_scoping-0.1.1.tar.gz
Algorithm Hash digest
SHA256 b2dfa673b60cf09ee815251f4da274ccbd3365b62e04eab2808bca57f3a4838d
MD5 feb2aa5f926ba441e20f9fd42c061293
BLAKE2b-256 9f27df9966cb5fc0f2a10b14b10a62a3486f1c15be834305c557ad453f8e6981

See more details on using hashes here.

File details

Details for the file block_scoping-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: block_scoping-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for block_scoping-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c7a0736f6895222dba720e761cedd66bd928ebe872f2f1573f7b8648f3d34a84
MD5 f53c8e4916c5178d2185fe85d66a186c
BLAKE2b-256 d4f0878ec8dfa3f1d8defeb2141df7b3077ec4c8d467a16ca80038150b071c73

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