Skip to main content

A Python tool to guard against incorrect usage of python modules.

Project description

PyPI version

modguard

A Python tool to guard against incorrect usage of python modules.

What is modguard?

Modguard enables you to explicitly define the public interface for a given module.

This helps prevent other developers unintentionally mis-using code, which can lead to poor performance, security vulnerabilities, bugs, and more.

By declaring a module within modguard, all members of the module will be made private by default. Each member of the interface that you intend to make public can then be exposed through a simple decorator.

Modguard is incredibly lightweight, and has no impact on the runtime of your code. Instead, it's checks are performed through a lightweight CLI tool.

Installation

pip install modguard

Usage

Add a Boundary to the __init__.py of the module you're creating an interface for.

# core/__init__.py
from modguard import Boundary

Boundary(__name__)

Implement the public decorator on any part of the interface that is public

# core/main.py
from modguard import public

# Adding the decorator here signifies this function is public
@public
def public_function(user_id: int) -> str:
    ...

# This function will now be considered private
def private_function():
    ...

Modguard will now flag any incorrect usages of your interface.

> # From the root of your project
> guard .
1 error found.
utils/helpers.py:L45-60 E001 Restricted usage of 'core.main.private_function' in 'utils.helpers'

Details

Modguard works by analyzing the abstract syntax tree of your codebase. It will only protect against usages that are within the scope of the cli runtime, which is why we suggest always running the tool from the root of your project.

PyPi Package

License

GNU GPLv3

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

modguard-0.1.0.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

modguard-0.1.0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: modguard-0.1.0.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for modguard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c81467de10991371d9369e45553112042c51ce357b791c77beee69564e3d50c6
MD5 9a8061c167e63d9b0fc75d94e1c58193
BLAKE2b-256 8a1ff0ce8974b2f72ddbcec3550a2c6c4b250c52a714c0a52d1b7b3edcdc9fb6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: modguard-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for modguard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e60243e271be1cae247cffee96c7881642d70b21fc669f1e70778e793f0e184
MD5 4a43aa7fb820408786c3bd3fd6cf73d5
BLAKE2b-256 9b1f7bd8055e3c55c8a250f42a2d954bf5ab9234308e7c90ee85fd055c800e13

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