Introduction
When you update a library, you need to know whether your code will still work. The version number is the project's answer. Semantic versioning, or SemVer, gives every part of the number a meaning, so a glance at 1.4.2 to 2.0.0 tells you to expect trouble, while 1.4.2 to 1.4.3 tells you it is safe.
A changelog says the same in words. Together they are how maintainers talk to the users of every release, and a contributor who writes a fix is often asked to add a changelog line for it.
How it works
A version has three numbers, MAJOR.MINOR.PATCH:
- PATCH goes up for bug fixes that change no behaviour users rely on:
1.4.2to1.4.3. - MINOR goes up when functionality is added in a way that still works with old code:
1.4.3to1.5.0, and PATCH goes back to 0. - MAJOR goes up for a breaking change:
1.5.0to2.0.0, and the others go back to 0.
While MAJOR is 0 (0.x.y) the project says it is still changing and anything may break. A pre-release is written with a dash, as in 2.0.0-rc.1, and it sorts before 2.0.0.
Versions must be compared number by number. As text, "1.10.0" sorts before "1.9.0" because the character 1 comes before 9, but as numbers 10 is larger than 9, so 1.10.0 is the newer version. The example shows both orders side by side.
Release tools can choose the number from the commit messages if the project uses conventional commits: a feat commit means MINOR, a fix commit means PATCH, and a type followed by ! (as in feat!:) or the words BREAKING CHANGE means MAJOR. The highest kind wins.
The changelog lists, for each release, the changes grouped under headings such as Added, Changed, Fixed and Removed, with the newest release first, with the date. Write for users: "Fixed a crash when the list is empty" and not "added an if in average()". Mark breaking changes clearly and say how to update.
Releases are marked in Git with a tag such as v1.5.0, so that anyone can get exactly the code of that version.
Example
def parse(version):
major, minor, patch = version.split(".")
return int(major), int(minor), int(patch)
def bump(version, change):
major, minor, patch = parse(version)
if change == "breaking":
return f"{major + 1}.0.0"
if change == "feature":
return f"{major}.{minor + 1}.0"
return f"{major}.{minor}.{patch + 1}"
versions = ["1.10.0", "1.2.0", "1.9.3", "2.0.0", "1.2.10"]
print("by number:", sorted(versions, key=parse))
print("as text: ", sorted(versions))
version = "1.4.2"
for change in ["fix", "feature", "breaking"]:
print(f"{version} + {change} = {bump(version, change)}")
changes = {"Added": ["dark mode"], "Fixed": ["crash on empty list", "typo in help text"]}
print("## 1.5.0")
for section, items in changes.items():
print("###", section)
for item in items:
print("-", item)Line by line
parseturns"1.10.0"into the numbers(1, 10, 0), which Python compares part by part.bumpapplies the rules: a breaking change raises MAJOR and resets the others, a feature raises MINOR, anything else raises PATCH.- Sorting with
key=parsegives the right order; sorting the plain strings puts1.10.0before1.2.0. - The last lines print a changelog entry for a release, grouped by headings.
Expected result
by number: ['1.2.0', '1.2.10', '1.9.3', '1.10.0', '2.0.0']
as text: ['1.10.0', '1.2.0', '1.2.10', '1.9.3', '2.0.0']
1.4.2 + fix = 1.4.3
1.4.2 + feature = 1.5.0
1.4.2 + breaking = 2.0.0
## 1.5.0
### Added
- dark mode
### Fixed
- crash on empty list
- typo in help text
In the real world
Package managers use version rules to decide which updates are safe. A rule such as requests>=2.0,<3.0 allows every 2.x release and refuses 3.0, trusting the library to follow SemVer. When a project breaks that trust, with a breaking change in a minor release, users' builds fail, so maintainers take the numbering seriously, and good changelogs save everyone a lot of support questions.
Common mistakes
- Sorting versions as strings. Compare the numbers.
- A breaking change in a minor or patch release, which breaks users who trusted the number.
- A changelog that is a copy of the commit log. Users do not care about internal refactoring.
- Forgetting to reset the lower numbers when a higher number goes up.
Best practices
- Follow SemVer and say so in the README.
- Keep a changelog with the newest release on top, and add a line with each user-visible change.
- Write breaking changes in a clear section and show how to migrate.
- Tag every release in Git.
Summary
A version number MAJOR.MINOR.PATCH tells users how risky an update is: patch for fixes, minor for new features, major for breaking changes. Compare versions as numbers, and treat a pre-release as older than its release. A changelog explains each release in words for the users.
Quick check
Key takeaways
- In MAJOR.MINOR.PATCH, a breaking change raises the first number, a new feature the second, a fix the third.
- Compare versions as numbers, part by part, never as text.
- A changelog is for users: say what changed for them, not what the code did.
- A pre-release is lower than the release it leads to.
Practice problems for this lesson
Was this lesson clear?
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.