From 9ce508f9392f6775a38ab1feadeb5dd241aa3ed6 Mon Sep 17 00:00:00 2001 From: Wes John-Alder Date: Thu, 19 Nov 2026 10:01:51 -0500 Subject: [PATCH 1/2] Document how to remove a capability from the public API Add a CONTRIBUTING.md section on replacing a removed field, module, or exported name with a shim that warns, and on scheduling the shim's deletion. Point AGENTS.md at the section. Bump the version to 3.0.4. Co-Authored-By: Claude --- AGENTS.md | 3 +- CONTRIBUTING.md | 97 +++++++++++++++++++++++++++++++++++++++++++++ gemd/__version__.py | 2 +- 3 files changed, 100 insertions(+), 2 deletions(-) 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..a230199 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -49,6 +49,103 @@ 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: +* The constructor warns when a caller passes the argument, even an empty list. It does not warn when the caller omits the argument. +* The getter warns and returns an empty value. +* The setter warns and discards the value, even `None`. +* 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_REMOVED = ( + "ProcessTemplate.allowed_names has been removed. GEMD Python discards the value." +) + +def __init__(self, name, *, allowed_names=None, ...): + ... + if allowed_names is not None: + warn(_ALLOWED_NAMES_REMOVED, DeprecationWarning, stacklevel=2) + +@property +def allowed_names(self): + """Get an empty list, and warn that allowed_names has been removed.""" + warn(_ALLOWED_NAMES_REMOVED, DeprecationWarning, stacklevel=2) + return [] + +@allowed_names.setter +def allowed_names(self, allowed_names): + warn(_ALLOWED_NAMES_REMOVED, DeprecationWarning, stacklevel=2) +``` + +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. +It reports a direct `warn(..., DeprecationWarning)` call as a non-blocking notice. +When a shim calls `warn()` directly and names a removal version, add a test that fails once `__version__` reaches it. +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 8d1c862..8e10cb4 100644 --- a/gemd/__version__.py +++ b/gemd/__version__.py @@ -1 +1 @@ -__version__ = "3.0.3" +__version__ = "3.0.4" From bad252eca0fd8584e6f972afe7b735c6eb707255 Mon Sep 17 00:00:00 2001 From: Wes John-Alder Date: Thu, 19 Nov 2026 11:07:11 -0500 Subject: [PATCH 2/2] Recommend @deprecated for shims in the removal guide The field example marks the getter and setter with @deprecated, so the removed_in gate requires the deletion. A version test remains the advice for a shim that calls warn() directly. Co-Authored-By: Claude --- CONTRIBUTING.md | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a230199..148ee07 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -63,9 +63,11 @@ Choose the shim by what the old code can do after the removal: ### 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 getter warns and returns an empty value. -* The setter warns and discards the value, even `None`. +* 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. @@ -73,24 +75,26 @@ Warn on empty values too, because any value shows that the name remains in the c `from_dict()` passes the field to the constructor, so old JSON that includes the field loads and warns. ```python -_ALLOWED_NAMES_REMOVED = ( - "ProcessTemplate.allowed_names has been removed. GEMD Python discards the value." -) +_ALLOWED_NAMES_DETAILS = "GEMD Python discards the value." def __init__(self, name, *, allowed_names=None, ...): ... if allowed_names is not None: - warn(_ALLOWED_NAMES_REMOVED, DeprecationWarning, stacklevel=2) + 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, and warn that allowed_names has been removed.""" - warn(_ALLOWED_NAMES_REMOVED, DeprecationWarning, stacklevel=2) + """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): - warn(_ALLOWED_NAMES_REMOVED, DeprecationWarning, stacklevel=2) + pass ``` Add tests for these behaviors: @@ -142,8 +146,11 @@ Add tests for these behaviors: 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. -It reports a direct `warn(..., DeprecationWarning)` call as a non-blocking notice. -When a shim calls `warn()` directly and names a removal version, add a test that fails once `__version__` reaches it. +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