@@ -651,6 +651,60 @@ class MyClass:
651651 with pytest .raises (ArgumentValidationError , match = "does not match the required pattern" ):
652652 _ = obj .name
653653
654+ def test_pattern_is_unanchored_search (self ) -> None :
655+ """An unanchored pattern matches anywhere in the value (JSON Schema semantics)."""
656+
657+ class MyClass :
658+ invocation = _MockInvocation (Arguments (positional = (pa .scalar ("xhttp://a" ),)))
659+ url = Arg [str ](0 , pattern = r"https?://" )
660+
661+ assert MyClass ().url == "xhttp://a"
662+
663+ def test_pattern_start_anchor_rejects_prefix (self ) -> None :
664+ """A ``^``-anchored pattern still rejects a value with a leading prefix."""
665+
666+ class MyClass :
667+ invocation = _MockInvocation (Arguments (positional = (pa .scalar ("xhttp://a" ),)))
668+ url = Arg [str ](0 , pattern = r"^https?://" )
669+
670+ with pytest .raises (ArgumentValidationError , match = "does not match the required pattern" ):
671+ _ = MyClass ().url
672+
673+ def test_pattern_start_anchor_accepts_match (self ) -> None :
674+ """A ``^``-anchored pattern accepts a value that starts with the match."""
675+
676+ class MyClass :
677+ invocation = _MockInvocation (Arguments (positional = (pa .scalar ("https://a" ),)))
678+ url = Arg [str ](0 , pattern = r"^https?://" )
679+
680+ assert MyClass ().url == "https://a"
681+
682+ def test_pattern_end_anchor_enforced (self ) -> None :
683+ """A ``$`` anchor is honoured: trailing text after the match is rejected."""
684+
685+ class MyClass :
686+ invocation = _MockInvocation (Arguments (positional = (pa .scalar ("ab1" ),)))
687+ code = Arg [str ](0 , pattern = r"^[a-z]+$" )
688+
689+ with pytest .raises (ArgumentValidationError , match = "does not match the required pattern" ):
690+ _ = MyClass ().code
691+
692+ def test_pattern_search_on_named_arg (self ) -> None :
693+ """Named arguments use the same unanchored search."""
694+
695+ class MyClass :
696+ invocation = _MockInvocation (Arguments (positional = (), named = {"url" : pa .scalar ("xhttp://a" )}))
697+ url = Arg [str ]("url" , pattern = r"https?://" )
698+
699+ assert MyClass ().url == "xhttp://a"
700+
701+ class Anchored :
702+ invocation = _MockInvocation (Arguments (positional = (), named = {"url" : pa .scalar ("xhttp://a" )}))
703+ url = Arg [str ]("url" , pattern = r"^https?://" )
704+
705+ with pytest .raises (ArgumentValidationError , match = "does not match the required pattern" ):
706+ _ = Anchored ().url
707+
654708 def test_pattern_validation_requires_string (self ) -> None :
655709 """Arg pattern validation should fail for non-string types."""
656710
@@ -1001,6 +1055,22 @@ class MyClass:
10011055 with pytest .raises (ArgumentValidationError , match = "element 1.*does not match pattern" ):
10021056 _ = obj .values
10031057
1058+ def test_varargs_pattern_is_unanchored_search (self ) -> None :
1059+ """Varargs elements use the same unanchored search as scalar args."""
1060+
1061+ class MyClass :
1062+ invocation = _MockInvocation (Arguments (positional = (pa .scalar ("xhttp://a" ), pa .scalar ("https://b" ))))
1063+ values = Arg [str ](0 , varargs = True , pattern = r"https?://" , arrow_type = pa .string ())
1064+
1065+ assert MyClass ().values == ("xhttp://a" , "https://b" ) # type: ignore[comparison-overlap]
1066+
1067+ class Anchored :
1068+ invocation = _MockInvocation (Arguments (positional = (pa .scalar ("https://b" ), pa .scalar ("xhttp://a" ))))
1069+ values = Arg [str ](0 , varargs = True , pattern = r"^https?://" , arrow_type = pa .string ())
1070+
1071+ with pytest .raises (ArgumentValidationError , match = "element 1.*does not match pattern" ):
1072+ _ = Anchored ().values
1073+
10041074 def test_varargs_must_be_positional (self ) -> None :
10051075 """Varargs with named argument should raise ValueError at definition."""
10061076 with pytest .raises (ValueError , match = "varargs=True requires a positional argument" ):
0 commit comments