|
| 1 | +"""Type compliance tests for callback with strict mypy/pyright settings.""" |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +import pytest # type: ignore |
| 6 | + |
| 7 | +from tests.compliance.test_typing import format_template_and_save, run_module |
| 8 | + |
| 9 | + |
| 10 | +callback_template = """ |
| 11 | +from dash import Dash, html, dcc, callback, Input, Output, State |
| 12 | +
|
| 13 | +app = Dash() |
| 14 | +
|
| 15 | +app.layout = html.Div([ |
| 16 | + dcc.Input(id='input1', value=''), |
| 17 | + dcc.Input(id='input2', value=''), |
| 18 | + html.Button('Click', id='btn'), |
| 19 | + html.Div(id='output1'), |
| 20 | + html.Div(id='output2'), |
| 21 | +]) |
| 22 | +
|
| 23 | +{0} |
| 24 | +""" |
| 25 | + |
| 26 | +strict_mypy_template = """# mypy: disallow-untyped-defs |
| 27 | +# mypy: disallow-untyped-calls |
| 28 | +# mypy: disallow-untyped-decorators |
| 29 | +from dash import Dash, html, dcc, callback, Input, Output, State |
| 30 | +
|
| 31 | +app = Dash(__name__) |
| 32 | +
|
| 33 | +app.layout = html.Div([ |
| 34 | + dcc.Input(id='input', value=''), |
| 35 | + html.Div(id='output'), |
| 36 | +]) |
| 37 | +
|
| 38 | +{0} |
| 39 | +""" |
| 40 | + |
| 41 | + |
| 42 | +valid_callback_single = """ |
| 43 | +@callback(Output('output1', 'children'), Input('input1', 'value')) |
| 44 | +def update_output(value: str) -> str: |
| 45 | + return f"You typed: {value}" |
| 46 | +""" |
| 47 | + |
| 48 | +valid_callback_multi_input = """ |
| 49 | +@callback( |
| 50 | + Output('output1', 'children'), |
| 51 | + Input('input1', 'value'), |
| 52 | + Input('input2', 'value') |
| 53 | +) |
| 54 | +def update_output(val1: str, val2: str) -> str: |
| 55 | + return f"{val1} and {val2}" |
| 56 | +""" |
| 57 | + |
| 58 | +valid_callback_with_state = """ |
| 59 | +@callback( |
| 60 | + Output('output1', 'children'), |
| 61 | + Input('btn', 'n_clicks'), |
| 62 | + State('input1', 'value') |
| 63 | +) |
| 64 | +def update_output(n_clicks: int | None, state_value: str) -> str: |
| 65 | + if n_clicks is None: |
| 66 | + return "Not clicked" |
| 67 | + return f"Clicked {n_clicks} times with {state_value}" |
| 68 | +""" |
| 69 | + |
| 70 | +valid_callback_multi_output = """ |
| 71 | +@callback( |
| 72 | + Output('output1', 'children'), |
| 73 | + Output('output2', 'children'), |
| 74 | + Input('input1', 'value') |
| 75 | +) |
| 76 | +def update_outputs(value: str) -> tuple[str, str]: |
| 77 | + return f"First: {value}", f"Second: {value}" |
| 78 | +""" |
| 79 | + |
| 80 | +strict_mode_callback = """ |
| 81 | +@callback(Output('output', 'children'), Input('input', 'value')) |
| 82 | +def my_callback(value: str) -> str: |
| 83 | + '''Fully typed callback function.''' |
| 84 | + return f"Result: {value}" |
| 85 | +""" |
| 86 | + |
| 87 | +complex_return_types = """ |
| 88 | +from typing import Union |
| 89 | +
|
| 90 | +@callback( |
| 91 | + Output('output1', 'children'), |
| 92 | + Output('output2', 'children'), |
| 93 | + Input('input1', 'value') |
| 94 | +) |
| 95 | +def complex_callback(value: str) -> tuple[Union[str, int], list[str]]: |
| 96 | + return len(value), [value, value.upper()] |
| 97 | +""" |
| 98 | + |
| 99 | + |
| 100 | +typing_modules = ["pyright"] |
| 101 | +if sys.version_info.minor >= 10: |
| 102 | + typing_modules.append("mypy") |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.parametrize("typing_module", typing_modules) |
| 106 | +@pytest.mark.parametrize( |
| 107 | + "callback_code, expected_status", |
| 108 | + [ |
| 109 | + (valid_callback_single, 0), |
| 110 | + (valid_callback_multi_input, 0), |
| 111 | + (valid_callback_with_state, 0), |
| 112 | + (valid_callback_multi_output, 0), |
| 113 | + (complex_return_types, 0), |
| 114 | + ], |
| 115 | +) |
| 116 | +def test_typi_callback_basic(typing_module, callback_code, expected_status, tmp_path): |
| 117 | + """Test that callback passes type checking in normal mode.""" |
| 118 | + codefile = os.path.join(tmp_path, "code.py") |
| 119 | + code = format_template_and_save(callback_template, codefile, callback_code) |
| 120 | + |
| 121 | + output, error, status = run_module(codefile, typing_module) |
| 122 | + assert ( |
| 123 | + status == expected_status |
| 124 | + ), f"Status: {status}\nOutput: {output}\nError: {error}\nCode: {code}\nModule: {typing_module}" |
| 125 | + |
| 126 | + |
| 127 | +@pytest.mark.parametrize("typing_module", typing_modules) |
| 128 | +def test_typi_callback_strict_mode(typing_module, tmp_path): |
| 129 | + """Test that callback works with strict mypy/pyright settings.""" |
| 130 | + codefile = os.path.join(tmp_path, "code.py") |
| 131 | + code = format_template_and_save( |
| 132 | + strict_mypy_template, codefile, strict_mode_callback |
| 133 | + ) |
| 134 | + |
| 135 | + output, error, status = run_module(codefile, typing_module) |
| 136 | + assert status == 0, ( |
| 137 | + f"callback should pass strict type checking.\n" |
| 138 | + f"Status: {status}\nOutput: {output}\nError: {error}\n" |
| 139 | + f"Code: {code}\nModule: {typing_module}" |
| 140 | + ) |
| 141 | + |
| 142 | + |
| 143 | +@pytest.mark.parametrize("typing_module", typing_modules) |
| 144 | +def test_typi_callback_preserves_signature(typing_module, tmp_path): |
| 145 | + """Test that callback preserves function signatures for type inference.""" |
| 146 | + code = """ |
| 147 | +from dash import callback, Input, Output, html, Dash |
| 148 | +
|
| 149 | +app = Dash(__name__) |
| 150 | +app.layout = html.Div([html.Div(id='in'), html.Div(id='out')]) |
| 151 | +
|
| 152 | +@callback(Output('out', 'children'), Input('in', 'children')) |
| 153 | +def my_func(value: str) -> int: |
| 154 | + return len(value) |
| 155 | +
|
| 156 | +# The decorated function should still have its original signature |
| 157 | +result = my_func("test") # Should return int |
| 158 | +""" |
| 159 | + |
| 160 | + codefile = os.path.join(tmp_path, "code.py") |
| 161 | + with open(codefile, "w") as f: |
| 162 | + f.write(code) |
| 163 | + |
| 164 | + output, error, status = run_module(codefile, typing_module) |
| 165 | + |
| 166 | + assert status == 0, ( |
| 167 | + f"callback should preserve function signature.\n" |
| 168 | + f"Status: {status}\nOutput: {output}\nError: {error}\n" |
| 169 | + f"Module: {typing_module}" |
| 170 | + ) |
| 171 | + |
| 172 | + |
| 173 | +@pytest.mark.parametrize("typing_module", typing_modules) |
| 174 | +def test_typi_callback_with_none_values(typing_module, tmp_path): |
| 175 | + """Test callback with Optional types.""" |
| 176 | + code = """ |
| 177 | +from dash import Dash, html, dcc, callback, Input, Output |
| 178 | +
|
| 179 | +app = Dash(__name__) |
| 180 | +app.layout = html.Div([ |
| 181 | + dcc.Input(id='input', value=''), |
| 182 | + html.Button('Click', id='btn'), |
| 183 | + html.Div(id='output'), |
| 184 | +]) |
| 185 | +
|
| 186 | +@callback( |
| 187 | + Output('output', 'children'), |
| 188 | + Input('btn', 'n_clicks') |
| 189 | +) |
| 190 | +def handle_optional(n_clicks: int | None) -> str: |
| 191 | + if n_clicks is None: |
| 192 | + return "Not clicked yet" |
| 193 | + return f"Clicked {n_clicks} times" |
| 194 | +""" |
| 195 | + |
| 196 | + codefile = os.path.join(tmp_path, "code.py") |
| 197 | + with open(codefile, "w") as f: |
| 198 | + f.write(code) |
| 199 | + |
| 200 | + output, error, status = run_module(codefile, typing_module) |
| 201 | + assert status == 0, ( |
| 202 | + f"callback should handle Optional types.\n" |
| 203 | + f"Status: {status}\nOutput: {output}\nError: {error}\n" |
| 204 | + f"Module: {typing_module}" |
| 205 | + ) |
0 commit comments