Publication
Publication helps maintain public-api-friendly Python modules by preventing unintentional access to private implementation details via introspection. It's currently at version 0.0.3 and is an early-stage library with a focused purpose.
Warnings
- gotcha Publication works by modifying module `__dict__` and `__all__` at import time. This means it primarily affects tools relying on introspection (debuggers, IDE autocompletion, `dir()`), but it does not prevent direct attribute access if a user knows the name.
- gotcha This library is intended for API hygiene and clarity, not for security. Do not rely on `publication` to enforce security boundaries or prevent malicious access to internal components, as direct access is still possible.
- gotcha Publication operates at import time. It may not function as expected or might be bypassed by dynamically loaded modules, modules loaded in unusual ways, or changes to objects made after the initial import process.
Install
-
pip install publication
Imports
- publish
from publication import publish
Quickstart
from publication import publish
@publish
class MyPrivateImplementation:
def _secret_method(self):
return "This should not be accessible"
def public_method(self):
return "This is public"
# Without publication, you could do:
# obj = MyPrivateImplementation()
# print(obj._secret_method())
# With publication, the _secret_method will not be exposed by introspection tools.
# Direct access still works, but tools won't find it.
# Example of usage (not for execution, just demonstrating effect):
# If you run dir(MyPrivateImplementation) or use an IDE's autocomplete,
# _secret_method should not appear.
# For demonstration, direct access still works for runtime logic, but not discovery.
obj = MyPrivateImplementation()
assert obj.public_method() == "This is public"
# print(obj._secret_method()) # This line would still run if uncommented,
# but it won't be discovered by introspection.