diff --git a/AGENTS.md b/AGENTS.md index 78cd599..2d8bac2 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -50,12 +50,13 @@ CI also runs the tests on Python 3.10 through 3.14 on Linux, macOS, and Windows. - Write Numpy-style docstrings. Sphinx autogenerates `docs/source/reference/`, which git ignores. - Make arguments keyword-only. Every positional argument must be required. - Run `uv lock` after you change dependencies in `pyproject.toml`, and commit `uv.lock`. +- Before you remove a field, module, or exported name from the public API, follow "Removing a capability from the public API" in `CONTRIBUTING.md`. - Python 3.10 is the floor, set by `requires-python` and the ruff `target-version`. - List new data files under `[tool.setuptools.package-data]` in `pyproject.toml`, so the wheel includes them. ## Further reading -- `CONTRIBUTING.md`: checks, style, branching, and the release process +- `CONTRIBUTING.md`: checks, style, removing a capability, branching, and the release process - `gemd/demo/README.md`: the example builders - GEMD data model docs: https://citrineinformatics.github.io/gemd-docs/ - Package docs: https://citrineinformatics.github.io/gemd-python/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 62e1582..148ee07 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -49,6 +49,110 @@ Additionally: For additional (non-binding) inspiration, check out the [Google Python Style Guide](https://github.com/google/styleguide/blob/gh-pages/pyguide.md). +## Removing a capability from the public API + +Replace a removed capability with a shim, so that existing code that uses it keeps importing. +Each use of the shim emits a `DeprecationWarning`, so callers can find the use and delete it. +Adding a shim takes a minor version bump, because existing code keeps importing. +Deleting the shim breaks that code and takes a major version bump. + +Choose the shim by what the old code can do after the removal: +* When the class works without a field's value, keep the field as a shim that stores nothing. +* When the code behind a module or exported name is gone, keep the name importable and make each use of a removed class raise an error. + +### Removing a field from a class + +Keep the constructor argument and the property, and store nothing: +* Mark the getter and the setter with `@deprecated` from the `deprecation` library, so that each call warns. +* The getter returns an empty value. +* The setter discards the value, even `None`. +* The constructor warns when a caller passes the argument, even an empty list. It does not warn when the caller omits the argument. +* The constructor emits the library's `DeprecatedWarning` with `stacklevel=2`, so that Python attributes the warning to the caller's line. +* The docstring keeps the argument and states that the class discards its value. + +Warn on empty values too, because any value shows that the name remains in the caller's code. +`as_dict()` serializes instance attributes. The shim does not set one, so the field stays out of `as_dict()` and the JSON. +`from_dict()` passes the field to the constructor, so old JSON that includes the field loads and warns. + +```python +_ALLOWED_NAMES_DETAILS = "GEMD Python discards the value." + +def __init__(self, name, *, allowed_names=None, ...): + ... + if allowed_names is not None: + warn( + DeprecatedWarning("allowed_names", "3.1.0", "4.0.0", _ALLOWED_NAMES_DETAILS), + stacklevel=2, + ) + +@property +@deprecated(deprecated_in="3.1.0", removed_in="4.0.0", details=_ALLOWED_NAMES_DETAILS) +def allowed_names(self): + """Get an empty list.""" + return [] + +@allowed_names.setter +@deprecated(deprecated_in="3.1.0", removed_in="4.0.0", details=_ALLOWED_NAMES_DETAILS) +def allowed_names(self, allowed_names): + pass +``` + +Add tests for these behaviors: +* The constructor, getter, setter, and JSON loading each warn. +* The constructor does not warn when the argument is omitted. +* `as_dict()` and the JSON leave out the field. + +### Removing a module or exported name + +Keep every name that a release exported at its old import path: +* Define the stand-in classes in one private module. Constructing a stand-in raises an error whose message names the removed feature. Reading a missing attribute from the class, such as an old method, raises the same error. +* Give each module that exported a name a `__getattr__` function. It warns and returns the stand-in. +* The warning message gives the deprecation version, the removal version, and the removed feature. +* For an unknown name, `__getattr__` raises `AttributeError` and does not warn. + +Python calls a module's `__getattr__` only for names that the module does not bind. +Import the private module as a module, rather than importing its names, so that each removed name reaches `__getattr__`. +Therefore, `import gemd` does not warn, and importing a removed name does. +`stacklevel=2` attributes the warning to the importing line, so Python's default filters show it in a script. + +```python +"""The old feature has been removed.""" + +from warnings import warn + +from gemd import _removed_features + +_REMOVED_NAMES = {"OldFeature", "OldFeatureResult"} + + +def __getattr__(name): + if name not in _REMOVED_NAMES: + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") + warn( + f"{__name__}.{name} is deprecated as of 3.1.0 and will be removed in 4.0.0. " + "The old feature has been removed.", + DeprecationWarning, + stacklevel=2, + ) + return getattr(_removed_features, name) +``` + +Add tests for these behaviors: +* Importing each removed name from each old path emits the warning. +* Each use of a stand-in raises the error. +* Importing the package does not warn. + +### Scheduling the deletion + +The `removed_in` gate under [Testing](#testing) checks `@deprecated` decorators from the `deprecation` library. +It fails a PR once the package version reaches a decorator's `removed_in` version. +Mark a shim with `@deprecated` wherever a function or method can carry the decorator, so that the gate requires the deletion. +Write `deprecated_in` and `removed_in` as string literals on each decorator, because the gate reads them from the source. +The gate fails a PR when a decorator lacks either keyword. +The gate reports a direct `warn(..., DeprecationWarning)` call as a non-blocking notice. +When a shim calls `warn()` directly, as the module `__getattr__` above does, add a test that fails once `__version__` reaches the removal version. +Write the test's failure message as a list of the code to delete. + ## Branching strategy This project currently follows a [feature branch workflow](https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow): diff --git a/gemd/__version__.py b/gemd/__version__.py index f5f41e5..d539d50 100644 --- a/gemd/__version__.py +++ b/gemd/__version__.py @@ -1 +1 @@ -__version__ = "3.1.0" +__version__ = "3.1.1"