diff --git a/MakieCore/src/recipes.jl b/MakieCore/src/recipes.jl index 660965e37bc..6cdbb71e28a 100644 --- a/MakieCore/src/recipes.jl +++ b/MakieCore/src/recipes.jl @@ -670,9 +670,13 @@ plottype(plot_args...) = Plot{plot} # default to dispatch to type recipes! deprecated_attributes(_) = () struct InvalidAttributeError <: Exception - plottype::Type + type::Type + object_name::String # Generic name like plot, block attributes::Set{Symbol} end +function InvalidAttributeError(::Type{PT}, attributes::Set{Symbol}) where {PT <: Plot} + return InvalidAttributeError(PT, "plot", attributes) +end function print_columns(io::IO, v::Vector{String}; gapsize = 2, rows_first = true, cols = displaysize(io)[2]) @@ -718,26 +722,22 @@ function print_columns(io::IO, v::Vector{String}; gapsize = 2, rows_first = true return end -__obj_name(_) = "plot" -__valid_attributes(p) = attribute_names(p) -__has_generic_attributes(_) = true - -function Base.showerror(io::IO, i::InvalidAttributeError) - n = length(i.attributes) +function Base.showerror(io::IO, err::InvalidAttributeError) + n = length(err.attributes) print(io, "Invalid attribute$(n > 1 ? "s" : "") ") - for (j, att) in enumerate(i.attributes) - j > 1 && print(io, j == length(i.attributes) ? " and " : ", ") + for (j, att) in enumerate(err.attributes) + j > 1 && print(io, j == length(err.attributes) ? " and " : ", ") printstyled(io, att; color = :red, bold = true) end - print(io, " for $(__obj_name(i.plottype)) type ") - printstyled(io, i.plottype; color = :blue, bold = true) + print(io, " for $(err.object_name) type ") + printstyled(io, err.type; color = :blue, bold = true) println(io, ".") - nameset = sort(string.(collect(__valid_attributes(i.plottype)))) + nameset = sort(string.(collect(attribute_names(err.type)))) println(io) - println(io, "The available $(__obj_name(i.plottype)) attributes for $(i.plottype) are:") + println(io, "The available $(err.object_name) attributes for $(err.type) are:") println(io) print_columns(io, nameset; cols = displaysize(stderr)[2], rows_first = true) - if __has_generic_attributes(i.plottype) + if err.type isa Plot allowlist = attribute_name_allowlist() println(io) println(io) diff --git a/src/makielayout/blocks.jl b/src/makielayout/blocks.jl index 2dae30502df..d90f2921731 100644 --- a/src/makielayout/blocks.jl +++ b/src/makielayout/blocks.jl @@ -288,21 +288,23 @@ function block_defaults(::Type{B}, attribute_kwargs::Dict, scene::Union{Nothing, return attributes end -MakieCore.__obj_name(::Type{<:Block}) = "block" -function MakieCore.__valid_attributes(T::Type{S}) where {S<:Block} +function MakieCore.InvalidAttributeError(::Type{BT}, attributes::Set{Symbol}) where {BT <: Block} + return MakieCore.InvalidAttributeError(BT, "block", attributes) +end + +function MakieCore.attribute_names(::Type{T}) where {T <: Block} attrs = _attribute_docs(T) # Some blocks have keyword arguments that are not attributes. # TODO: Refactor intiailize_block! to just not use kwargs? - (S <: Axis || S <: PolarAxis) && (attrs[:palette] = "") - S <: Legend && (attrs[:entrygroups] = "") - S <: Menu && (attrs[:default] = "") - S <: LScene && (attrs[:scenekw] = "") + (T <: Axis || T <: PolarAxis) && (attrs[:palette] = "") + T <: Legend && (attrs[:entrygroups] = "") + T <: Menu && (attrs[:default] = "") + T <: LScene && (attrs[:scenekw] = "") return keys(attrs) end -MakieCore.__has_generic_attributes(::Type{<:Block}) = false function _check_remaining_kwargs(T::Type{<:Block}, kwdict::Dict) - badnames = setdiff(keys(kwdict), MakieCore.__valid_attributes(T)) + badnames = setdiff(keys(kwdict), MakieCore.attribute_names(T)) if !isempty(badnames) throw(MakieCore.InvalidAttributeError(T, badnames)) end diff --git a/test/pipeline.jl b/test/pipeline.jl index b13cea906d7..a2e6d4d1ac2 100644 --- a/test/pipeline.jl +++ b/test/pipeline.jl @@ -148,10 +148,7 @@ end @test all(x -> x isa Volume, plots) end -import Makie.MakieCore: - __obj_name, - __valid_attributes, - __has_generic_attributes, +import Makie.MakieCore: InvalidAttributeError, attribute_names import Makie: _attribute_docs @@ -183,14 +180,13 @@ end @test testrecipe(1:4, 1:4, color=:red) isa Makie.FigureAxisPlot end -@testset "validated attributes for blocks" begin - @test __obj_name(Lines) == "plot" - @test __valid_attributes(Lines) == attribute_names(Lines) - @test __has_generic_attributes(Lines) +@testset "validated attributes for blocks" begin + err = InvalidAttributeError(Lines, Set{Symbol}()) + @test err.object_name == "plot" - @test __obj_name(Axis) == "block" - @test __valid_attributes(Axis3) == keys(_attribute_docs(Axis3)) - @test !__has_generic_attributes(Axis3) + err = InvalidAttributeError(Axis, Set{Symbol}()) + @test err.object_name == "block" + @test attribute_names(Axis3) == keys(_attribute_docs(Axis3)) fig = Figure() @test_throws InvalidAttributeError Axis(fig[1, 1], does_not_exist = 123) @@ -214,8 +210,8 @@ end @test Menu(fig[1, 2], default = nothing) isa Menu @test Legend(fig[1, 3], entrygroups = []) isa Legend @test PolarAxis(fig[1, 4], palette = nothing) isa PolarAxis - @test :palette in __valid_attributes(Axis) - @test :default in __valid_attributes(Menu) - @test :entrygroups in __valid_attributes(Legend) - @test :palette in __valid_attributes(PolarAxis) + @test :palette in attribute_names(Axis) + @test :default in attribute_names(Menu) + @test :entrygroups in attribute_names(Legend) + @test :palette in attribute_names(PolarAxis) end \ No newline at end of file