| 561 | |
| 562 | class TestStructInit: |
| 563 | def test_init_positional(self): |
| 564 | class Test(Struct): |
| 565 | a: int |
| 566 | b: float |
| 567 | c: int = 3 |
| 568 | d: float = 4.0 |
| 569 | |
| 570 | assert as_tuple(Test(1, 2.0)) == (1, 2.0, 3, 4.0) |
| 571 | assert as_tuple(Test(1, b=2.0)) == (1, 2.0, 3, 4.0) |
| 572 | assert as_tuple(Test(a=1, b=2.0)) == (1, 2.0, 3, 4.0) |
| 573 | assert as_tuple(Test(1, b=2.0, c=5)) == (1, 2.0, 5, 4.0) |
| 574 | assert as_tuple(Test(1, b=2.0, d=5.0)) == (1, 2.0, 3, 5.0) |
| 575 | assert as_tuple(Test(1, 2.0, 5)) == (1, 2.0, 5, 4.0) |
| 576 | assert as_tuple(Test(1, 2.0, 5, 6.0)) == (1, 2.0, 5, 6.0) |
| 577 | |
| 578 | with pytest.raises(TypeError, match="Missing required argument 'a'"): |
| 579 | Test() |
| 580 | |
| 581 | with pytest.raises(TypeError, match="Missing required argument 'b'"): |
| 582 | Test(1) |
| 583 | |
| 584 | with pytest.raises(TypeError, match="Extra positional arguments provided"): |
| 585 | Test(1, 2, 3, 4, 5) |
| 586 | |
| 587 | with pytest.raises(TypeError, match="Argument 'a' given by name and position"): |
| 588 | Test(1, 2, a=3) |
| 589 | |
| 590 | with pytest.raises(TypeError, match="Unexpected keyword argument 'e'"): |
| 591 | Test(1, 2, e=5) |
| 592 | |
| 593 | def test_init_kw_only(self): |
| 594 | class Test(Struct, kw_only=True): |