Sub topic 5
Confirm behavior in versioned docs and changelogs
This chapter assumes you can run Python, use pip, and read library documentation. It also assumes you have already checked that a symbol exists, as covered in the previous chapter. Existence is not enough. A function can exist and still behave differently from what the generated code expects.
Why existence checks are not enough
An introspection check tells you that requests.get is real. It does not tell you what happens when you pass a particular argument, what the function returns, or whether the behavior changed in the version you run. Documentation and changelogs answer those questions.
Three common problems survive an existence check:
- The function exists, but the generated code passes an argument the function never accepted.
- The function exists, but its default or return type changed between versions.
- The function exists but is deprecated, and the generated code was written for an older release.
Read the docs for your version
Most projects publish documentation for several releases. Read the page that matches the version you found in the previous chapter, not the page the search engine ranks first. Search results often point at the oldest or the newest docs.
Look for a version selector, usually at the bottom left or top of the page. Many projects also put the version in the URL, such as /en/stable/, /en/latest/, or /en/2.0/. stable is the latest release, which may be newer than the one you have installed.
When you open the page for a function, check these items:
- The signature, including parameter names and defaults.
- Notes such as "Changed in version 2.0" or "Deprecated since version 3.12".
- The return type and what it contains on failure.
- The examples. Compare them with the generated code line by line.
Read the changelog
A changelog lists what changed in each release. Find the file in the repository, usually CHANGELOG.md, HISTORY.rst, or a "What's new" page in the docs. Read the entries between the version the model likely learned and the version you run.
Search the changelog for the name of the function or argument in question. Terms such as "removed", "deprecated", "renamed", and "breaking" are the most useful.
The pandas DataFrame.append example shows the pattern. The 2.0 release notes list the removal, and the docs point to pd.concat as the replacement. Generated code that calls append fails on pandas 2.0 and later with an AttributeError.
import pandas as pd
rows = [{"id": 1}, {"id": 2}]
# Outdated: DataFrame.append was removed in pandas 2.0.
# df = pd.DataFrame()
# for row in rows:
# df = df.append(row, ignore_index=True)
# Current: build the frame in one step, or use pd.concat.
df = pd.DataFrame(rows)Use the installed docstring as a quick cross-check
Your installed package carries its own documentation, and it always matches your version. Python's help() and __doc__ read it directly. This is faster than finding the right web page, and it cannot be the wrong version.
import inspect
import json
sig = inspect.signature(json.dumps)
print(sig)
print("indent" in sig.parameters) # True
print("pretty" in sig.parameters) # FalseUse the web docs for explanation and the installed docstring to confirm the details. If the two disagree, trust the installed code, then work out which docs page you were reading.
A verification routine
Follow this order for any call you cannot vouch for yourself. Do it for each new function or argument in the generated change, not for every line.
Common errors
- Reading
latestdocs. The page describes a release you have not installed. Switch the version first. - Trusting a blog post or answer. These are rarely versioned. Prefer the project's own docs.
- Skipping the changelog because the call "works". A call can run and still behave differently, for example by returning a different type or ignoring an argument.
- Ignoring deprecation notes. A deprecated call works today and breaks in a later release. The next chapters turn these notes into failures.
Exercise
The model suggests df.append(row, ignore_index=True) and your environment has pandas 2.2. The call fails with an AttributeError. Where do you look to confirm the cause, and what do you replace it with?
Show answer
Look in the pandas release notes (changelog) for version 2.0, which list the removal of DataFrame.append, and confirm on the docs page for your version. Replace the loop with pd.DataFrame(rows), or use pd.concat when you must combine existing frames.
Exercise
Why can a docs page for stable mislead you even when it is the official documentation?
Show answer
stable describes the latest release, which may be newer than the version you have installed. Signatures, defaults, and deprecations on that page may not match your environment, so select the page for your installed version.