In-class exercise#
In this session, you will add type hints to your assignment 4 code. This exercise reinforces the preparation materials and gives you hands-on experience with type annotations in a real codebase you’ve already written.
Setup#
Open your assignment 4 repository in VS Code
Look for all sections that include “ty” in the current version of the [templates’
pyproject.tomlfile] (OpenSourceEconomics/econ-project-templates)Copy them over.
Create a new branch for this work:
git checkout -b type-hintsAs always, commit frequently!
Part 1: Adding Type Hints to the Data Management Code#
Scare yourself by running
pixi run ty check src/assignment_4/data_management
Go through your data management modules and add type hints to all functions:
Function parameters
Return types
Use
pd.DataFramefor pandas DataFramesUse
Pathfor file paths
Example:
import pandas as pd from pathlib import Path def load_data(path: Path) -> pd.DataFrame: return pd.read_csv(path) def clean_data(df: pd.DataFrame) -> pd.DataFrame: # ... cleaning logic return df
Part 2: Continue with the remaining components#
Run ty on each step of your analysis and fix the errors
Make sure you caught everything by running
pixi run ty checkRun your pytask build to ensure everything still works:
pixi run pytask