{"id":10067,"library":"public","title":"public: Python Module Visibility Decorator","description":"The `public` library for Python, version 2020.12.3, provides a `@public` decorator to explicitly mark functions, classes, and variables as part of a module's public API. It automatically manages and replaces the module's `__all__` list, aiming to simplify explicit module visibility control. The library appears to be in maintenance mode, with its last release in late 2020.","status":"maintenance","version":"2020.12.3","language":"en","source_language":"en","source_url":"https://github.com/andrewp-as-is/public.py","tags":["module-visibility","decorator","__all__","api-management"],"install":[{"cmd":"pip install public","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The `public` object itself is the primary decorator factory, which internally aliases `public.add`.","wrong":"from public import add","symbol":"public","correct":"from public import public"}],"quickstart":{"code":"# my_module.py\nfrom public import public\n\n@public\ndef hello_world():\n    \"\"\"A publicly exposed function.\"\"\"\n    return \"Hello from the public module!\"\n\ndef _private_function():\n    \"\"\"A private function, not exposed.\"\"\"\n    return \"You shouldn't see this!\"\n\n@public(alias='my_custom_name')\ndef original_function_name():\n    \"\"\"A public function exposed under an alias.\"\"\"\n    return \"This function has a custom public name.\"\n\n# To use this module:\n# from my_module import hello_world, my_custom_name\n# print(hello_world())\n# print(my_custom_name())\n# try:\n#     from my_module import _private_function # This will raise an AttributeError\n# except AttributeError as e:\n#     print(f\"Error trying to import private function: {e}\")","lang":"python","description":"This example demonstrates how to use the `@public` decorator for functions and how to alias them. It also illustrates that non-decorated functions remain private and cannot be imported."},"warnings":[{"fix":"Avoid manually defining `__all__` in modules where `public` is used, or be mindful that `public` will manage it. Ensure all desired public symbols are decorated.","message":"The `public` decorator actively modifies or overwrites your module's `__all__` list. If you manually define `__all__`, the decorated symbols will typically take precedence, potentially leading to unexpected exposure or hiding of symbols.","severity":"gotcha","affected_versions":"All versions"},{"fix":"To expose a variable `my_var`, either use `@public('my_var')` on a dummy function (e.g., `def _pass(): pass`) or explicitly call `public.add(my_var=my_var)` in your module's top-level scope.","message":"Directly decorating non-callable objects (like variables or constants) with `@public` as `@public my_var = 'value'` is not supported. You must use specific syntax to expose variables.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Factor project status into your dependency choices; consider alternatives for new projects requiring active development or modern Python features.","message":"The `public` library appears to be in maintenance mode, with its last commit and release in 2020. While stable, users should be aware that active development, new features, or timely bug fixes are unlikely.","severity":"maintenance","affected_versions":"All current and future users"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Decorate `your_private_func` with `@public` in `your_module.py` if it should be public, or refrain from importing it.","cause":"You attempted to import a function, class, or variable from a module that was not explicitly marked with `@public`. The `public` library automatically manages `__all__`, only exposing decorated symbols.","error":"AttributeError: module 'your_module' has no attribute 'your_private_func'"},{"fix":"For variables, use `@public('variable_name')` on a dummy `def _pass(): pass` or explicitly call `public.add(variable_name=your_variable)`. Ensure `@public` is used correctly as a decorator for functions/classes.","cause":"This often occurs when trying to apply `@public` directly to a variable declaration, or when calling `public` incorrectly (e.g., `public()`). The decorator needs to be applied to a callable or via specific variable exposure syntax.","error":"TypeError: 'NoneType' object is not callable"},{"fix":"Ensure the variable `my_variable` is defined at the module level and that the `public.add` mechanism (either via decorator or direct call) is correctly implemented and executes during module import.","cause":"When using `@public('my_variable')` on a dummy function or `public.add(my_variable=my_variable)`, the variable `my_variable` must exist in the module's top-level scope and be correctly referenced by name or object.","error":"NameError: name 'my_variable' is not defined"}]}