Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions src/ConstructionBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,34 @@ 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

@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 CAN_CHECK_PROPERTIES_ARE_FIELDS
@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
function check_properties_are_fields(obj)
nothing
end
end

Expand Down
34 changes: 18 additions & 16 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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 ConstructionBase.CAN_CHECK_PROPERTIES_ARE_FIELDS
res = @test_throws ErrorException getproperties(x)
msg = sprint(showerror, res.value)
@test occursin("overload", msg)
Expand Down