@@ -149,6 +149,24 @@ def catchall(
149149 test_create_handler (catchall )
150150
151151
152+ @cases (
153+ (("a" ,), "foo: catchall" , "a" ),
154+ (("{foo}a" ,), "foo: catchall" , "{foo}a" ),
155+ (("d0" , "d1" ), "foo: catchall" , "d0" ),
156+ (("d0" , "d1" , "d2" ), "foo: catchall" , "d0" ),
157+ ((":text:" ,), "foo: catchall" , ":text:" ),
158+ )
159+ def test_one_partial_catchall (test_create_handler : TEST_CREATE_HANDLER_TYPE ) -> None :
160+ def catchall (
161+ ctx : str , node : Union [XMLElement , XMLText ]
162+ ) -> Iterator [Tuple [str , Union [XMLElement , XMLText ]]]:
163+ yield (f"{ ctx } : catchall" , node )
164+
165+ partial_handler = partial (catchall , "foo" )
166+
167+ test_create_handler (partial_handler )
168+
169+
152170@cases (
153171 (("a" ,), "0" , "a" ),
154172 (("{foo}a" ,), "1" , "{foo}a" ),
@@ -448,6 +466,20 @@ class Handler:
448466 assert isinstance (items [0 ], Handler )
449467
450468
469+ def test_partial_class_without_subhandler () -> None :
470+ @xml_handle_element ("x" )
471+ class Handler :
472+ def __init__ (self , ctx : str ) -> None :
473+ self .ctx = ctx
474+
475+ partial_handler = partial (Handler , "foo" )
476+ nodes = create_nodes ("x" , "y" )
477+ handler = create_handler (partial_handler )
478+ items = list (handler (nodes [0 ]))
479+ assert len (items ) == 1
480+ assert isinstance (items [0 ], Handler )
481+
482+
451483@pytest .mark .parametrize ("init_mandatory" , [False , True ])
452484@pytest .mark .parametrize ("init_optional" , [False , True ])
453485def test_class_init (init_mandatory : bool , init_optional : bool ) -> None :
@@ -581,6 +613,21 @@ def __init__(self, node: XMLElement, answer: int) -> None:
581613 assert "Add a default value for dataclass fields" not in str (excinfo .value )
582614
583615
616+ def test_partial_class_multiple_mandatory_parameters () -> None :
617+ @xml_handle_element ("x" )
618+ class Handler :
619+ def __init__ (self , before : str , node : XMLElement , after : str ) -> None :
620+ pass
621+
622+ partial_handler = partial (Handler , "before" , after = "after" )
623+ nodes = create_nodes ("x" , "y" )
624+ handler = create_handler (partial_handler )
625+ items = list (handler (nodes [0 ]))
626+
627+ assert len (items ) == 1
628+ assert isinstance (items [0 ], Handler )
629+
630+
584631def test_dataclass_init_two_mandatory_parameters () -> None :
585632 @xml_handle_element ("x" )
586633 @dataclass
@@ -598,6 +645,23 @@ class Handler:
598645 assert "Add a default value for dataclass fields" in str (excinfo .value )
599646
600647
648+ def test_partial_dataclass_two_mandatory_parameters () -> None :
649+ @xml_handle_element ("x" )
650+ @dataclass
651+ class Handler :
652+ before : str
653+ node : XMLElement
654+ after : str
655+
656+ partial_handler = partial (Handler , "before" , after = "after" )
657+ nodes = create_nodes ("x" , "y" )
658+ handler = create_handler (partial_handler )
659+ items = list (handler (nodes [0 ]))
660+
661+ assert len (items ) == 1
662+ assert isinstance (items [0 ], Handler )
663+
664+
601665def test_class_init_crash () -> None :
602666 @xml_handle_element ("x" )
603667 class Handler :
0 commit comments