From 59dc8b25a68d2c5369b680e0287713d102ab810c Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Mon, 16 Nov 2020 19:50:15 +0100 Subject: [PATCH 1/3] define haskey/hasproperty on Composite --- Project.toml | 2 +- src/differentials/composite.jl | 5 +++++ test/differentials/composite.jl | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 7700e37cf..ced2642ae 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "ChainRulesCore" uuid = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" -version = "0.9.17" +version = "0.9.18" [deps] LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/src/differentials/composite.jl b/src/differentials/composite.jl index 98a56e259..5abbd31dd 100644 --- a/src/differentials/composite.jl +++ b/src/differentials/composite.jl @@ -82,6 +82,11 @@ end Base.keys(comp::Composite) = keys(backing(comp)) Base.propertynames(comp::Composite) = propertynames(backing(comp)) +Base.haskey(comp::Composite, key) = haskey(backing(comp), key) +if isdefined(Base, :hasproperty) + Base.hasproperty(comp::Composite, key::Symbol) = hasproperty(backing(comp), key) +end + Base.iterate(comp::Composite, args...) = iterate(backing(comp), args...) Base.length(comp::Composite) = length(backing(comp)) Base.eltype(::Type{<:Composite{<:Any, T}}) where T = eltype(T) diff --git a/test/differentials/composite.jl b/test/differentials/composite.jl index cb872e9a3..0ac921da6 100644 --- a/test/differentials/composite.jl +++ b/test/differentials/composite.jl @@ -48,6 +48,10 @@ end @testset "indexing, iterating, and properties" begin @test keys(Composite{Foo}(x=2.5)) == (:x,) @test propertynames(Composite{Foo}(x=2.5)) == (:x,) + @test haskey(Composite{Foo}(x=2.5), :x) == true + if isdefined(Base, :hasproperty) + @test hasproperty(Composite{Foo}(x=2.5), :y) == false + end @test Composite{Foo}(x=2.5).x == 2.5 @test keys(Composite{Tuple{Float64,}}(2.0)) == Base.OneTo(1) @@ -56,6 +60,10 @@ end @test getproperty(Composite{Tuple{Float64,}}(@thunk 2.0^2), 1) == 4.0 @test getproperty(Composite{Tuple{Float64,}}(a=(@thunk 2.0^2),), :a) == 4.0 + # https://github.com/JuliaLang/julia/issues/35516 + # @test haskey(Composite{Tuple{Float64}}(2.0), 1) == true + # @test hasproperty(Composite{Tuple{Float64}}(2.0), 2) == false + @test length(Composite{Foo}(x=2.5)) == 1 @test length(Composite{Tuple{Float64,}}(2.0)) == 1 From a1c94e5b5388c50da8f781f57863868d274745fb Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Mon, 16 Nov 2020 20:04:30 +0100 Subject: [PATCH 2/3] Update test/differentials/composite.jl Co-authored-by: Lyndon White --- test/differentials/composite.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/differentials/composite.jl b/test/differentials/composite.jl index 0ac921da6..2720cd2df 100644 --- a/test/differentials/composite.jl +++ b/test/differentials/composite.jl @@ -60,7 +60,7 @@ end @test getproperty(Composite{Tuple{Float64,}}(@thunk 2.0^2), 1) == 4.0 @test getproperty(Composite{Tuple{Float64,}}(a=(@thunk 2.0^2),), :a) == 4.0 - # https://github.com/JuliaLang/julia/issues/35516 + # TODO: uncomment this once https://github.com/JuliaLang/julia/issues/35516 # @test haskey(Composite{Tuple{Float64}}(2.0), 1) == true # @test hasproperty(Composite{Tuple{Float64}}(2.0), 2) == false From 194c6c705bf6f404978e9259f169aba14f8772e3 Mon Sep 17 00:00:00 2001 From: Simeon Schaub Date: Mon, 16 Nov 2020 20:52:40 +0100 Subject: [PATCH 3/3] Update test/differentials/composite.jl --- test/differentials/composite.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/differentials/composite.jl b/test/differentials/composite.jl index 2720cd2df..b26ba5c13 100644 --- a/test/differentials/composite.jl +++ b/test/differentials/composite.jl @@ -61,8 +61,8 @@ end @test getproperty(Composite{Tuple{Float64,}}(a=(@thunk 2.0^2),), :a) == 4.0 # TODO: uncomment this once https://github.com/JuliaLang/julia/issues/35516 - # @test haskey(Composite{Tuple{Float64}}(2.0), 1) == true - # @test hasproperty(Composite{Tuple{Float64}}(2.0), 2) == false + @test_broken haskey(Composite{Tuple{Float64}}(2.0), 1) == true + @test_broken hasproperty(Composite{Tuple{Float64}}(2.0), 2) == false @test length(Composite{Foo}(x=2.5)) == 1 @test length(Composite{Tuple{Float64,}}(2.0)) == 1