Skip to content

Commit

Permalink
Julia 0.7 fixes (#113)
Browse files Browse the repository at this point in the history
* Workarounds for some stdlib moves

* Use uninitialized in Array constructors

* Wrap Nullable definitions in a VERSION check
  • Loading branch information
ararslan authored and nalimilan committed Dec 16, 2017
1 parent df86ae9 commit aa30eea
Show file tree
Hide file tree
Showing 27 changed files with 90 additions and 55 deletions.
2 changes: 1 addition & 1 deletion REQUIRE
@@ -1,4 +1,4 @@
julia 0.6
Missings
Reexport
Compat 0.32.0
Compat 0.39.0
4 changes: 4 additions & 0 deletions src/CategoricalArrays.jl
Expand Up @@ -15,6 +15,10 @@ module CategoricalArrays
using Reexport
@reexport using Missings

if VERSION >= v"0.7.0-DEV.3052"
using Printf
end

include("typedefs.jl")

include("buildfields.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/array.jl
Expand Up @@ -340,7 +340,7 @@ Base.fill!(A::CategoricalArray, v::Any) =

function mergelevels(ordered, levels...)
T = Base.promote_eltype(levels...)
res = Array{T}(0)
res = Array{T}(uninitialized, 0)

# Fast path in case all levels are equal
if all(l -> l == levels[1], levels[2:end])
Expand Down
4 changes: 2 additions & 2 deletions src/buildfields.jl
@@ -1,5 +1,5 @@
function buildindex(invindex::Dict{S, R}) where {S, R <: Integer}
index = Vector{S}(length(invindex))
index = Vector{S}(uninitialized, length(invindex))
for (v, i) in invindex
index[i] = v
end
Expand Down Expand Up @@ -37,6 +37,6 @@ function buildorder!(order::Array{R},
end

function buildorder(invindex::Dict{S, R}, levels::Vector) where {S, R <: Integer}
order = Vector{R}(length(invindex))
order = Vector{R}(uninitialized, length(invindex))
return buildorder!(order, invindex, levels)
end
10 changes: 10 additions & 0 deletions src/deprecated.jl
Expand Up @@ -9,3 +9,13 @@
@deprecate CategoricalVector(::Type{T}, m::Integer; ordered=false) where {T} CategoricalVector{T}(m, ordered=ordered)

@deprecate CategoricalMatrix(::Type{T}, m::Int, n::Int; ordered=false) where {T} CategoricalMatrix{T}(m, n, ordered=ordered)

# Only define methods for Nullables while they're in Base, otherwise we don't care
if VERSION < v"0.7.0-DEV.3017"
Base.convert(::Type{Nullable{S}}, x::CategoricalValue{Nullable}) where {S} =
convert(Nullable{S}, get(x))
Base.convert(::Type{Nullable}, x::CategoricalValue{S}) where {S} = convert(Nullable{S}, x)
Base.convert(::Type{Nullable{CategoricalValue{Nullable{T}}}},
x::CategoricalValue{Nullable{T}}) where {T} =
Nullable(x)
end
4 changes: 2 additions & 2 deletions src/extras.jl
Expand Up @@ -76,7 +76,7 @@ function cut(x::AbstractArray{T, N}, breaks::AbstractVector;
end
end

refs = Array{DefaultRefType, N}(size(x))
refs = Array{DefaultRefType, N}(uninitialized, size(x))
try
fill_refs!(refs, x, breaks, extend, allow_missing)
catch err
Expand All @@ -93,7 +93,7 @@ function cut(x::AbstractArray{T, N}, breaks::AbstractVector;
if isempty(labels)
from = map(x -> sprint(showcompact, x), breaks[1:n-1])
to = map(x -> sprint(showcompact, x), breaks[2:n])
levs = Vector{String}(n-1)
levs = Vector{String}(uninitialized, n-1)
for i in 1:n-2
levs[i] = string("[", from[i], ", ", to[i], ")")
end
Expand Down
6 changes: 3 additions & 3 deletions src/recode.jl
Expand Up @@ -146,7 +146,7 @@ function recode!(dest::CategoricalArray{T}, src::CategoricalArray, default::Any,

# Remove recoded levels as they won't appear in result
firsts = (p.first for p in pairs)
keptlevels = Vector{T}()
keptlevels = Vector{T}(uninitialized, 0)
sizehint!(keptlevels, length(srclevels))

for l in srclevels
Expand Down Expand Up @@ -177,7 +177,7 @@ function recode!(dest::CategoricalArray{T}, src::CategoricalArray, default::Any,
srefs = src.refs

origmap = [get(dest.pool, v, 0) for v in srcindex]
indexmap = Vector{DefaultRefType}(length(srcindex)+1)
indexmap = Vector{DefaultRefType}(uninitialized, length(srcindex)+1)
# For missing values (0 if no missing in pairs' keys)
indexmap[1] = 0
for p in pairs
Expand Down Expand Up @@ -343,7 +343,7 @@ function recode(a::AbstractArray, default::Any, pairs::Pair...)
elseif T >: Missing || default === missing || (eltype(a) >: Missing && !keytype_hasmissing(pairs...))
dest = Array{Union{T, Missing}}(size(a))
else
dest = Array{Missings.T(T)}(size(a))
dest = Array{Missings.T(T)}(uninitialized, size(a))
end
recode!(dest, a, default, pairs...)
end
Expand Down
6 changes: 0 additions & 6 deletions src/value.jl
Expand Up @@ -57,12 +57,6 @@ Base.promote_rule(::Type{C}, ::Type{T}) where {C <: CategoricalString, T <: Abst
promote_type(leveltype(C), T)
Base.promote_rule(::Type{C}, ::Type{Missing}) where {C <: CatValue} = Union{C, Missing}

Base.convert(::Type{Nullable{S}}, x::CategoricalValue{Nullable}) where {S} =
convert(Nullable{S}, get(x))
Base.convert(::Type{Nullable}, x::CategoricalValue{S}) where {S} = convert(Nullable{S}, x)
Base.convert(::Type{Nullable{CategoricalValue{Nullable{T}}}},
x::CategoricalValue{Nullable{T}}) where {T} =
Nullable(x)
Base.convert(::Type{Ref}, x::CatValue) = RefValue{leveltype(x)}(x)
Base.convert(::Type{String}, x::CatValue) = convert(String, get(x))
Base.convert(::Type{Any}, x::CatValue) = x
Expand Down
3 changes: 2 additions & 1 deletion test/01_typedef.jl
@@ -1,5 +1,6 @@
module TestTypeDef
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays
using CategoricalArrays: DefaultRefType, level, reftype, leveltype, catvalue, iscatvalue

Expand Down
5 changes: 3 additions & 2 deletions test/02_buildorder.jl
@@ -1,5 +1,6 @@
module TestUpdateOrder
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays
using CategoricalArrays: DefaultRefType

Expand All @@ -17,7 +18,7 @@ using CategoricalArrays: DefaultRefType
)
)

order = Vector{DefaultRefType}(length(pool.index))
order = Vector{DefaultRefType}(uninitialized, length(pool.index))

CategoricalArrays.buildorder!(order, pool.invindex, ["b", "a", "c"])

Expand Down
3 changes: 2 additions & 1 deletion test/03_buildfields.jl
@@ -1,5 +1,6 @@
module TestBuildFields
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays
using CategoricalArrays: DefaultRefType

Expand Down
3 changes: 2 additions & 1 deletion test/04_constructors.jl
@@ -1,5 +1,6 @@
module TestConstructors
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays
using CategoricalArrays: DefaultRefType, catvalue

Expand Down
3 changes: 2 additions & 1 deletion test/05_convert.jl
@@ -1,5 +1,6 @@
module TestConvert
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays
using CategoricalArrays: DefaultRefType, level, reftype, leveltype, catvalue, iscatvalue

Expand Down
3 changes: 2 additions & 1 deletion test/06_length.jl
@@ -1,5 +1,6 @@
module TestLength
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays

@testset "length(pool)" begin
Expand Down
27 changes: 18 additions & 9 deletions test/06_show.jl
@@ -1,7 +1,16 @@
module TestShow
using Base.Test
using Compat
using Compat.Test
using Compat.Dates
using CategoricalArrays

# The type is prefixed after Dates moved to the stdlib
if VERSION >= v"0.7.0-DEV.2575"
const DateStr = "Dates.Date"
else
const DateStr = "Date"
end

@testset "show() for CategoricalPool{String} and its values" begin
pool = CategoricalPool(["c", "b", "a"])

Expand Down Expand Up @@ -57,16 +66,16 @@ end
ov2 = CategoricalArrays.catvalue(2, opool)
ov3 = CategoricalArrays.catvalue(3, opool)

@test sprint(show, pool) == "CategoricalArrays.CategoricalPool{Date,UInt32}([1999-12-01,1991-08-01,1993-10-01])"
@test sprint(show, opool) == "CategoricalArrays.CategoricalPool{Date,UInt32}([1991-08-01,1993-10-01,1999-12-01]) with ordered levels"
@test sprint(show, pool) == "CategoricalArrays.CategoricalPool{$DateStr,UInt32}([1999-12-01,1991-08-01,1993-10-01])"
@test sprint(show, opool) == "CategoricalArrays.CategoricalPool{$DateStr,UInt32}([1991-08-01,1993-10-01,1999-12-01]) with ordered levels"

@test sprint(show, nv1) == "CategoricalArrays.CategoricalValue{Date,UInt32} 1999-12-01"
@test sprint(show, nv2) == "CategoricalArrays.CategoricalValue{Date,UInt32} 1991-08-01"
@test sprint(show, nv3) == "CategoricalArrays.CategoricalValue{Date,UInt32} 1993-10-01"
@test sprint(show, nv1) == "CategoricalArrays.CategoricalValue{$DateStr,UInt32} 1999-12-01"
@test sprint(show, nv2) == "CategoricalArrays.CategoricalValue{$DateStr,UInt32} 1991-08-01"
@test sprint(show, nv3) == "CategoricalArrays.CategoricalValue{$DateStr,UInt32} 1993-10-01"

@test sprint(show, ov1) == "CategoricalArrays.CategoricalValue{Date,UInt32} 1999-12-01 (3/3)"
@test sprint(show, ov2) == "CategoricalArrays.CategoricalValue{Date,UInt32} 1991-08-01 (1/3)"
@test sprint(show, ov3) == "CategoricalArrays.CategoricalValue{Date,UInt32} 1993-10-01 (2/3)"
@test sprint(show, ov1) == "CategoricalArrays.CategoricalValue{$DateStr,UInt32} 1999-12-01 (3/3)"
@test sprint(show, ov2) == "CategoricalArrays.CategoricalValue{$DateStr,UInt32} 1991-08-01 (1/3)"
@test sprint(show, ov3) == "CategoricalArrays.CategoricalValue{$DateStr,UInt32} 1993-10-01 (2/3)"

@test sprint(showcompact, nv1) == sprint(showcompact, ov1) == "1999-12-01"
@test sprint(showcompact, nv2) == sprint(showcompact, ov2) == "1991-08-01"
Expand Down
3 changes: 2 additions & 1 deletion test/07_levels.jl
@@ -1,5 +1,6 @@
module TestLevels
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays
using CategoricalArrays: DefaultRefType, levels!

Expand Down
3 changes: 2 additions & 1 deletion test/08_equality.jl
@@ -1,5 +1,6 @@
module TestEquality
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays

@testset "== and isequal() for CategoricalPool{Int} and CategoricalPool{Float64}" begin
Expand Down
3 changes: 2 additions & 1 deletion test/08_string.jl
@@ -1,5 +1,6 @@
module TestString
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays

@testset "AbstractString operations on values of CategoricalPool{String}" begin
Expand Down
3 changes: 2 additions & 1 deletion test/09_hash.jl
@@ -1,5 +1,6 @@
module TestHash
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays

@testset "hash() for CategoricalPool{Int} and CategoricalPool{Float64} and its values" begin
Expand Down
3 changes: 2 additions & 1 deletion test/10_isless.jl
@@ -1,5 +1,6 @@
module TestIsLess
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays

pool = CategoricalPool([1, 2, 3])
Expand Down
3 changes: 2 additions & 1 deletion test/11_array.jl
@@ -1,5 +1,6 @@
module TestArray
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays
using CategoricalArrays: DefaultRefType, catvaluetype, leveltype

Expand Down
3 changes: 2 additions & 1 deletion test/12_missingarray.jl
@@ -1,5 +1,6 @@
module TestMissingArray
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays
using CategoricalArrays: DefaultRefType, catvaluetype, leveltype

Expand Down
7 changes: 4 additions & 3 deletions test/13_arraycommon.jl
@@ -1,5 +1,6 @@
module TestArrayCommon
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays
using CategoricalArrays: DefaultRefType, index

Expand Down Expand Up @@ -81,8 +82,8 @@ end
@test levels(r) == collect(3:300)

# Test vcat of multidimensional arrays
a1 = Array{Int}(2, 3, 4, 5)
a2 = Array{Int}(3, 3, 4, 5)
a1 = Array{Int}(uninitialized, 2, 3, 4, 5)
a2 = Array{Int}(uninitialized, 3, 3, 4, 5)
a1[1:end] = (length(a1):-1:1) + 2
a2[1:end] = (1:length(a2)) + 10
ca1 = CategoricalArray{Union{T, Int}}(a1)
Expand Down
3 changes: 2 additions & 1 deletion test/14_view.jl
@@ -1,5 +1,6 @@
module TestView
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays

@testset "view($(CategoricalArray{Union{T, eltype(a)}}), $inds), ordered=$order construction" for
Expand Down
3 changes: 2 additions & 1 deletion test/15_extras.jl
@@ -1,5 +1,6 @@
module TestExtras
using Base.Test
using Compat
using Compat.Test
using CategoricalArrays

const = isequal
Expand Down

0 comments on commit aa30eea

Please sign in to comment.