Skip to main content

Static Checker to Enforce (Some) Encapsulation 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.2.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.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: packaway-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 acc33665ec8b294e9d83f8d0c560ce3cdf3bb731f44d90a48b0dd0f43c08eaa1
MD5 25adabd8bf3bc05469dcae7e7a6db281
BLAKE2b-256 64bd678cb16f2b620c7f754cdb172c3918ed227ca40a80dd4fdc4951b58da122

See more details on using hashes here.

File details

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

File metadata

  • Download URL: packaway-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ba6e4173cf60dc8e3ba6ceb3da70616994a2d40bba64768a4801d786ececb253
MD5 8e6532b7d1ce4608e9367298873b1298
BLAKE2b-256 084837d79fab301f2931bc30ed9836643536f4725fd2ae397e9c4b5bd80aafc8

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