From bc5eda97233f84681e1c5004526c4d19c4ae07d3 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Wed, 22 Feb 2023 17:51:41 +0100 Subject: [PATCH 1/3] workaround issue 72 --- src/ConstructionBase.jl | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/ConstructionBase.jl b/src/ConstructionBase.jl index 546eae5..61ee2af 100644 --- a/src/ConstructionBase.jl +++ b/src/ConstructionBase.jl @@ -52,23 +52,27 @@ function is_propertynames_overloaded(T::Type)::Bool which(propertynames, Tuple{T}).sig !== Tuple{typeof(propertynames), Any} end -@generated function check_properties_are_fields(obj) - if is_propertynames_overloaded(obj) - return quote - T = typeof(obj) - msg = """ - The function `Base.propertynames` was overloaded for type `$T`. - Please make sure the following methods are also overloaded for this type: - ```julia - ConstructionBase.setproperties - ConstructionBase.getproperties # optional in VERSION >= julia v1.7 - ``` - """ - error(msg) +if VERSION < v"1.10-" + @generated function check_properties_are_fields(obj) + if is_propertynames_overloaded(obj) + return quote + T = typeof(obj) + msg = """ + The function `Base.propertynames` was overloaded for type `$T`. + Please make sure the following methods are also overloaded for this type: + ```julia + ConstructionBase.setproperties + ConstructionBase.getproperties # optional in VERSION >= julia v1.7 + ``` + """ + error(msg) + end + else + :(nothing) end - else - :(nothing) end +else + check_properties_are_fields(obj) = nothing end # names are consecutive integers: return tuple From 8e31be2b90d54d2178ffc7c1dc462e0b93c3700d Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Thu, 23 Feb 2023 02:43:29 +0100 Subject: [PATCH 2/3] make tests pass on julia v1.10- --- src/ConstructionBase.jl | 8 ++++++-- test/runtests.jl | 34 ++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/ConstructionBase.jl b/src/ConstructionBase.jl index 61ee2af..1ec59bb 100644 --- a/src/ConstructionBase.jl +++ b/src/ConstructionBase.jl @@ -48,11 +48,13 @@ getfields(x::NamedTuple) = x getproperties(o::NamedTuple) = o getproperties(o::Tuple) = o +const CAN_CHECK_PROPERTIES_ARE_FIELDS = VERSION < v"1.10-" + function is_propertynames_overloaded(T::Type)::Bool which(propertynames, Tuple{T}).sig !== Tuple{typeof(propertynames), Any} end -if VERSION < v"1.10-" +if CAN_CHECK_PROPERTIES_ARE_FIELDS @generated function check_properties_are_fields(obj) if is_propertynames_overloaded(obj) return quote @@ -72,7 +74,9 @@ if VERSION < v"1.10-" end end else - check_properties_are_fields(obj) = nothing + function check_properties_are_fields(obj) + nothing + end end # names are consecutive integers: return tuple diff --git a/test/runtests.jl b/test/runtests.jl index fcc236a..a73edad 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -271,25 +271,27 @@ Base.getproperty(obj::FieldProps, name::Symbol) = getproperty(getfield(obj, :com x = FieldProps((a=1, b=:b)) @test constructorof(typeof(x)) === FieldProps @test getfields(x) === (components=(a=1, b=:b),) - res = @test_throws ErrorException setproperties(x, c=0) - msg = sprint(showerror, res.value) - @test occursin("overload", msg) - @test occursin("setproperties", msg) - @test occursin("FieldProps", msg) - @test_throws ErrorException setproperties(x, components=(a=1,b=:b)) - msg = sprint(showerror, res.value) - @test occursin("overload", msg) - @test occursin("setproperties", msg) - @test occursin("FieldProps", msg) - @test_throws ErrorException setproperties(x, a="aaa") - msg = sprint(showerror, res.value) - @test occursin("overload", msg) - @test occursin("setproperties", msg) - @test occursin("FieldProps", msg) + if ConstructionBase.CAN_CHECK_PROPERTIES_ARE_FIELDS + res = @test_throws ErrorException setproperties(x, c=0) + msg = sprint(showerror, res.value) + @test occursin("overload", msg) + @test occursin("setproperties", msg) + @test occursin("FieldProps", msg) + @test_throws ErrorException setproperties(x, components=(a=1,b=:b)) + msg = sprint(showerror, res.value) + @test occursin("overload", msg) + @test occursin("setproperties", msg) + @test occursin("FieldProps", msg) + @test_throws ErrorException setproperties(x, a="aaa") + msg = sprint(showerror, res.value) + @test occursin("overload", msg) + @test occursin("setproperties", msg) + @test occursin("FieldProps", msg) + end # == FieldProps((a="aaa", b=:b) if VERSION >= v"1.7" @test getproperties(x) == (a=1, b=:b) - else + elseif CAN_CHECK_PROPERTIES_ARE_FIELDS res = @test_throws ErrorException getproperties(x) msg = sprint(showerror, res.value) @test occursin("overload", msg) From 24055433e82c9f02ab49d19ac2ea4964d30d2f07 Mon Sep 17 00:00:00 2001 From: Jan Weidner Date: Thu, 23 Feb 2023 02:47:52 +0100 Subject: [PATCH 3/3] fix --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index a73edad..7e59425 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -291,7 +291,7 @@ Base.getproperty(obj::FieldProps, name::Symbol) = getproperty(getfield(obj, :com # == FieldProps((a="aaa", b=:b) if VERSION >= v"1.7" @test getproperties(x) == (a=1, b=:b) - elseif CAN_CHECK_PROPERTIES_ARE_FIELDS + elseif ConstructionBase.CAN_CHECK_PROPERTIES_ARE_FIELDS res = @test_throws ErrorException getproperties(x) msg = sprint(showerror, res.value) @test occursin("overload", msg)