
Code
Semantic Versioning
Most packages use semantic versioning: MAJOR.MINOR.PATCH.
| Bump | When | Example |
|---|---|---|
| PATCH | bug fixes only | 1.4.2 to 1.4.3 |
| MINOR | new features, still compatible | 1.4.2 to 1.5.0 |
| MAJOR | breaking changes | 1.4.2 to 2.0.0 |
A bump resets everything to its right to zero. Users can then write
rich>=13,<14 to say "any compatible 13.x release".
The classic trap
Comparing versions as strings goes wrong:
"1.10.0" > "1.9.0" # False! '1' < '9'
(1, 10, 0) > (1, 9, 0) # True: compare numbers
Tuples compare item by item, left to right, which is exactly the rule versions need. So parse versions into tuples of ints.
(Real version strings can also have pre-release tags like 2.0.0rc1.
The packaging library handles every case, but you'll build the core
version here.)
Assignment
- Fix
parse_version(text)so it returns a tuple of 3 ints. Allow a leading"v"("v2.0.1"). RaiseValueErrorunless there are exactly 3 parts and they are all digits. compare_versions(a, b)returns-1,0or1.bump(version, part)returns the new version string forpart="major","minor"or"patch". RaiseValueErrorfor anything else.
- Fix
parse_version(text)so it returns a tuple of 3 ints. Allow a leading"v"("v2.0.1"). RaiseValueErrorunless there are exactly 3 parts and they are all digits. compare_versions(a, b)returns-1,0or1.bump(version, part)returns the new version string forpart="major","minor"or"patch". RaiseValueErrorfor anything else.
main.py Python sleeping
Press Run to try your code, then Check to test it.