|
| 1 | +from typing import Any, cast |
| 2 | + |
| 3 | +from mypy.nodes import CallExpr, Expression |
| 4 | + |
| 5 | +from .argmapper import ArgMapper |
| 6 | +from .test_utils import dump_expr, parse |
| 7 | + |
| 8 | + |
| 9 | +def _named_arg_mapping_test_body(defs: str, expected_keys: list[str]) -> None: |
| 10 | + parse_result = parse(defs) |
| 11 | + parse_result.accept_all() |
| 12 | + |
| 13 | + call = parse_result.defs["call"] |
| 14 | + assert isinstance(call, CallExpr) |
| 15 | + |
| 16 | + raw_arg_map = ArgMapper.named_arg_mapping(call, parse_result.checker) |
| 17 | + |
| 18 | + def dump_arg_map( |
| 19 | + arg_map: dict[str, Expression], |
| 20 | + ) -> dict[str, tuple[type, dict[str, Any]]]: |
| 21 | + return {key: (dump_expr(expr)) for key, expr in arg_map.items()} |
| 22 | + |
| 23 | + assert dump_arg_map(raw_arg_map) == dump_arg_map( |
| 24 | + {key: cast(Expression, parse_result.defs[key]) for key in expected_keys} |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +def test_named_arg_mapping_no_args() -> None: |
| 29 | + _named_arg_mapping_test_body( |
| 30 | + """ |
| 31 | + def main() -> int: |
| 32 | + return 0 |
| 33 | +
|
| 34 | + call = main() |
| 35 | + """, |
| 36 | + [], |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def test_named_arg_mapping_no_named_args() -> None: |
| 41 | + _named_arg_mapping_test_body( |
| 42 | + """ |
| 43 | + def main(x: int, y: str, /, *args: bool) -> int: |
| 44 | + return 0 |
| 45 | +
|
| 46 | + call = main(3, "2", True, False) |
| 47 | + """, |
| 48 | + [], |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def test_named_arg_mapping_named_args_only() -> None: |
| 53 | + _named_arg_mapping_test_body( |
| 54 | + """ |
| 55 | + def main(x: int, y: bool, *, z: str) -> int: |
| 56 | + return 0 |
| 57 | +
|
| 58 | + call = main(0, z="1", y=False) |
| 59 | + x = 0 |
| 60 | + y = False |
| 61 | + z = "1" |
| 62 | + """, |
| 63 | + ["x", "y", "z"], |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def test_named_arg_mapping_arg_mix() -> None: |
| 68 | + _named_arg_mapping_test_body( |
| 69 | + """ |
| 70 | + from typing import Any |
| 71 | +
|
| 72 | + def main(a: int, /, b: bool, *args: Any, c: float, **kwargs: dict) -> int: |
| 73 | + return 0 |
| 74 | +
|
| 75 | + call = main(0, True, 2j, c=3.0, d={}, e=dict()) |
| 76 | + b = True |
| 77 | + c = 3.0 |
| 78 | + """, |
| 79 | + ["b", "c"], |
| 80 | + ) |
| 81 | + |
| 82 | + |
| 83 | +def test_named_arg_mapping_simple_overload() -> None: |
| 84 | + _named_arg_mapping_test_body( |
| 85 | + """ |
| 86 | + from typing import overload |
| 87 | +
|
| 88 | + @overload |
| 89 | + def foo(x: int) -> int: |
| 90 | + ... |
| 91 | +
|
| 92 | + @overload |
| 93 | + def foo(x: str) -> str: |
| 94 | + ... |
| 95 | +
|
| 96 | + def foo(x: str | int) -> str | int: |
| 97 | + return x |
| 98 | +
|
| 99 | + call = foo(x="str") |
| 100 | + x = "str" |
| 101 | + """, |
| 102 | + ["x"], |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +def test_named_arg_mapping_complex_overload() -> None: |
| 107 | + _named_arg_mapping_test_body( |
| 108 | + """ |
| 109 | + from typing import overload |
| 110 | +
|
| 111 | + @overload |
| 112 | + def foo(x: int, y: str, *, z: bool) -> int: |
| 113 | + ... |
| 114 | +
|
| 115 | + @overload |
| 116 | + def foo(x: int, y: str) -> int: |
| 117 | + ... |
| 118 | +
|
| 119 | + def foo(x: int, y: str, **kwargs: bool) -> int: |
| 120 | + return 0 |
| 121 | +
|
| 122 | + call = foo(y="0", z=False, x=2) |
| 123 | + x = 2 |
| 124 | + y = "0" |
| 125 | + """, |
| 126 | + ["x", "y"], |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +def test_named_arg_mapping_varargs_varkwargs_overload() -> None: |
| 131 | + _named_arg_mapping_test_body( |
| 132 | + """ |
| 133 | + from typing import overload |
| 134 | +
|
| 135 | + @overload |
| 136 | + def foo(x: int, y: str, **kwargs: bool) -> int: |
| 137 | + ... |
| 138 | +
|
| 139 | + @overload |
| 140 | + def foo(x: int, y: str, **kwargs: int) -> int: |
| 141 | + ... |
| 142 | +
|
| 143 | + def foo(x: int, y: str, **kwargs: bool | int) -> int: |
| 144 | + return 0 |
| 145 | +
|
| 146 | + call = foo(*(1, "2"), z=True) |
| 147 | + """, |
| 148 | + [], |
| 149 | + ) |
| 150 | + |
| 151 | + |
| 152 | +def test_named_arg_mapping_instance_method() -> None: |
| 153 | + _named_arg_mapping_test_body( |
| 154 | + """ |
| 155 | + from typing import overload |
| 156 | +
|
| 157 | + class Foo: |
| 158 | + def bar(self, x: str, y: str, *args: str) -> str: |
| 159 | + return x + y |
| 160 | +
|
| 161 | + foo = Foo() |
| 162 | + call = foo.bar("a", "b", "c") |
| 163 | + x = "a" |
| 164 | + y = "b" |
| 165 | + """, |
| 166 | + ["x", "y"], |
| 167 | + ) |
| 168 | + |
| 169 | + |
| 170 | +def test_named_arg_mapping_call_method() -> None: |
| 171 | + _named_arg_mapping_test_body( |
| 172 | + """ |
| 173 | + from typing import overload |
| 174 | +
|
| 175 | + class Foo: |
| 176 | + def __call__(self, x: str, y: str, *args: str) -> str: |
| 177 | + return x + y |
| 178 | +
|
| 179 | + foo = Foo() |
| 180 | + call = foo("a", "b", "c") |
| 181 | + x = "a" |
| 182 | + y = "b" |
| 183 | + """, |
| 184 | + ["x", "y"], |
| 185 | + ) |
0 commit comments