Skip to content

Commit 9dc6b52

Browse files
chore(internal): loosen internal type restrictions
1 parent 9297be8 commit 9dc6b52

8 files changed

Lines changed: 86 additions & 43 deletions

File tree

lib/orb/internal/type/enum.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ def ===(other) = values.include?(other)
5858
#
5959
# @return [Boolean]
6060
def ==(other)
61-
# rubocop:disable Layout/LineLength
62-
other.is_a?(Module) && other.singleton_class <= Orb::Internal::Type::Enum && other.values.to_set == values.to_set
63-
# rubocop:enable Layout/LineLength
61+
# rubocop:disable Style/CaseEquality
62+
Orb::Internal::Type::Enum === other && other.values.to_set == values.to_set
63+
# rubocop:enable Style/CaseEquality
6464
end
6565

6666
# @api private

lib/orb/internal/type/union.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ def ===(other)
140140
#
141141
# @return [Boolean]
142142
def ==(other)
143-
# rubocop:disable Layout/LineLength
144-
other.is_a?(Module) && other.singleton_class <= Orb::Internal::Type::Union && other.derefed_variants == derefed_variants
145-
# rubocop:enable Layout/LineLength
143+
Orb::Internal::Type::Union === other && other.derefed_variants == derefed_variants
146144
end
147145

148146
# @api private

rbi/lib/orb/internal/type/array_of.rbi

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ module Orb
1010
include Orb::Internal::Type::Converter
1111

1212
abstract!
13-
final!
1413

1514
Elem = type_member(:out)
1615

17-
sig(:final) do
16+
sig do
1817
params(
1918
type_info: T.any(
2019
Orb::Internal::AnyHash,
@@ -27,14 +26,14 @@ module Orb
2726
end
2827
def self.[](type_info, spec = {}); end
2928

30-
sig(:final) { params(other: T.anything).returns(T::Boolean) }
29+
sig { params(other: T.anything).returns(T::Boolean) }
3130
def ===(other); end
3231

33-
sig(:final) { params(other: T.anything).returns(T::Boolean) }
32+
sig { params(other: T.anything).returns(T::Boolean) }
3433
def ==(other); end
3534

3635
# @api private
37-
sig(:final) do
36+
sig do
3837
override
3938
.params(value: T.any(
4039
T::Array[T.anything],
@@ -46,7 +45,7 @@ module Orb
4645
def coerce(value, state:); end
4746

4847
# @api private
49-
sig(:final) do
48+
sig do
5049
override
5150
.params(value: T.any(
5251
T::Array[T.anything],
@@ -58,15 +57,15 @@ module Orb
5857
def dump(value, state:); end
5958

6059
# @api private
61-
sig(:final) { returns(Elem) }
60+
sig { returns(Elem) }
6261
protected def item_type; end
6362

6463
# @api private
65-
sig(:final) { returns(T::Boolean) }
64+
sig { returns(T::Boolean) }
6665
protected def nilable?; end
6766

6867
# @api private
69-
sig(:final) do
68+
sig do
7069
params(
7170
type_info: T.any(
7271
Orb::Internal::AnyHash,
@@ -80,7 +79,7 @@ module Orb
8079
def initialize(type_info, spec = {}); end
8180

8281
# @api private
83-
sig(:final) { params(depth: Integer).returns(String) }
82+
sig { params(depth: Integer).returns(String) }
8483
def inspect(depth: 0); end
8584
end
8685
end

rbi/lib/orb/internal/type/boolean.rbi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@ module Orb
1010
extend Orb::Internal::Type::Converter
1111

1212
abstract!
13-
final!
1413

15-
sig(:final) { params(other: T.anything).returns(T::Boolean) }
14+
sig { params(other: T.anything).returns(T::Boolean) }
1615
def self.===(other); end
1716

18-
sig(:final) { params(other: T.anything).returns(T::Boolean) }
17+
sig { params(other: T.anything).returns(T::Boolean) }
1918
def self.==(other); end
2019

2120
class << self
2221
# @api private
23-
sig(:final) do
22+
sig do
2423
override
2524
.params(value: T.any(
2625
T::Boolean,
@@ -32,7 +31,7 @@ module Orb
3231
def coerce(value, state:); end
3332

3433
# @api private
35-
sig(:final) do
34+
sig do
3635
override
3736
.params(value: T.any(T::Boolean, T.anything), state: Orb::Internal::Type::Converter::DumpState)
3837
.returns(T.any(T::Boolean, T.anything))

rbi/lib/orb/internal/type/hash_of.rbi

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ module Orb
1010
include Orb::Internal::Type::Converter
1111

1212
abstract!
13-
final!
1413

1514
Elem = type_member(:out)
1615

17-
sig(:final) do
16+
sig do
1817
params(
1918
type_info: T.any(
2019
Orb::Internal::AnyHash,
@@ -27,14 +26,14 @@ module Orb
2726
end
2827
def self.[](type_info, spec = {}); end
2928

30-
sig(:final) { params(other: T.anything).returns(T::Boolean) }
29+
sig { params(other: T.anything).returns(T::Boolean) }
3130
def ===(other); end
3231

33-
sig(:final) { params(other: T.anything).returns(T::Boolean) }
32+
sig { params(other: T.anything).returns(T::Boolean) }
3433
def ==(other); end
3534

3635
# @api private
37-
sig(:final) do
36+
sig do
3837
override
3938
.params(
4039
value: T.any(T::Hash[T.anything, T.anything], T.anything),
@@ -45,7 +44,7 @@ module Orb
4544
def coerce(value, state:); end
4645

4746
# @api private
48-
sig(:final) do
47+
sig do
4948
override
5049
.params(
5150
value: T.any(T::Hash[T.anything, T.anything], T.anything),
@@ -56,15 +55,15 @@ module Orb
5655
def dump(value, state:); end
5756

5857
# @api private
59-
sig(:final) { returns(Elem) }
58+
sig { returns(Elem) }
6059
protected def item_type; end
6160

6261
# @api private
63-
sig(:final) { returns(T::Boolean) }
62+
sig { returns(T::Boolean) }
6463
protected def nilable?; end
6564

6665
# @api private
67-
sig(:final) do
66+
sig do
6867
params(
6968
type_info: T.any(
7069
Orb::Internal::AnyHash,
@@ -78,7 +77,7 @@ module Orb
7877
def initialize(type_info, spec = {}); end
7978

8079
# @api private
81-
sig(:final) { params(depth: Integer).returns(String) }
80+
sig { params(depth: Integer).returns(String) }
8281
def inspect(depth: 0); end
8382
end
8483
end

rbi/lib/orb/internal/type/io_like.rbi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@ module Orb
1010
extend Orb::Internal::Type::Converter
1111

1212
abstract!
13-
final!
1413

15-
sig(:final) { params(other: T.anything).returns(T::Boolean) }
14+
sig { params(other: T.anything).returns(T::Boolean) }
1615
def self.===(other); end
1716

18-
sig(:final) { params(other: T.anything).returns(T::Boolean) }
17+
sig { params(other: T.anything).returns(T::Boolean) }
1918
def self.==(other); end
2019

2120
class << self
2221
# @api private
23-
sig(:final) do
22+
sig do
2423
override
2524
.params(value: T.any(
2625
StringIO,
@@ -33,7 +32,7 @@ module Orb
3332
def coerce(value, state:); end
3433

3534
# @api private
36-
sig(:final) do
35+
sig do
3736
override
3837
.params(
3938
value: T.any(Pathname, StringIO, IO, String, T.anything),

rbi/lib/orb/internal/type/unknown.rbi

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@ module Orb
1010
extend Orb::Internal::Type::Converter
1111

1212
abstract!
13-
final!
1413

15-
sig(:final) { params(other: T.anything).returns(T::Boolean) }
14+
sig { params(other: T.anything).returns(T::Boolean) }
1615
def self.===(other); end
1716

18-
sig(:final) { params(other: T.anything).returns(T::Boolean) }
17+
sig { params(other: T.anything).returns(T::Boolean) }
1918
def self.==(other); end
2019

2120
class << self
2221
# @api private
23-
sig(:final) do
22+
sig do
2423
override.params(
2524
value: T.anything,
2625
state: Orb::Internal::Type::Converter::CoerceState
@@ -29,7 +28,7 @@ module Orb
2928
def coerce(value, state:); end
3029

3130
# @api private
32-
sig(:final) do
31+
sig do
3332
override.params(
3433
value: T.anything,
3534
state: Orb::Internal::Type::Converter::DumpState

test/orb/internal/type/base_model_test.rb

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,12 @@ def test_dump_retry
154154
end
155155

156156
class Orb::Test::EnumModelTest < Minitest::Test
157+
class E0
158+
include Orb::Internal::Type::Enum
159+
160+
def initialize(*values) = (@values = values)
161+
end
162+
157163
module E1
158164
extend Orb::Internal::Type::Enum
159165

@@ -183,6 +189,10 @@ module E4
183189

184190
def test_coerce
185191
cases = {
192+
[E0.new, "one"] => [{no: 1}, "one"],
193+
[E0.new(:one), "one"] => [{yes: 1}, :one],
194+
[E0.new(:two), "one"] => [{maybe: 1}, "one"],
195+
186196
# rubocop:disable Lint/BooleanSymbol
187197
[E1, true] => [{yes: 1}, true],
188198
[E1, false] => [{no: 1}, false],
@@ -432,8 +442,10 @@ def test_accessors
432442
end
433443

434444
class Orb::Test::UnionTest < Minitest::Test
435-
module U0
436-
extend Orb::Internal::Type::Union
445+
class U0
446+
include Orb::Internal::Type::Union
447+
448+
def initialize(*variants) = variants.each { variant(_1) }
437449
end
438450

439451
module U1
@@ -519,6 +531,11 @@ def test_coerce
519531
cases = {
520532
[U0, :""] => [{no: 1}, 0, :""],
521533

534+
[U0.new(Integer, Float), "one"] => [{no: 1}, 2, "one"],
535+
[U0.new(Integer, Float), 1.0] => [{yes: 1}, 2, 1.0],
536+
[U0.new({const: :a}), "a"] => [{yes: 1}, 1, :a],
537+
[U0.new({const: :a}), "2"] => [{maybe: 1}, 1, "2"],
538+
522539
[U1, "a"] => [{yes: 1}, 1, :a],
523540
[U1, "2"] => [{maybe: 1}, 2, "2"],
524541
[U1, :b] => [{maybe: 1}, 2, :b],
@@ -556,6 +573,12 @@ def test_coerce
556573
end
557574

558575
class Orb::Test::BaseModelQoLTest < Minitest::Test
576+
class E0
577+
include Orb::Internal::Type::Enum
578+
579+
def initialize(*values) = (@values = values)
580+
end
581+
559582
module E1
560583
extend Orb::Internal::Type::Enum
561584

@@ -575,6 +598,26 @@ module E3
575598
B = 3
576599
end
577600

601+
class U0
602+
include Orb::Internal::Type::Union
603+
604+
def initialize(*variants) = variants.each { variant(_1) }
605+
end
606+
607+
module U1
608+
extend Orb::Internal::Type::Union
609+
610+
variant String
611+
variant Integer
612+
end
613+
614+
module U2
615+
extend Orb::Internal::Type::Union
616+
617+
variant String
618+
variant Integer
619+
end
620+
578621
class M1 < Orb::Internal::Type::BaseModel
579622
required :a, Integer
580623
end
@@ -592,8 +635,15 @@ def test_equality
592635
[Orb::Internal::Type::Unknown, Orb::Internal::Type::Unknown] => true,
593636
[Orb::Internal::Type::Boolean, Orb::Internal::Type::Boolean] => true,
594637
[Orb::Internal::Type::Unknown, Orb::Internal::Type::Boolean] => false,
638+
[E0.new(:a, :b), E0.new(:a, :b)] => true,
639+
[E0.new(:a, :b), E0.new(:b, :a)] => true,
640+
[E0.new(:a, :b), E0.new(:b, :c)] => false,
595641
[E1, E2] => true,
596642
[E1, E3] => false,
643+
[U0.new(String, Integer), U0.new(String, Integer)] => true,
644+
[U0.new(String, Integer), U0.new(Integer, String)] => false,
645+
[U0.new(String, Float), U0.new(String, Integer)] => false,
646+
[U1, U2] => true,
597647
[M1, M2] => false,
598648
[M1, M3] => true
599649
}

0 commit comments

Comments
 (0)