Skip to main content

Static Checker to Enforce (Some) Encapsultion in Python.

Project description

Packaway is a tool for enforcing encapsulation and access control in Python by performing static code analysis.

Currently the distribution supplies a flake8 plugin.

Installing

To install:

$ pip install packaway

To lint your file:

$ flake8 example.py

Packaging rules

Whether a module is internal or not is indicated by whether its name has a single preceding underscore. If it does, then it is only “visible” within the package the module resides. Similarly, a function whose name has a preceding underscore is only “visible” to members with in the same module where the function is defined.

Suppose a project has the following structure:

./package
    ./person
        __init__.py
        api.py
        _greeting.py
        _reading.py
    ./office
        __init__.py
        api.py
        _hours.py
        ./_legal
            __init__.py
            api.py
            _compliance.py
        ./_accounting
            __init__.py
            api.py
            _booking.py

Example 1: package.office._legal._complicance, being named with a preceding underscore, it is only visible to modules within package.office._legal but not modules outside of package.office._legal. Importing package.office._legal._complicance in package.person.api would be a violation of the encapsulation intended.

Example 2: package.office._legal.api being named WITHOUT a preceding underscore, indicates that it is as visible as package.office._legal is to members within package.office. package.office._accounting._booking is allowed to import from package.office._legal.api because it is a member of package.office.

However, package.person._greeting should not be allowed to import package.office._legal.api because package.office._legal is only visible within package.office.

See the examples/package folder for more examples.

Limitations

This tool does not capture accessing privately named attribute on a module (an object in general) that can otherwise be imported following the above rules.

Motivation

Python does not enforce encapsulations. While this is enpowering for use cases where encapsulation matters little and has made Python hugely accessible to beginners, this means more disciplines are required for developers working on large systems (with great powers come great responsibilities).

Consequently, Python developers often rely on implicit naming conventions such as a preceding underscore to signal something being hidden. However this can only be enforced by vigorous code review. For a team of developers with different skill levels, this is difficult to achieve for a large project. Even the most seasoned developer with the best intention could still make mistakens, especially if the intended visibility of a software component isn’t obvious.

Many programming languages (e.g. Java, C#, C++) offer programmers ways to control over what is hidden and what is accessible via “access modifiers” or keywords such as “public”, “private” and “internal”. These protections are enforced by the compilers, but can be overruled with some efforts.

Packaway is created in order to provide a relatively easy way to enforce encapsulation in Python at the module level in a way that is not intrusive.

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

packaway-0.1.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

packaway-0.1.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: packaway-0.1.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.9

File hashes

Hashes for packaway-0.1.1.tar.gz
Algorithm Hash digest
SHA256 bd9b21efeff7f962076fb294593b0478ae70a034250e92889abadfb2bcec62b7
MD5 31c2da59ae064e2ae3f6c2d939097e0a
BLAKE2b-256 06518c90df74d0ef6a77c6f2c55b8506cab16d6ac50b049930e44f5cb433f993

See more details on using hashes here.

File details

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

File metadata

  • Download URL: packaway-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.6.9

File hashes

Hashes for packaway-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1060865e8e9c4475ce387b20da5e805608cbdb96e26df3c77df2d3e2cf98a5ca
MD5 7e679ec25afc6523e9b260240236a43e
BLAKE2b-256 0a73afb0f1493962679c0e72c46f03a5dfd83e2253a3a1542a81a8d938d572a5

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