Getting It Right: Flipping a Default Without Surprising Anyone

Defaults are part of a package's contract even when nobody wrote them down. This hypothetical walkthrough follows "Package S", an invented YAML loader, as it changes the default for one option from permissive to strict. Each phase is shown with the code a maintainer would write and the reason it helps.

The change being made

Package S's load() currently accepts duplicate keys in a mapping and keeps the last one. The maintainers want duplicates to raise an error by default, because silent overwrites have hidden real configuration mistakes. Some users rely on the permissive behaviour, perhaps knowingly, perhaps not. That uncertainty is exactly why the change gets three phases instead of one release.

Phase 1: make the new behaviour available (minor)

def load(text, *, duplicate_keys=None):
    if duplicate_keys is None:
        duplicate_keys = "last"      # old default, unchanged
    ...

In the first minor release, duplicate_keys="error" becomes an accepted value. Nothing changes for anyone who does not pass it. Early adopters can opt in and report problems while the behaviour is still optional, which is a cheap way to find edge cases before they become everybody's problem.

Phase 2: warn people who depend on the old default (minor)

if duplicate_keys is None:
    if document_has_duplicates:
        warnings.warn(
            "Duplicate keys found; the default will change to 'error' in 3.0. "
            "Pass duplicate_keys='last' to keep the current behaviour.",
            FutureWarning, stacklevel=2)
    duplicate_keys = "last"

Two details make this warning useful. It only fires when the input actually contains duplicates, so users unaffected by the change never see it. And it tells the reader exactly how to silence it by stating their intent, rather than telling them to "update your code". Python's FutureWarning is meant for deprecated features aimed at end users and is shown by default, which suits a change that application authors need to notice.

Phase 3: flip the default (major)

In 3.0.0, the unset value maps to "error". Anyone who followed the warning already passes duplicate_keys="last" explicitly and sees no difference. Anyone who ignored it gets a clear exception that names the duplicated key and its line, instead of a quiet behaviour change. The changelog's Changed section leads with this item, and the migration note repeats the one-argument fix.

Why this counts as done well

RiskHow the phases handle it
Users broken without warningWarning ships at least one minor before the flip
Warning noise for unaffected usersFires only when duplicates exist
No way to keep old behaviourExplicit "last" remains supported in 3.x
Version number liesThe flip happens only in a major

The same pattern works in any language: add the option, warn on reliance, flip in a major, keep the escape hatch. For deeper treatment of the warning step, read our long read on deprecation policy.

Related pages