Training Notes: Writing Deprecation Warnings People Act On

A deprecation warning is the only message from you that most users will ever read inside their own terminal. Done well, it turns a future breaking change into a five-minute fix. Done badly, it is noise that gets silenced and forgotten. This unit covers the content of a good warning and the mechanics in three ecosystems, then gives you a rewriting exercise.

Objectives

What a good warning says

Think of the reader as someone who did not choose to see this message and has a deadline. Give them:

  1. What is deprecated, by its public name.
  2. What to use instead, as concrete code if it fits on one line.
  3. When it will be removed, as a version ("removed in 5.0"), not a date you might miss.
  4. Where to read more: a stable link to the migration notes.

A compact template: "fetchAll() is deprecated and will be removed in 5.0. Use list({ all: true }) instead. See docs/migrating-to-v5.md".

The mechanics

Python

The standard library warnings module is the conventional route. Use DeprecationWarning and set stacklevel so the warning reports the caller's file and line:

import warnings

def fetch_all(client):
    warnings.warn(
        "fetch_all() is deprecated and will be removed in 5.0; "
        "use client.list(all=True)",
        DeprecationWarning,
        stacklevel=2,
    )
    return client.list(all=True)

Python hides DeprecationWarning by default except in code run directly as __main__, and test runners such as pytest surface them. If you need end users of applications to see the message, FutureWarning is the category the Python documentation describes for that audience.

Node.js

util.deprecate() wraps a function and prints its message once per process the first time the function is called. Pass a code as the third argument so users can filter it, and remember that the --no-deprecation flag silences these warnings entirely. For whole packages or versions, npm also offers npm deprecate, which shows a message at install time.

Go

Go has no runtime warning convention. Instead, add a paragraph to the doc comment starting with Deprecated:. Editors, pkg.go.dev and linters such as staticcheck recognise it and flag callers.

// FetchAll returns every item.
//
// Deprecated: use List with All set to true. FetchAll will be removed in v5.
func FetchAll(c *Client) ([]Item, error)

When not to warn

Practice exercise

Rewrite these three illustrative warnings so they meet the four-part standard. Package F is hypothetical.

  1. "Warning: this option is old"
  2. "connect() deprecated"
  3. "Package F: legacy mode will go away soon, please update"

Self-check answers

  1. Name the option and its replacement: "The 'retries' option is deprecated and will be removed in 3.0. Use 'retry: { attempts }' instead. See the migration notes."
  2. Add replacement, version and link: "connect() is deprecated and will be removed in 3.0. Use createConnection(). See the migration notes."
  3. Replace "soon" with a version and "please update" with an instruction: "legacyMode: true is deprecated and will be removed in 3.0. Remove the flag; the default now matches. See the migration notes."

A warning is one step in a longer process; the migration guide tutorial covers what comes after it.

Related pages