Skip to content

Commit 8c9b27f

Browse files
committed
Tighten comments in unexpected-keyword-argument error path
1 parent 0d2cdd2 commit 8c9b27f

2 files changed

Lines changed: 9 additions & 24 deletions

File tree

‎src/runtime/MethodBinder.cs‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,9 +1016,7 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
10161016
// If we already have an exception pending, don't create a new one
10171017
if (!Exceptions.ErrorOccurred())
10181018
{
1019-
// A keyword argument whose name no candidate overload accepts gets the
1020-
// Python-native "unexpected keyword argument" error: the generic no-match
1021-
// message below does not echo kwargs, leaving the actual mistake invisible.
1019+
// Unknown kwarg names get the Python-style error; the generic message below does not echo kwargs.
10221020
if (TryRaiseUnexpectedKeywordArgumentError(kw, info, methodinfo))
10231021
{
10241022
return default;
@@ -1132,11 +1130,8 @@ internal virtual NewReference Invoke(BorrowedReference inst, BorrowedReference a
11321130
}
11331131

11341132
/// <summary>
1135-
/// When a bind failure involves a keyword argument whose name no candidate overload
1136-
/// accepts, raises the Python-style "got an unexpected keyword argument" TypeError
1137-
/// (with a "Did you mean" hint when a similarly-named parameter exists) and returns
1138-
/// true. Returns false when every kwarg name is accepted by at least one overload,
1139-
/// so the generic no-match error is raised instead.
1133+
/// Raises "got an unexpected keyword argument" and returns true when a kwarg name is
1134+
/// accepted by no candidate overload; returns false to let the generic no-match error be raised.
11401135
/// </summary>
11411136
private bool TryRaiseUnexpectedKeywordArgumentError(BorrowedReference kw, MethodBase info, MethodInfo[] methodinfo)
11421137
{
@@ -1146,9 +1141,7 @@ private bool TryRaiseUnexpectedKeywordArgumentError(BorrowedReference kw, Method
11461141
return false;
11471142
}
11481143

1149-
// The same candidate set Bind considered: parameter names are snake_case for
1150-
// snake_case-registered methods and original for the original ones, matching
1151-
// the names the caller can actually use.
1144+
// Same candidate set Bind considered; ParameterNames are already in the caller's convention.
11521145
var methods = info == null
11531146
? GetMethods()
11541147
: new List<MethodInformation>(1) { new MethodInformation(info, true) };
@@ -1206,10 +1199,8 @@ private bool TryRaiseUnexpectedKeywordArgumentError(BorrowedReference kw, Method
12061199
}
12071200

12081201
/// <summary>
1209-
/// The candidate parameter name closest to the unexpected kwarg name, or null when
1210-
/// none is similar enough to suggest. A candidate is considered when it is within
1211-
/// a small edit distance of the name, or when one contains the other (e.g. 'as_tag'
1212-
/// suggests 'tag'); containment requires 3+ characters so tiny names don't match.
1202+
/// Closest parameter name to suggest, or null: small edit distance, or containment
1203+
/// between names of 3+ characters (e.g. 'as_tag' suggests 'tag').
12131204
/// </summary>
12141205
private static string ClosestParameterName(string name, HashSet<string> parameterNames)
12151206
{
@@ -1234,7 +1225,7 @@ private static string ClosestParameterName(string name, HashSet<string> paramete
12341225
return best;
12351226
}
12361227

1237-
// Case-insensitive Levenshtein distance, local to keyword suggestions.
1228+
// Case-insensitive Levenshtein distance.
12381229
private static int KeywordEditDistance(string a, string b)
12391230
{
12401231
a = a.ToLowerInvariant();

‎tests/test_method.py‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,16 +1105,13 @@ def test_default_params():
11051105
MethodTest.DefaultParams(1,2,3,4,5)
11061106

11071107
def test_unexpected_keyword_argument_with_suggestion():
1108-
# A kwarg no overload accepts raises the Python-style error naming the kwarg,
1109-
# with a did-you-mean hint when a similarly-named parameter exists.
11101108
with pytest.raises(TypeError) as excinfo:
11111109
MethodTest.order_like_method("SPY", 10, as_tag="EmergencyFlatten")
11121110
message = str(excinfo.value)
11131111
assert "order_like_method() got an unexpected keyword argument 'as_tag'" in message
11141112
assert "Did you mean 'tag'?" in message
11151113

1116-
# Same behavior when calling through the original PascalCase name; parameter
1117-
# names are the original ones there.
1114+
# PascalCase call path: parameter names are the original ones.
11181115
with pytest.raises(TypeError) as excinfo:
11191116
MethodTest.OrderLikeMethod("SPY", 10, asTag="EmergencyFlatten")
11201117
message = str(excinfo.value)
@@ -1123,7 +1120,6 @@ def test_unexpected_keyword_argument_with_suggestion():
11231120

11241121

11251122
def test_unexpected_keyword_argument_without_suggestion():
1126-
# No parameter is remotely similar: the kwarg is still named, but no hint is added.
11271123
with pytest.raises(TypeError) as excinfo:
11281124
MethodTest.order_like_method("SPY", 10, completely_unrelated_name=1)
11291125
message = str(excinfo.value)
@@ -1144,9 +1140,7 @@ def test_valid_keyword_arguments_still_bind():
11441140

11451141

11461142
def test_valid_keyword_argument_names_keep_no_match_message():
1147-
# All kwarg names are real parameters, but the call still cannot bind ('d' is
1148-
# supplied both positionally and by name): the classic no-method-matches
1149-
# message must be preserved for this case.
1143+
# 'd' is supplied both positionally and by name: valid names, unbindable call.
11501144
with pytest.raises(TypeError) as excinfo:
11511145
MethodTest.DefaultParams(1, 2, 3, 4, d=5)
11521146
assert "No method matches given arguments for default_params" in str(excinfo.value)

0 commit comments

Comments
 (0)