From 93562288b1a8a543053f0f43c4058508d7f444fb Mon Sep 17 00:00:00 2001 From: Oliver Schulz Date: Mon, 12 Jun 2023 11:47:32 +0200 Subject: [PATCH] Improve support for InverseFunctions (#652) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Support InverseFunctions on Julia < v1.9 * Document ustrip support for InverseFunctions * Fix up whitespace in test/runtests.jl Co-authored-by: Mosè Giordano * Move ustrip inverse docs * Fix ustrip inverse docs Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> * Improve Aqua tests Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> * Update Aqua config in tests Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> * Require Aqua v0.6.3 Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> --------- Co-authored-by: Mosè Giordano Co-authored-by: Sebastian Stock <42280794+sostock@users.noreply.github.com> --- Project.toml | 16 +++++++++------- docs/Project.toml | 1 + docs/src/manipulations.md | 1 + src/Unitful.jl | 4 ++++ src/utils.jl | 7 +++++++ test/runtests.jl | 4 ++-- 6 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Project.toml b/Project.toml index 2f877280..83de5193 100644 --- a/Project.toml +++ b/Project.toml @@ -5,17 +5,22 @@ version = "1.14.0" [deps] ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9" Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" +InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" -[compat] -Aqua = "0.6" -ConstructionBase = "1" -julia = "1" +[weakdeps] +InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" [extensions] InverseFunctionsUnitfulExt = "InverseFunctions" +[compat] +Aqua = "0.6.3" +ConstructionBase = "1" +InverseFunctions = "0.1" +julia = "1" + [extras] Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" @@ -25,6 +30,3 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [targets] test = ["Aqua", "InverseFunctions", "LinearAlgebra", "Test", "Random"] - -[weakdeps] -InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" diff --git a/docs/Project.toml b/docs/Project.toml index ff62cc79..db359a7e 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,6 +1,7 @@ [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" +InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316" Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a" diff --git a/docs/src/manipulations.md b/docs/src/manipulations.md index e0b11945..852c2483 100644 --- a/docs/src/manipulations.md +++ b/docs/src/manipulations.md @@ -1,6 +1,7 @@ ```@meta DocTestSetup = quote using Unitful + using InverseFunctions end ``` # Manipulating units diff --git a/src/Unitful.jl b/src/Unitful.jl index ed34b1d5..8e3f9295 100644 --- a/src/Unitful.jl +++ b/src/Unitful.jl @@ -71,4 +71,8 @@ include("complex.jl") include("pkgdefaults.jl") include("dates.jl") +if !isdefined(Base, :get_extension) + include("../ext/InverseFunctionsUnitfulExt.jl") +end + end diff --git a/src/utils.jl b/src/utils.jl index c0975ce5..e57df1b7 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -24,6 +24,13 @@ true julia> ustrip(Float64, u"m", 2u"mm") == 0.002 true ``` + +`ustrip` supports `InverseFunctions.inverse`: + +```jldoctest +julia> inverse(Base.Fix1(ustrip, u"m"))(5) +5 m +``` """ @inline ustrip(u::Units, x) = ustrip(uconvert(u, x)) @inline ustrip(T::Type, u::Units, x) = convert(T, ustrip(u, x)) diff --git a/test/runtests.jl b/test/runtests.jl index 05eeb0af..75a54750 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -85,7 +85,7 @@ const colon = Base.:(:) @test ConstructionBase.constructorof(typeof(1.0m))(2) === 2m end -VERSION >= v"1.9-" && @testset "inverse" begin +@testset "inverse" begin InverseFunctions.test_inverse(Base.Fix1(ustrip, m), 2m) InverseFunctions.test_inverse(Base.Fix1(ustrip, m), 2mm) end @@ -2178,4 +2178,4 @@ end using Aqua -Aqua.test_all(Unitful, ambiguities=VERSION≥v"1.1", unbound_args=false, piracy=VERSION≥v"1.8") +Aqua.test_all(Unitful, ambiguities=VERSION≥v"1.1", unbound_args=false, piracy=VERSION≥v"1.8", project_toml_formatting=VERSION≥v"1.6")