@@ -882,7 +882,7 @@ class MyClass:
882882
883883 obj = MyClass ()
884884 with pytest .raises (
885- ArgumentValidationError , match = "must be one of: 'fast', 'slow', 'auto' "
885+ ArgumentValidationError , match = "must be one of the allowed choices "
886886 ):
887887 _ = obj .mode
888888
@@ -908,7 +908,9 @@ class MyClass:
908908 name = Arg [str ](0 , pattern = r"^[a-z_][a-z0-9_]*$" )
909909
910910 obj = MyClass ()
911- with pytest .raises (ArgumentValidationError , match = "does not match pattern" ):
911+ with pytest .raises (
912+ ArgumentValidationError , match = "does not match the required pattern"
913+ ):
912914 _ = obj .name
913915
914916 def test_pattern_validation_requires_string (self ) -> None :
@@ -919,7 +921,9 @@ class MyClass:
919921 value = Arg [int ](0 , pattern = r".*" )
920922
921923 obj = MyClass ()
922- with pytest .raises (ArgumentValidationError , match = "requires string" ):
924+ with pytest .raises (
925+ ArgumentValidationError , match = "must be a string for pattern validation"
926+ ):
923927 _ = obj .value
924928
925929 def test_conflicting_ge_gt_raises (self ) -> None :
@@ -951,3 +955,173 @@ def test_repr_with_validation(self) -> None:
951955 assert "le=100" in repr_str
952956 assert "choices=" in repr_str
953957 assert "pattern=" in repr_str
958+
959+
960+ class TestArgumentValidationErrorMessages :
961+ """Tests for rich error messages in ArgumentValidationError."""
962+
963+ def test_error_includes_argument_info (self ) -> None :
964+ """Error should include argument position and attribute name."""
965+
966+ class MyClass :
967+ invocation = _MockInvocation (Arguments (positional = (pa .scalar (0 ),)))
968+ count = Arg [int ](0 , ge = 1 , doc = "Number of items to process" )
969+
970+ obj = MyClass ()
971+ with pytest .raises (ArgumentValidationError ) as exc_info :
972+ _ = obj .count
973+
974+ error = exc_info .value
975+ error_str = str (error )
976+
977+ # Check rich attributes
978+ assert error .arg_name == "count"
979+ assert error .position == 0
980+ assert error .value == 0
981+ assert error .constraint == "must be >= 1"
982+ assert error .doc == "Number of items to process"
983+
984+ # Check formatted message includes all info
985+ assert "positional argument 0" in error_str
986+ assert "self.count" in error_str
987+ assert "Value: 0" in error_str
988+ assert "must be >= 1" in error_str
989+ assert "Number of items to process" in error_str
990+
991+ def test_error_includes_valid_range (self ) -> None :
992+ """Error should include valid range description."""
993+
994+ class MyClass :
995+ invocation = _MockInvocation (Arguments (positional = (pa .scalar (200 ),)))
996+ count = Arg [int ](0 , ge = 1 , le = 100 )
997+
998+ obj = MyClass ()
999+ with pytest .raises (ArgumentValidationError ) as exc_info :
1000+ _ = obj .count
1001+
1002+ error = exc_info .value
1003+ assert error .valid_range == ">= 1 and <= 100"
1004+ assert ">= 1 and <= 100" in str (error )
1005+
1006+ def test_error_includes_default_hint (self ) -> None :
1007+ """Error should suggest using default value if available."""
1008+
1009+ class MyClass :
1010+ invocation = _MockInvocation (Arguments (positional = (pa .scalar (- 5 ),)))
1011+ count = Arg [int ](0 , ge = 0 , default = 10 )
1012+
1013+ obj = MyClass ()
1014+ with pytest .raises (ArgumentValidationError ) as exc_info :
1015+ _ = obj .count
1016+
1017+ error_str = str (exc_info .value )
1018+ assert "default value: 10" in error_str
1019+
1020+ def test_error_suggests_similar_choices (self ) -> None :
1021+ """Error should suggest similar choices for typos."""
1022+
1023+ class MyClass :
1024+ invocation = _MockInvocation (Arguments (positional = (pa .scalar ("fst" ),)))
1025+ mode = Arg [str ](0 , choices = ["fast" , "slow" , "auto" ])
1026+
1027+ obj = MyClass ()
1028+ with pytest .raises (ArgumentValidationError ) as exc_info :
1029+ _ = obj .mode
1030+
1031+ error = exc_info .value
1032+ error_str = str (error )
1033+
1034+ # Should suggest 'fast' as it's similar to 'fst'
1035+ assert "Did you mean:" in error_str
1036+ assert "'fast'" in error_str
1037+
1038+ def test_error_for_named_argument (self ) -> None :
1039+ """Error should format named arguments correctly."""
1040+
1041+ class MyClass :
1042+ invocation = _MockInvocation (
1043+ Arguments (named = {"threshold" : pa .scalar (- 1.0 )})
1044+ )
1045+ threshold = Arg [float ]("threshold" , ge = 0.0 , le = 1.0 )
1046+
1047+ obj = MyClass ()
1048+ with pytest .raises (ArgumentValidationError ) as exc_info :
1049+ _ = obj .threshold
1050+
1051+ error = exc_info .value
1052+ error_str = str (error )
1053+
1054+ assert error .position == "threshold"
1055+ assert "named argument 'threshold'" in error_str
1056+ assert "self.threshold" in error_str
1057+
1058+ def test_error_with_doc_shows_purpose (self ) -> None :
1059+ """Error should show documentation explaining the argument's purpose."""
1060+
1061+ class MyClass :
1062+ invocation = _MockInvocation (Arguments (positional = (pa .scalar ("xyz" ),)))
1063+ mode = Arg [str ](
1064+ 0 ,
1065+ choices = ["json" , "csv" , "parquet" ],
1066+ doc = "Output format for exported data" ,
1067+ )
1068+
1069+ obj = MyClass ()
1070+ with pytest .raises (ArgumentValidationError ) as exc_info :
1071+ _ = obj .mode
1072+
1073+ error_str = str (exc_info .value )
1074+ assert "Purpose: Output format for exported data" in error_str
1075+
1076+ def test_choices_truncated_for_many_options (self ) -> None :
1077+ """Valid range should truncate long choice lists."""
1078+
1079+ class MyClass :
1080+ invocation = _MockInvocation (Arguments (positional = (pa .scalar ("invalid" ),)))
1081+ mode = Arg [str ](0 , choices = ["a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" ])
1082+
1083+ obj = MyClass ()
1084+ with pytest .raises (ArgumentValidationError ) as exc_info :
1085+ _ = obj .mode
1086+
1087+ error = exc_info .value
1088+ assert error .valid_range is not None
1089+ assert "8 total options" in error .valid_range
1090+
1091+ def test_pattern_validation_shows_pattern (self ) -> None :
1092+ """Pattern validation error should show the expected pattern."""
1093+
1094+ class MyClass :
1095+ invocation = _MockInvocation (
1096+ Arguments (positional = (pa .scalar ("123abc" ),))
1097+ )
1098+ name = Arg [str ](0 , pattern = r"^[a-z][a-z0-9]*$" , doc = "Variable name" )
1099+
1100+ obj = MyClass ()
1101+ with pytest .raises (ArgumentValidationError ) as exc_info :
1102+ _ = obj .name
1103+
1104+ error = exc_info .value
1105+ error_str = str (error )
1106+
1107+ assert "must match pattern" in error .constraint # type: ignore[operator]
1108+ assert "^[a-z][a-z0-9]*$" in error_str
1109+ assert "Variable name" in error_str
1110+
1111+ def test_numeric_choices_suggests_closest (self ) -> None :
1112+ """For numeric choices, should suggest closest values."""
1113+
1114+ class MyClass :
1115+ invocation = _MockInvocation (Arguments (positional = (pa .scalar (15 ),)))
1116+ size = Arg [int ](0 , choices = [8 , 16 , 32 , 64 , 128 ])
1117+
1118+ obj = MyClass ()
1119+ with pytest .raises (ArgumentValidationError ) as exc_info :
1120+ _ = obj .size
1121+
1122+ error = exc_info .value
1123+ suggestions = error ._suggest_similar_choices ()
1124+
1125+ # 16 is closest to 15, then 8
1126+ assert suggestions [0 ] == 16
1127+ assert suggestions [1 ] == 8
0 commit comments