Type Hints, Linting and Formatting
Make your code say what it expects and keep it tidy without arguing about spaces. You'll add type hints to variables and functions, let mypy find the bugs they reveal, and use Ruff to format your code and point out mistakes, in the terminal and every time you save in VS Code. Then you'll tidy a gym's membership code until every check is clean.
Updated
Type Hints in Python
- Type Hints: Saying What a Value Should Be (Apprentice): A type hint is a label on a variable, a parameter or a return value that says what kind of value belongs there, such as float, list[float] or a string that may be None, so people and tools can see what a function expects without reading its body.
- Checking Types with mypy (Journeyman): mypy reads your type hints and every call without running the code, and reports each place where a value can't be what its hint promises, such as text passed as a price or a None that the next line uses as text.
Tidy Code with Ruff
- Linting and Formatting with Ruff (Journeyman): Ruff is a linter and a formatter in one tool. ruff check finds likely mistakes such as unused imports and misspelled names before the code runs, and ruff format lays out every file the same way, with its settings in pyproject.toml and its checks in VS Code as you type.
Exercise: Tidy the Gym Memberships
- Exercise: Tidy the Gym Memberships (Journeyman): Add type hints to a gym's working but messy membership code until mypy passes, fix the bug mypy finds for a plan that doesn't exist, and tidy the file until ruff check and ruff format have nothing left to change, while the tests prove every answer stays the same.
Up Next: Automation
- Up Next: Automation (Journeyman): Look back at what Module 4 taught, tests with pytest, type hints checked by mypy and tidy code from Ruff, try a library fines calculator of your own written test first, and see what Module 5 adds, scripts that sort files, take options, run on a schedule and fetch live data.