Skip to main content

Strict access control for Python classes (private/protected/public).

Project description

strictaccess

CI PyPI Python versions Coverage License: MIT Typed: PEP 561

strictaccess enforces Java/C++-style access modifiers on Python classes through the @private, @protected, and @public decorators. Violations raise typed exceptions; an optional debug mode downgrades them to logged warnings.

This is a discipline tool, not a security boundary. See Limitations below.

Installation

pip install strictaccess

Requires Python 3.11+ (the engine relies on frame.f_code.co_qualname, introduced in CPython 3.11).

Quick example

from strictaccess import strict_access_control, private, protected, PrivateAccessError

@strict_access_control()
class Account:
    def __init__(self, balance: int) -> None:
        self._balance = balance        # underscore -> protected by convention

    @private
    def _adjust(self, delta: int) -> None:
        self._balance += delta

    @protected
    def _audit(self) -> str:
        return f"balance={self._balance}"

    def deposit(self, amount: int) -> None:
        self._adjust(amount)            # OK: internal call

acct = Account(100)
acct.deposit(50)                        # OK
acct._adjust(1000)                      # raises PrivateAccessError
acct._balance                           # raises ProtectedAccessError

What each decorator does

Decorator Callable from If called from outside
@public Anywhere (overrides underscore convention) Allowed
@protected Defining class + any subclass ProtectedAccessError
@private Defining class only PrivateAccessError

Plus, the underscore convention is enforced automatically:

Attribute name pattern Treated as
name (no underscore) public
_name protected
__name (name-mangled) private
__name__ (dunder) unrestricted

Debug mode

Replace exceptions with WARNING log records via the strictaccess logger:

import logging
logging.basicConfig(level=logging.WARNING)

@strict_access_control(debug=True)
class Audit:
    @private
    def _secret(self): return 42

Audit()._secret()  # logs a warning, returns 42

Useful for adopting strictaccess on a legacy codebase without stopping production behaviour while you fix call sites.

Limitations

strictaccess enforces discipline at attribute-read time. It is not a security boundary. Specifically:

  • object.__getattribute__(obj, name) bypasses the check entirely.
  • Pickling a decorated instance can fail because the wrapper class is created dynamically by type(...). Use copy.deepcopy instead, or unwrap the instance before pickling.

The full list is documented in docs/limitations.md.

Documentation

Full guide and API reference: https://jhoelperaltap.github.io/strictaccess (generated from docs/).

Contributing

See CONTRIBUTING.md for development setup, test commands, and the contribution flow. Open a discussion before large changes.

License

MIT — see LICENSE.

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

strictaccess-1.0.0.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

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

strictaccess-1.0.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file strictaccess-1.0.0.tar.gz.

File metadata

  • Download URL: strictaccess-1.0.0.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for strictaccess-1.0.0.tar.gz
Algorithm Hash digest
SHA256 d26bb947f000ddae3eb42c2444884690f9013093d4f396a4ed1f63eda94c3296
MD5 12b931ec47356cdaac3014d3beda25a4
BLAKE2b-256 239b6144c5b628b84585a7f50780b6c309462f3c9e5120137920cca40dccaed8

See more details on using hashes here.

Provenance

The following attestation bundles were made for strictaccess-1.0.0.tar.gz:

Publisher: release.yml on Jhoelperaltap/strictaccess

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file strictaccess-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: strictaccess-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for strictaccess-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c61e8070c561461c2d81db8d85eb0c0167f4ef081b3c95061104d9e4f73bb8cc
MD5 9d1fba23beb5dde96552e7f2e25b6253
BLAKE2b-256 c4f4c899446959eeda157bab9f1949668a77271b3c9bd355857b2177562b0e76

See more details on using hashes here.

Provenance

The following attestation bundles were made for strictaccess-1.0.0-py3-none-any.whl:

Publisher: release.yml on Jhoelperaltap/strictaccess

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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