Full answer
python
import sys, tempfile, textwrap, warnings
from pathlib import Path
root = Path(tempfile.mkdtemp()) / "src" / "payments"
root.mkdir(parents=True)
(root / "_core.py").write_text(textwrap.dedent('''
def charge(amount_paise: int) -> str:
return f"charged {amount_paise}"
def _retry(): pass
'''))
(root / "__init__.py").write_text(textwrap.dedent('''
import warnings
from ._core import charge
__all__ = ["charge"]
__version__ = "2.3.0"
def old_charge(amount):
warnings.warn("old_charge is deprecated; use charge", DeprecationWarning, stacklevel=2)
return charge(amount)
'''))
(root / "py.typed").write_text("")
sys.path.insert(0, str(root.parent))
import payments
ns = {}
exec("from payments import *", ns)
print(payments.__version__, sorted(k for k in ns if not k.startswith("__")))
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
print(payments.old_charge(500), w[0].category.__name__)
print(sorted(p.name for p in root.iterdir() if not p.name.startswith("__pycache")))Output:
text
2.3.0 ['charge']
charged 500 DeprecationWarning
['__init__.py', '_core.py', 'py.typed']The run builds a minimal library in a temporary src layout. from payments import * exposes only charge, as declared in __all__, while _core and _retry stay internal by convention. The deprecated function still works but emits a DeprecationWarning pointing at the caller, thanks to stacklevel=2. The empty py.typed file tells type checkers to use the inline hints.
Further design points: keep the public surface small, because everything exported becomes a promise; document a support policy for Python versions; add a changelog; run tests against the oldest and newest supported versions; and give consuming teams a migration note for every major release.
python
import sys, tempfile, textwrap, warnings
from pathlib import Path
root = Path(tempfile.mkdtemp()) / "src" / "payments"
root.mkdir(parents=True)
(root / "_core.py").write_text(textwrap.dedent('''
def charge(amount_paise: int) -> str:
return f"charged {amount_paise}"
def _retry(): pass
'''))
(root / "__init__.py").write_text(textwrap.dedent('''
import warnings
from ._core import charge
__all__ = ["charge"]
__version__ = "2.3.0"
def old_charge(amount):
warnings.warn("old_charge is deprecated; use charge", DeprecationWarning, stacklevel=2)
return charge(amount)
'''))
(root / "py.typed").write_text("")
sys.path.insert(0, str(root.parent))
import payments
ns = {}
exec("from payments import *", ns)
print(payments.__version__, sorted(k for k in ns if not k.startswith("__")))
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
print(payments.old_charge(500), w[0].category.__name__)
print(sorted(p.name for p in root.iterdir() if not p.name.startswith("__pycache")))Run on Python 3.13.5, twice in fresh interpreters, with identical output.
Key points
- src layout with pyproject.toml
- Explicit public API with __all__
- Underscore-prefixed internals
- Semantic versioning with deprecation periods
- Ship py.typed for type hints
Common mistakes
- Exposing every module as public API
- Removing functions without a deprecation period
Follow-ups
- How would you handle a breaking change used by ten teams?
- How do you publish to an internal index?
Tips
- Show the deprecation path