|
| 1 | +"""Test type compatibility issues that might occur in client code.""" |
| 2 | + |
| 3 | +from typing import Sequence |
| 4 | + |
| 5 | +import pandas as pd |
| 6 | +import polars as pl |
| 7 | + |
| 8 | +from daffy import df_in, df_out |
| 9 | + |
| 10 | + |
| 11 | +# Pass-through function for testing |
| 12 | +@df_in(columns=["Brand", "Price"]) |
| 13 | +def simple_list_columns(df: pd.DataFrame) -> pd.DataFrame: |
| 14 | + return df |
| 15 | + |
| 16 | + |
| 17 | +def test_simple_list_columns() -> None: |
| 18 | + """Test with a simple list of string columns.""" |
| 19 | + df = pd.DataFrame({"Brand": ["Toyota"], "Price": [25000]}) |
| 20 | + result = simple_list_columns(df) |
| 21 | + assert isinstance(result, pd.DataFrame) |
| 22 | + |
| 23 | + |
| 24 | +# This would test the Union type DataFrameType compatibility |
| 25 | +@df_out(columns=["Brand", "Price"]) |
| 26 | +def return_dataframe() -> pd.DataFrame: |
| 27 | + return pd.DataFrame({"Brand": ["Toyota"], "Price": [25000]}) |
| 28 | + |
| 29 | + |
| 30 | +def function_with_explicit_type_annotations(columns: Sequence[str]) -> None: |
| 31 | + @df_in(columns=columns) |
| 32 | + def inner_function(df: pd.DataFrame) -> pd.DataFrame: |
| 33 | + return df |
| 34 | + |
| 35 | + df = pd.DataFrame({"Brand": ["Toyota"], "Price": [25000]}) |
| 36 | + inner_function(df) |
| 37 | + |
| 38 | + |
| 39 | +def test_with_polars() -> None: |
| 40 | + df = pl.DataFrame({"Brand": ["Toyota"], "Price": [25000]}) |
| 41 | + |
| 42 | + @df_in(columns=["Brand", "Price"]) |
| 43 | + def inner_function(df_param: pl.DataFrame) -> pl.DataFrame: |
| 44 | + return df_param |
| 45 | + |
| 46 | + inner_function(df) |
| 47 | + |
| 48 | + |
| 49 | +def test_function_with_explicit_type_annotations() -> None: |
| 50 | + columns = ["Brand", "Price"] |
| 51 | + function_with_explicit_type_annotations(columns) |
| 52 | + |
| 53 | + |
| 54 | +def test_simple_list_columns_function() -> None: |
| 55 | + df = pd.DataFrame({"Brand": ["Toyota"], "Price": [25000]}) |
| 56 | + simple_list_columns(df) |
| 57 | + |
| 58 | + |
| 59 | +def test_return_dataframe_function() -> None: |
| 60 | + result = return_dataframe() |
| 61 | + assert isinstance(result, pd.DataFrame) |
| 62 | + |
| 63 | + |
| 64 | +def test_dtype_with_regex_pandas() -> None: |
| 65 | + """Test using both dtype validation and regex patterns with pandas.""" |
| 66 | + # Create a DataFrame with numeric columns following a pattern |
| 67 | + df = pd.DataFrame( |
| 68 | + { |
| 69 | + "measure_2020": [10, 20, 30], |
| 70 | + "measure_2021": [15, 25, 35], |
| 71 | + "measure_2022": [18, 28, 38], |
| 72 | + "category": ["A", "B", "C"], |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + # Define a function using both regex patterns and dtype validation |
| 77 | + @df_in( |
| 78 | + columns={ |
| 79 | + "category": "object", |
| 80 | + "r/measure_\\d{4}/": "int64", # All measure_YYYY columns should be int64 |
| 81 | + } |
| 82 | + ) |
| 83 | + def process_measures(data: pd.DataFrame) -> pd.DataFrame: |
| 84 | + return data |
| 85 | + |
| 86 | + # This should pass type checking and runtime validation |
| 87 | + result = process_measures(df) |
| 88 | + assert "measure_2020" in result.columns |
| 89 | + assert "measure_2021" in result.columns |
| 90 | + assert "measure_2022" in result.columns |
| 91 | + |
| 92 | + |
| 93 | +def test_dtype_with_regex_polars() -> None: |
| 94 | + """Test using both dtype validation and regex patterns with polars.""" |
| 95 | + # Create a Polars DataFrame with numeric columns following a pattern |
| 96 | + df = pl.DataFrame( |
| 97 | + { |
| 98 | + "measure_2020": [10, 20, 30], |
| 99 | + "measure_2021": [15, 25, 35], |
| 100 | + "measure_2022": [18, 28, 38], |
| 101 | + "category": ["A", "B", "C"], |
| 102 | + } |
| 103 | + ) |
| 104 | + |
| 105 | + # Define a function using both regex patterns and dtype validation |
| 106 | + @df_in( |
| 107 | + columns={ |
| 108 | + "category": pl.String, |
| 109 | + "r/measure_\\d{4}/": pl.Int64, # All measure_YYYY columns should be Int64 |
| 110 | + } |
| 111 | + ) |
| 112 | + def process_measures(data: pl.DataFrame) -> pl.DataFrame: |
| 113 | + return data |
| 114 | + |
| 115 | + # This should pass type checking and runtime validation |
| 116 | + result = process_measures(df) |
| 117 | + assert "measure_2020" in result.columns |
| 118 | + assert "measure_2021" in result.columns |
| 119 | + assert "measure_2022" in result.columns |
| 120 | + |
| 121 | + |
| 122 | +def test_type_narrowing_with_df_out_pandas() -> None: |
| 123 | + """Test assigning df_out decorated function result to a specific Pandas DataFrame type.""" |
| 124 | + |
| 125 | + # Define a function that returns a DataFrame with df_out decoration |
| 126 | + @df_out(columns=["name", "value"]) |
| 127 | + def get_data() -> pd.DataFrame: |
| 128 | + return pd.DataFrame({"name": ["A", "B", "C"], "value": [1, 2, 3]}) |
| 129 | + |
| 130 | + # The critical test: we should be able to assign the result to a variable |
| 131 | + # explicitly typed as pd.DataFrame without mypy errors |
| 132 | + result: pd.DataFrame = get_data() |
| 133 | + assert "name" in result.columns |
| 134 | + assert "value" in result.columns |
| 135 | + |
| 136 | + |
| 137 | +def test_type_narrowing_with_df_out_polars() -> None: |
| 138 | + """Test assigning df_out decorated function result to a specific Polars DataFrame type.""" |
| 139 | + |
| 140 | + # Define a function that returns a DataFrame with df_out decoration |
| 141 | + @df_out(columns=["name", "value"]) |
| 142 | + def get_data() -> pl.DataFrame: |
| 143 | + return pl.DataFrame({"name": ["A", "B", "C"], "value": [1, 2, 3]}) |
| 144 | + |
| 145 | + # The critical test: we should be able to assign the result to a variable |
| 146 | + # explicitly typed as pl.DataFrame without mypy errors |
| 147 | + result: pl.DataFrame = get_data() |
| 148 | + assert "name" in result.columns |
| 149 | + assert "value" in result.columns |
| 150 | + |
| 151 | + |
| 152 | +def test_df_out_preserves_specific_return_type() -> None: |
| 153 | + """Test that df_out preserves the specific DataFrame return type annotation.""" |
| 154 | + |
| 155 | + # Function that specifically returns pandas DataFrame with df_out |
| 156 | + @df_out(columns=["col1", "col2"]) |
| 157 | + def function_with_pandas_df() -> pd.DataFrame: |
| 158 | + return pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}) |
| 159 | + |
| 160 | + # We should be able to assign to a variable typed as pandas DataFrame |
| 161 | + # without having to cast or getting type errors |
| 162 | + result: pd.DataFrame = function_with_pandas_df() |
| 163 | + |
| 164 | + # Same with a function returning polars DataFrame |
| 165 | + @df_out(columns=["col1", "col2"]) |
| 166 | + def function_with_polars_df() -> pl.DataFrame: |
| 167 | + return pl.DataFrame({"col1": [1, 2], "col2": [3, 4]}) |
| 168 | + |
| 169 | + # Should be assignable to a variable typed as polars DataFrame |
| 170 | + polars_result: pl.DataFrame = function_with_polars_df() |
| 171 | + |
| 172 | + # Both should work at runtime too |
| 173 | + assert isinstance(result, pd.DataFrame) |
| 174 | + assert isinstance(polars_result, pl.DataFrame) |
0 commit comments