Skip to content

Commit 61b64b8

Browse files
authored
Fix deprecation warnings in tests (#1894)
* Fix deprecation warnings in tests * Change deprecations * Fixes * Fix for Julia 1.3 * Fix for Julia 1.3 * `redirect_stderr` on Julia < 1.6
1 parent 08c56ea commit 61b64b8

File tree

3 files changed

+116
-89
lines changed

3 files changed

+116
-89
lines changed

src/Distributions.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ export
129129
MatrixBeta,
130130
MatrixFDist,
131131
MatrixNormal,
132-
MatrixReshaped,
133132
MatrixTDist,
134133
MixtureModel,
135134
Multinomial,

src/deprecates.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,20 @@ end
5353
@deprecate expectation(distr::Union{UnivariateDistribution,MultivariateDistribution}, g::Function; kwargs...) expectation(g, distr; kwargs...) false
5454

5555
# Deprecate `MatrixReshaped`
56+
# This is very similar to `Base.@deprecate_binding MatrixReshaped{...} ReshapedDistribution{...}`
57+
# However, `Base.@deprecate_binding` does not support type parameters
58+
export MatrixReshaped
5659
const MatrixReshaped{S<:ValueSupport,D<:MultivariateDistribution{S}} = ReshapedDistribution{2,S,D}
5760
Base.deprecate(@__MODULE__, :MatrixReshaped)
58-
@deprecate MatrixReshaped(
59-
d::MultivariateDistribution, n::Integer, p::Integer=n
60-
) reshape(d, (n, p))
61+
# This is very similar to `Base.@deprecate MatrixReshaped(...) reshape(...)`
62+
# We use another (unexported!) alias here to not throw a deprecation warning/error
63+
# Unexported aliases do not affect the type printing
64+
# In Julia >= 1.6, instead of a new alias we could have defined a method for (ReshapedDistribution{2,S,D} where {S<:ValueSupport,D<:MultivariateDistribution{S}})
65+
const _MatrixReshaped{S<:ValueSupport,D<:MultivariateDistribution{S}} = ReshapedDistribution{2,S,D}
66+
function _MatrixReshaped(d::MultivariateDistribution, n::Integer, p::Integer=n)
67+
Base.depwarn("`MatrixReshaped(d, n, p)` is deprecated, use `reshape(d, (n, p))` instead.", :MatrixReshaped)
68+
return reshape(d, (n, p))
69+
end
6170

6271
for D in (:InverseWishart, :LKJ, :MatrixBeta, :MatrixFDist, :Wishart)
6372
@eval @deprecate dim(d::$D) size(d, 1)

test/matrixreshaped.jl

Lines changed: 104 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,113 +3,132 @@ using Distributions, Test, Random, LinearAlgebra
33

44
rng = MersenneTwister(123456)
55

6-
@testset "matrixreshaped.jl" begin
6+
if VERSION >= v"1.6.0-DEV.254"
7+
_redirect_stderr(f, ::Base.DevNull) = redirect_stderr(f, devnull)
8+
else
9+
function _redirect_stderr(f, ::Base.DevNull)
10+
nulldev = @static Sys.iswindows() ? "NUL" : "/dev/null"
11+
open(nulldev, "w") do io
12+
redirect_stderr(f, io)
13+
end
14+
end
15+
end
716

817
function test_matrixreshaped(rng, d1, sizes)
9-
x1 = rand(rng, d1)
10-
d1s = [@test_deprecated(MatrixReshaped(d1, s...)) for s in sizes]
18+
@testset "MatrixReshaped $(nameof(typeof(d1))) tests" begin
19+
x1 = rand(rng, d1)
20+
d1s = [@test_deprecated(MatrixReshaped(d1, s...)) for s in sizes]
1121

12-
@testset "MatrixReshaped $(nameof(typeof(d1))) tests" begin
13-
@testset "MatrixReshaped constructor" begin
14-
for d in d1s
15-
@test d isa MatrixReshaped
22+
@testset "MatrixReshaped constructor" begin
23+
for d in d1s
24+
@test d isa MatrixReshaped
25+
end
1626
end
17-
end
18-
@testset "MatrixReshaped constructor errors" begin
19-
@test_deprecated(@test_throws ArgumentError MatrixReshaped(d1, length(d1), 2))
20-
@test_deprecated(@test_throws ArgumentError MatrixReshaped(d1, length(d1)))
21-
@test_deprecated(@test_throws ArgumentError MatrixReshaped(d1, -length(d1), -1))
22-
end
23-
@testset "MatrixReshaped size" begin
24-
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
25-
@test size(d) == s
27+
@testset "MatrixReshaped constructor errors" begin
28+
@test_deprecated(@test_throws ArgumentError MatrixReshaped(d1, length(d1), 2))
29+
@test_deprecated(@test_throws ArgumentError MatrixReshaped(d1, length(d1)))
30+
@test_deprecated(@test_throws ArgumentError MatrixReshaped(d1, -length(d1), -1))
2631
end
27-
end
28-
@testset "MatrixReshaped length" begin
29-
for d in d1s
30-
@test length(d) == length(d1)
32+
@testset "MatrixReshaped size" begin
33+
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
34+
@test size(d) == s
35+
end
3136
end
32-
end
33-
@testset "MatrixReshaped rank" begin
34-
for (d, s) in zip(d1s, sizes)
35-
@test rank(d) == minimum(s)
37+
@testset "MatrixReshaped length" begin
38+
for d in d1s
39+
@test length(d) == length(d1)
40+
end
3641
end
37-
end
38-
@testset "MatrixReshaped insupport" begin
39-
for (i, d) in enumerate(d1s[1:end-1])
40-
for (j, s) in enumerate(sizes[1:end-1])
41-
@test (i == j) !insupport(d, reshape(x1, s))
42+
@testset "MatrixReshaped rank" begin
43+
for (d, s) in zip(d1s, sizes)
44+
@test rank(d) == minimum(s)
4245
end
4346
end
44-
end
45-
@testset "MatrixReshaped mean" begin
46-
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
47-
@test mean(d) == reshape(mean(d1), s)
47+
@testset "MatrixReshaped insupport" begin
48+
for (i, d) in enumerate(d1s[1:end-1])
49+
for (j, s) in enumerate(sizes[1:end-1])
50+
@test (i == j) !insupport(d, reshape(x1, s))
51+
end
52+
end
4853
end
49-
end
50-
@testset "MatrixReshaped mode" begin
51-
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
52-
@test mode(d) == reshape(mode(d1), s)
54+
@testset "MatrixReshaped mean" begin
55+
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
56+
@test mean(d) == reshape(mean(d1), s)
57+
end
5358
end
54-
end
55-
@testset "MatrixReshaped covariance" begin
56-
for (d, (n, p)) in zip(d1s[1:end-1], sizes[1:end-1])
57-
@test cov(d) == cov(d1)
58-
@test cov(d, Val(false)) == reshape(cov(d1), n, p, n, p)
59+
@testset "MatrixReshaped mode" begin
60+
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
61+
@test mode(d) == reshape(mode(d1), s)
62+
end
5963
end
60-
end
61-
@testset "MatrixReshaped variance" begin
62-
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
63-
@test var(d) == reshape(var(d1), s)
64+
@testset "MatrixReshaped covariance" begin
65+
for (d, (n, p)) in zip(d1s[1:end-1], sizes[1:end-1])
66+
@test cov(d) == cov(d1)
67+
@test cov(d, Val(false)) == reshape(cov(d1), n, p, n, p)
68+
end
6469
end
65-
end
66-
@testset "MatrixReshaped params" begin
67-
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
68-
@test params(d) == (d1, s)
70+
@testset "MatrixReshaped variance" begin
71+
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
72+
@test var(d) == reshape(var(d1), s)
73+
end
6974
end
70-
end
71-
@testset "MatrixReshaped partype" begin
72-
for d in d1s
73-
@test partype(d) === partype(d1)
75+
@testset "MatrixReshaped params" begin
76+
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
77+
@test params(d) == (d1, s)
78+
end
7479
end
75-
end
76-
@testset "MatrixReshaped eltype" begin
77-
for d in d1s
78-
@test eltype(d) === eltype(d1)
80+
@testset "MatrixReshaped partype" begin
81+
for d in d1s
82+
@test partype(d) === partype(d1)
83+
end
7984
end
80-
end
81-
@testset "MatrixReshaped logpdf" begin
82-
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
83-
x = reshape(x1, s)
84-
@test logpdf(d, x) == logpdf(d1, x1)
85+
@testset "MatrixReshaped eltype" begin
86+
for d in d1s
87+
@test eltype(d) === eltype(d1)
88+
end
8589
end
86-
end
87-
@testset "MatrixReshaped rand" begin
88-
for d in d1s
89-
x = rand(rng, d)
90-
@test insupport(d, x)
91-
@test insupport(d1, vec(x))
92-
@test logpdf(d, x) == logpdf(d1, vec(x))
90+
@testset "MatrixReshaped logpdf" begin
91+
for (d, s) in zip(d1s[1:end-1], sizes[1:end-1])
92+
x = reshape(x1, s)
93+
@test logpdf(d, x) == logpdf(d1, x1)
94+
end
9395
end
94-
end
95-
@testset "MatrixReshaped vec" begin
96-
for d in d1s
97-
@test vec(d) === d1
96+
@testset "MatrixReshaped rand" begin
97+
for d in d1s
98+
x = rand(rng, d)
99+
@test insupport(d, x)
100+
@test insupport(d1, vec(x))
101+
@test logpdf(d, x) == logpdf(d1, vec(x))
102+
end
103+
end
104+
@testset "MatrixReshaped vec" begin
105+
for d in d1s
106+
@test vec(d) === d1
107+
end
98108
end
99109
end
100-
end
101110
end
102111

103-
# MvNormal
104-
σ = rand(rng, 16, 16)
105-
μ = rand(rng, 16)
106-
d1 = MvNormal(μ, σ * σ')
107-
sizes = [(4, 4), (8, 2), (2, 8), (1, 16), (16, 1), (4,)]
108-
test_matrixreshaped(rng, d1, sizes)
112+
# Note: In contrast to `@deprecate`, `@deprecate_binding` can't be tested with `@test_deprecated`
113+
# Ref: https://github.com/JuliaLang/julia/issues/38780
114+
@testset "matrixreshaped.jl" begin
115+
@testset "MvNormal" begin
116+
σ = rand(rng, 16, 16)
117+
μ = rand(rng, 16)
118+
d1 = MvNormal(μ, σ * σ')
119+
sizes = [(4, 4), (8, 2), (2, 8), (1, 16), (16, 1), (4,)]
120+
_redirect_stderr(devnull) do
121+
test_matrixreshaped(rng, d1, sizes)
122+
end
123+
end
109124

110125
# Dirichlet
111-
α = rand(rng, 36) .+ 1 # mode is only defined if all alpha > 1
112-
d1 = Dirichlet(α)
113-
sizes = [(6, 6), (4, 9), (9, 4), (3, 12), (12, 3), (1, 36), (36, 1), (6,)]
114-
test_matrixreshaped(rng, d1, sizes)
126+
@testset "Dirichlet" begin
127+
α = rand(rng, 36) .+ 1 # mode is only defined if all alpha > 1
128+
d1 = Dirichlet(α)
129+
sizes = [(6, 6), (4, 9), (9, 4), (3, 12), (12, 3), (1, 36), (36, 1), (6,)]
130+
_redirect_stderr(devnull) do
131+
test_matrixreshaped(rng, d1, sizes)
132+
end
133+
end
115134
end

0 commit comments

Comments
 (0)