Skip to content

Commit 8cd51a2

Browse files
chore: loosen const and integer coercion rules (#121)
1 parent 980ab0c commit 8cd51a2

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

lib/openai/internal/type/converter.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ def coerce(
149149
if value.is_a?(Integer)
150150
exactness[:yes] += 1
151151
return value
152-
elsif strictness == :strong
152+
elsif strictness == :strong && Integer(value, exception: false) != value
153153
message = "no implicit conversion of #{value.class} into #{target.inspect}"
154-
raise TypeError.new(message)
154+
raise value.is_a?(Numeric) ? ArgumentError.new(message) : TypeError.new(message)
155155
else
156156
Kernel.then do
157157
return Integer(value).tap { exactness[:maybe] += 1 }
@@ -197,12 +197,20 @@ def coerce(
197197
else
198198
end
199199
in Symbol
200-
if (value.is_a?(Symbol) || value.is_a?(String)) && value.to_sym == target
201-
exactness[:yes] += 1
202-
return target
203-
elsif strictness == :strong
204-
message = "cannot convert non-matching #{value.class} into #{target.inspect}"
205-
raise ArgumentError.new(message)
200+
case value
201+
in Symbol | String
202+
if value.to_sym == target
203+
exactness[:yes] += 1
204+
return target
205+
else
206+
exactness[:maybe] += 1
207+
return value
208+
end
209+
else
210+
if strictness == :strong
211+
message = "cannot convert non-matching #{value.class} into #{target.inspect}"
212+
raise ArgumentError.new(message)
213+
end
206214
end
207215
else
208216
end

test/openai/internal/type/base_model_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ def test_coerce_errors
111111
[Integer, "one"] => TypeError,
112112
[Float, "one"] => TypeError,
113113
[String, Time] => TypeError,
114-
[:a, "one"] => ArgumentError,
115114
[Date, "one"] => ArgumentError,
116115
[Time, "one"] => ArgumentError
117116
}
@@ -346,7 +345,7 @@ def test_coerce
346345
[M2, {a: "1990-09-19", c: nil}] => [{yes: 2, maybe: 2}, {a: "1990-09-19", c: nil}],
347346

348347
[M3, {c: "c", d: "d"}] => [{yes: 3}, {c: :c, d: :d}],
349-
[M3, {c: "d", d: "c"}] => [{yes: 1, no: 2}, {c: "d", d: "c"}],
348+
[M3, {c: "d", d: "c"}] => [{yes: 1, maybe: 2}, {c: "d", d: "c"}],
350349

351350
[M4, {c: 2}] => [{yes: 5}, {c: 2}],
352351
[M4, {a: "1", c: 2}] => [{yes: 4, maybe: 1}, {a: "1", c: 2}],
@@ -404,7 +403,8 @@ def test_accessors
404403
cases = {
405404
M2.new({a: "1990-09-19", b: "1"}) => {a: Time.new(1990, 9, 19), b: TypeError},
406405
M2.new(a: "one", b: "one") => {a: ArgumentError, b: TypeError},
407-
M2.new(a: nil, b: 2.0) => {a: TypeError, b: TypeError},
406+
M2.new(a: nil, b: 2.0) => {a: TypeError},
407+
M2.new(a: nil, b: 2.2) => {a: TypeError, b: ArgumentError},
408408

409409
M3.new => {d: :d},
410410
M3.new(d: 1) => {d: ArgumentError},
@@ -520,8 +520,8 @@ def test_coerce
520520
[U0, :""] => [{no: 1}, 0, :""],
521521

522522
[U1, "a"] => [{yes: 1}, 1, :a],
523-
[U1, "2"] => [{maybe: 1}, 2, 2],
524-
[U1, :b] => [{no: 1}, 2, :b],
523+
[U1, "2"] => [{maybe: 1}, 2, "2"],
524+
[U1, :b] => [{maybe: 1}, 2, :b],
525525

526526
[U2, {type: :a}] => [{yes: 3}, 0, {t: :a}],
527527
[U2, {type: "b"}] => [{yes: 3}, 0, {type: :b}],

0 commit comments

Comments
 (0)