Strict access control for Python classes (private/protected/public).
Project description
strictaccess
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(...). Usecopy.deepcopyinstead, 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d26bb947f000ddae3eb42c2444884690f9013093d4f396a4ed1f63eda94c3296
|
|
| MD5 |
12b931ec47356cdaac3014d3beda25a4
|
|
| BLAKE2b-256 |
239b6144c5b628b84585a7f50780b6c309462f3c9e5120137920cca40dccaed8
|
Provenance
The following attestation bundles were made for strictaccess-1.0.0.tar.gz:
Publisher:
release.yml on Jhoelperaltap/strictaccess
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strictaccess-1.0.0.tar.gz -
Subject digest:
d26bb947f000ddae3eb42c2444884690f9013093d4f396a4ed1f63eda94c3296 - Sigstore transparency entry: 1509224485
- Sigstore integration time:
-
Permalink:
Jhoelperaltap/strictaccess@2a7b65e6ec325ad4f9a6af033cabbe521a1f137f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/Jhoelperaltap
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2a7b65e6ec325ad4f9a6af033cabbe521a1f137f -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c61e8070c561461c2d81db8d85eb0c0167f4ef081b3c95061104d9e4f73bb8cc
|
|
| MD5 |
9d1fba23beb5dde96552e7f2e25b6253
|
|
| BLAKE2b-256 |
c4f4c899446959eeda157bab9f1949668a77271b3c9bd355857b2177562b0e76
|
Provenance
The following attestation bundles were made for strictaccess-1.0.0-py3-none-any.whl:
Publisher:
release.yml on Jhoelperaltap/strictaccess
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strictaccess-1.0.0-py3-none-any.whl -
Subject digest:
c61e8070c561461c2d81db8d85eb0c0167f4ef081b3c95061104d9e4f73bb8cc - Sigstore transparency entry: 1509224627
- Sigstore integration time:
-
Permalink:
Jhoelperaltap/strictaccess@2a7b65e6ec325ad4f9a6af033cabbe521a1f137f -
Branch / Tag:
refs/tags/v1.0.0 - Owner: https://github.com/Jhoelperaltap
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@2a7b65e6ec325ad4f9a6af033cabbe521a1f137f -
Trigger Event:
push
-
Statement type: