Skip to content

Commit 0d0c2bb

Browse files
authored
Fix empty tuple piracy (#214)
* fix empty tuple piracy, add some tests * test show too * bump patch version - bugfix
1 parent 5444c5e commit 0d0c2bb

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "StatsModels"
22
uuid = "3eaba693-59b7-5ba5-a881-562e759f1c8d"
3-
version = "0.6.20"
3+
version = "0.6.21"
44

55
[deps]
66
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"

src/terms.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
abstract type AbstractTerm end
2-
const TermOrTerms = Union{AbstractTerm, NTuple{N, AbstractTerm} where N}
3-
const TupleTerm = NTuple{N, TermOrTerms} where N
2+
const TermOrTerms = Union{AbstractTerm, Tuple{AbstractTerm, Vararg{AbstractTerm}}}
3+
const TupleTerm = Tuple{TermOrTerms, Vararg{TermOrTerms}}
44

55
width(::T) where {T<:AbstractTerm} =
66
throw(ArgumentError("terms of type $T have undefined width"))

test/terms.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,41 @@ StatsModels.apply_schema(mt::MultiTerm, sch::StatsModels.Schema, Mod::Type) =
173173

174174
end
175175

176+
@testset "Tuple terms" begin
177+
using StatsModels: TermOrTerms, TupleTerm, Term
178+
a, b, c = Term.((:a, :b, :c))
179+
180+
# TermOrTerms - one or more AbstractTerms (if more, a tuple)
181+
# empty tuples are never terms
182+
@test !(() isa TermOrTerms)
183+
@test (a, ) isa TermOrTerms
184+
@test (a, b) isa TermOrTerms
185+
@test (a, b, a&b) isa TermOrTerms
186+
@test !(((), a) isa TermOrTerms)
187+
# can't contain further tuples
188+
@test !((a, (a,), b) isa TermOrTerms)
189+
190+
# a tuple of AbstractTerms OR Tuples of one or more terms
191+
# empty tuples are never terms
192+
@test !(() isa TupleTerm)
193+
@test (a, ) isa TupleTerm
194+
@test (a, b) isa TupleTerm
195+
@test (a, b, a&b) isa TupleTerm
196+
@test !(((), a) isa TupleTerm)
197+
@test (((a,), a) isa TupleTerm)
198+
199+
# no methods for operators on term and empty tuple (=no type piracy)
200+
@test_throws MethodError a + ()
201+
@test_throws MethodError () + a
202+
@test_throws MethodError a & ()
203+
@test_throws MethodError () & a
204+
@test_throws MethodError a ~ ()
205+
@test_throws MethodError () ~ a
206+
207+
# show methods of empty tuples preserved
208+
@test "$(())" == "()"
209+
@test "$((a,b))" == "a + b"
210+
@test "$((a, ()))" == "(a, ())"
211+
end
212+
176213
end

0 commit comments

Comments
 (0)