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#

  1. Open your assignment 4 repository in VS Code

  2. Look for all sections that include “ty” in the current version of the [templates’ pyproject.toml file] (OpenSourceEconomics/econ-project-templates)

  3. Copy them over.

  4. Create a new branch for this work: git checkout -b type-hints

    As always, commit frequently!

Part 1: Adding Type Hints to the Data Management Code#

  1. Scare yourself by running

    pixi run ty check src/assignment_4/data_management
    
  2. Go through your data management modules and add type hints to all functions:

    • Function parameters

    • Return types

    • Use pd.DataFrame for pandas DataFrames

    • Use Path for 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#

  1. Run ty on each step of your analysis and fix the errors

  2. Make sure you caught everything by running pixi run ty check

  3. Run your pytask build to ensure everything still works: pixi run pytask