1496
"""Test cases for get_signature_format_from_schema_params union handling."""
1497
1498
@staticmethod
1499
- def _annotation(schema):
1499
+ def _parameter(schema):
1500
params = get_signature_format_from_schema_params(schema)
1501
assert len(params) == 1
1502
- return params[0].annotation
1502
+ return params[0]
1503
+
1504
+ @classmethod
1505
+ def _annotation(cls, schema):
1506
+ return cls._parameter(schema).annotation
1507
1508
@pytest.mark.unit
1509
@pytest.mark.schema
1635
assert str in args
1636
assert type(None) in args
1637
1638
+ @pytest.mark.unit
1639
+ @pytest.mark.schema
1640
+ @pytest.mark.parametrize(
1641
+ ("schema_type", "expected_members"),
1642
+ [
1643
+ (["string", "null"], {str, type(None)}),
1644
+ (["integer"], {int}),
1645
+ (["string", "integer"], {str, int}),
1646
+ (["file", "null"], {t.Any, type(None)}),
1647
+ ],
1648
+ )
1649
+ def test_type_list_resolves(self, schema_type, expected_members):
1650
+ """List-valued JSON Schema types produce an annotation without raising."""
1651
+ annotation = self._annotation({"properties": {"value": {"type": schema_type}}})
1652
+ actual = set(t.get_args(annotation)) if t.get_args(annotation) else {annotation}
1653
+ assert expected_members <= actual
1654
+
1655
+ @pytest.mark.unit
1656
+ @pytest.mark.schema
1657
+ @pytest.mark.parametrize(
1658
+ ("schema_type", "expected_annotation", "expected_default"),
1659
+ [
1660
+ (["integer"], int, 0),
1661
+ (["boolean"], bool, False),
1662
+ (["array"], t.List, []),
1663
+ ],
1664
+ )
1665
+ def test_single_type_list_uses_scalar_fallback(
1666
+ self, schema_type, expected_annotation, expected_default
1667
+ ):
1668
+ """A one-item type list keeps its scalar type's implicit default."""
1669
+ parameter = self._parameter({"properties": {"value": {"type": schema_type}}})
1670
+
1671
+ assert parameter.annotation is expected_annotation
1672
+ assert parameter.default == expected_default
1673
+
1674
+ @pytest.mark.unit
1675
+ @pytest.mark.schema
1676
+ def test_single_type_list_preserves_explicit_default(self):
1677
+ """An explicit default overrides the fallback for a one-item type list."""
1678
+ parameter = self._parameter(
1679
+ {"properties": {"value": {"type": ["integer"], "default": 42}}}
1680
+ )
1681
+
1682
+ assert parameter.default == 42
1683
+
1684
+ @pytest.mark.unit
1685
+ @pytest.mark.schema
1686
+ def test_multi_type_list_keeps_union_fallback(self):
1687
+ """A multi-type list remains a union with the existing empty-string fallback."""
1688
+ parameter = self._parameter(
1689
+ {"properties": {"value": {"type": ["integer", "null"]}}}
1690
+ )
1691
+
1692
+ assert {int, type(None)} <= set(t.get_args(parameter.annotation))
1693
+ assert parameter.default == ""
1694
+
1695
+ @pytest.mark.unit
1696
+ @pytest.mark.schema
1697
+ def test_combiner_option_with_type_list_resolves(self):
1698
+ """List-valued types inside combiners do not reach a scalar dict lookup."""
1699
+ schema = {
1700
+ "properties": {
1701
+ "value": {
1702
+ "anyOf": [
1703
+ {"type": ["string", "null"]},
1704
+ {"type": "integer"},
1705
+ ]
1706
+ }
1707
+ }
1708
+ }
1709
+
1710
+ annotation = self._annotation(schema)
1711
+ assert {str, int, type(None)} <= set(t.get_args(annotation))
1712
+
1713
1714
if __name__ == "__main__":
1715
pytest.main([__file__, "-v"])