|
1 | 1 | import sys |
2 | 2 | import unittest |
3 | 3 | from typing import Annotated |
4 | | -from tap import Tap, TapIgnore |
| 4 | +from tap import Tap, TapIgnore, tapify |
| 5 | +from dataclasses import dataclass |
| 6 | + |
| 7 | + |
| 8 | +try: |
| 9 | + import pydantic |
| 10 | +except ModuleNotFoundError: |
| 11 | + _IS_PYDANTIC_V1 = None |
| 12 | +else: |
| 13 | + _IS_PYDANTIC_V1 = pydantic.VERSION.startswith("1.") |
5 | 14 |
|
6 | 15 |
|
7 | 16 | # Suppress prints from SystemExit |
@@ -262,6 +271,68 @@ def configure(self): |
262 | 271 | args = Args().parse_args(["--a", "1"]) |
263 | 272 | self.assertEqual(args.b, 3) |
264 | 273 |
|
| 274 | + def test_tapify_function_with_tap_ignore(self): |
| 275 | + """Test that tapify handles TapIgnore annotations on function arguments.""" |
| 276 | + |
| 277 | + def my_func(a: int, b: TapIgnore[int] = 2, c: str = "hello") -> str: |
| 278 | + return f"{a} {b} {c}" |
| 279 | + |
| 280 | + # b is ignored, so it shouldn't be parsed from CLI - should use default |
| 281 | + output = tapify(my_func, command_line_args=["--a", "1", "--c", "world"]) |
| 282 | + self.assertEqual(output, "1 2 world") |
| 283 | + |
| 284 | + # Passing --b should fail because it's not a recognized argument |
| 285 | + with self.assertRaises(SystemExit): |
| 286 | + tapify(my_func, command_line_args=["--a", "1", "--b", "99", "--c", "world"]) |
| 287 | + |
| 288 | + def test_tapify_function_with_tap_ignore_known_only(self): |
| 289 | + """Test tapify with TapIgnore and known_only=True.""" |
| 290 | + |
| 291 | + def my_func(a: int, b: TapIgnore[int] = 2, c: str = "hello") -> str: |
| 292 | + return f"{a} {b} {c}" |
| 293 | + |
| 294 | + # With known_only=True, --b should be ignored (not cause an error) |
| 295 | + output = tapify( |
| 296 | + my_func, |
| 297 | + command_line_args=["--a", "1", "--b", "99", "--c", "world"], |
| 298 | + known_only=True |
| 299 | + ) |
| 300 | + # b should still be 2 (the default), not 99 |
| 301 | + self.assertEqual(output, "1 2 world") |
| 302 | + |
| 303 | + def test_tapify_class_with_tap_ignore(self): |
| 304 | + """Test that tapify handles TapIgnore annotations on class __init__ arguments.""" |
| 305 | + |
| 306 | + class MyClass: |
| 307 | + def __init__(self, a: int, b: TapIgnore[int] = 2, c: str = "hello"): |
| 308 | + self.result = f"{a} {b} {c}" |
| 309 | + |
| 310 | + # Passing --b should fail because it's not a recognized argument |
| 311 | + with self.assertRaises(SystemExit): |
| 312 | + tapify(MyClass, command_line_args=["--a", "1", "--b", "99", "--c", "world"]) |
| 313 | + |
| 314 | + # test with known_only |
| 315 | + myclass = tapify(MyClass, known_only=True, command_line_args=["--a", "1", "--b", "99", "--c", "world"]) |
| 316 | + # b should still be 2 (the default), not 99 |
| 317 | + self.assertEqual(myclass.result, "1 2 world") |
| 318 | + |
| 319 | + def test_tapify_ignore_dataclass(self): |
| 320 | + @dataclass |
| 321 | + class DataclassConfig: |
| 322 | + x: int |
| 323 | + y: TapIgnore[str] = "ignore_me" |
| 324 | + |
| 325 | + class PydanticModel(pydantic.BaseModel): |
| 326 | + x: int |
| 327 | + y: TapIgnore[str] = "ignore_me" |
| 328 | + |
| 329 | + for struct_cls in (DataclassConfig, PydanticModel): |
| 330 | + with self.subTest(struct_cls=struct_cls.__name__): |
| 331 | + model = tapify(struct_cls, command_line_args=["--x", "20"]) |
| 332 | + self.assertEqual(model.x, 20) |
| 333 | + self.assertEqual(model.y, "ignore_me") |
| 334 | + with self.assertRaises(SystemExit): |
| 335 | + tapify(struct_cls, command_line_args=["--x", "10", "--y", "should_fail"]) |
265 | 336 |
|
266 | 337 | if __name__ == "__main__": |
267 | 338 | unittest.main() |
0 commit comments