Skip to content

Commit

Permalink
validate plot attributes later, for axis specific plot attributes (#3974
Browse files Browse the repository at this point in the history
)

* validate plot attributes later, for axis specific plot attributes

* add changelog

* make sure recipe subplots are checked too
  • Loading branch information
SimonDanisch authored Jul 7, 2024
1 parent 604ba5d commit de1dfc3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- validate plot attributes later, for axis specific plot attributes [#3974](https://github.com/MakieOrg/Makie.jl/pull/3974).

## [0.21.4] - 2024-07-02

- Fixed support for GLFW 3.4 on OSX [#3999](https://github.com/MakieOrg/Makie.jl/issues/3999).
Expand Down
4 changes: 3 additions & 1 deletion MakieCore/src/recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -722,11 +722,12 @@ function attribute_name_allowlist()
:dim_conversions, :cycle)
end

function validate_attribute_keys(P::Type{<:Plot}, kw::Dict{Symbol})
function validate_attribute_keys(plot::P) where {P<:Plot}
nameset = attribute_names(P)
nameset === nothing && return
allowlist = attribute_name_allowlist()
deprecations = deprecated_attributes(P)::Tuple{Vararg{NamedTuple{(:attribute, :message, :error), Tuple{Symbol, String, Bool}}}}
kw = plot.kw
unknown = setdiff(keys(kw), nameset, allowlist, first.(deprecations))
if !isempty(unknown)
throw(InvalidAttributeError(P, unknown))
Expand All @@ -741,4 +742,5 @@ function validate_attribute_keys(P::Type{<:Plot}, kw::Dict{Symbol})
end
end
end
return
end
1 change: 0 additions & 1 deletion MakieCore/src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ mutable struct Plot{PlotFunc, T} <: ScenePlot{PlotFunc}
kw::Dict{Symbol,Any}, args::Vector{Any}, converted::Vector{Observable},
deregister_callbacks::Vector{Observables.ObserverFunction}=Observables.ObserverFunction[]
) where {Typ,T}
validate_attribute_keys(Plot{Typ}, kw)
return new{Typ,T}(nothing, kw, args, converted, Attributes(), Plot[], deregister_callbacks)
end
end
Expand Down
4 changes: 3 additions & 1 deletion src/scenes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,13 @@ function Base.empty!(scene::Scene; free=false)
end

function Base.push!(plot::Plot, subplot)
MakieCore.validate_attribute_keys(subplot)
subplot.parent = plot
push!(plot.plots, subplot)
end

function Base.push!(scene::Scene, @nospecialize(plot::AbstractPlot))
function Base.push!(scene::Scene, @nospecialize(plot::Plot))
MakieCore.validate_attribute_keys(plot)
push!(scene.plots, plot)
for screen in scene.current_screens
Base.invokelatest(insert!, screen, scene, plot)
Expand Down
17 changes: 16 additions & 1 deletion test/pipeline.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ end
@test all(x -> x isa Volume, plots)
end

import Makie.MakieCore: InvalidAttributeError

@testset "validated attributes" begin
InvalidAttributeError = Makie.MakieCore.InvalidAttributeError
@test_throws InvalidAttributeError heatmap(zeros(10, 10); does_not_exist = 123)
@test_throws InvalidAttributeError image(zeros(10, 10); does_not_exist = 123)
@test_throws InvalidAttributeError scatter(1:10; does_not_exist = 123)
Expand All @@ -161,3 +162,17 @@ end
@test_throws InvalidAttributeError poly(Point2f[]; does_not_exist = 123)
@test_throws InvalidAttributeError mesh(rand(Point3f, 3); does_not_exist = 123)
end


@recipe(TestRecipe, x, y) do scene
Attributes()
end

function Makie.plot!(p::TestRecipe)
lines!(p, p.x, p.y; Makie.attributes(p)...)
end

@testset "recipe attribute checking" begin
@test_throws InvalidAttributeError testrecipe(1:4, 1:4, colour=:red)
@test testrecipe(1:4, 1:4, color=:red) isa Makie.FigureAxisPlot
end

0 comments on commit de1dfc3

Please sign in to comment.