From 7c0d26ce967de35ab71a93d344cdd6dc950ac567 Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Mon, 14 Oct 2019 19:12:41 +0100 Subject: [PATCH 1/3] Add functionality for checking versions of packages --- Project.toml | 1 + src/ChainRules.jl | 2 ++ src/glue_utils.jl | 74 ++++++++++++++++++++++++++++++++++++++++++++++ test/glue_utils.jl | 4 +++ test/runtests.jl | 2 ++ 5 files changed, 83 insertions(+) create mode 100644 src/glue_utils.jl create mode 100644 test/glue_utils.jl diff --git a/Project.toml b/Project.toml index 442a0fb6f..63e18f9e7 100644 --- a/Project.toml +++ b/Project.toml @@ -5,6 +5,7 @@ version = "0.2.1" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" Requires = "ae029012-a4dd-5104-9daa-d747884805df" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" diff --git a/src/ChainRules.jl b/src/ChainRules.jl index d430c36bd..bcf7d4e11 100644 --- a/src/ChainRules.jl +++ b/src/ChainRules.jl @@ -12,6 +12,7 @@ const accumulate! = ChainRulesCore.accumulate! using LinearAlgebra using LinearAlgebra.BLAS using Requires +using Pkg: Pkg using Statistics using Base.Broadcast: materialize, materialize!, broadcasted, Broadcasted, broadcastable @@ -22,6 +23,7 @@ if VERSION < v"1.3.0-DEV.142" import LinearAlgebra: dot end +include("glue_utils.jl") include("helper_functions.jl") include("rulesets/Base/base.jl") diff --git a/src/glue_utils.jl b/src/glue_utils.jl new file mode 100644 index 000000000..c544d284b --- /dev/null +++ b/src/glue_utils.jl @@ -0,0 +1,74 @@ +""" + @version_spec_str(str::String) + +Returns a `VersionSpec` object which represents the range of compatible verions, +for a given Pkg3 style semver compat specifier. + +Example: +```jldoctest +julia> v"2.0.1" ∈ ChainRules.version_spec"1.2" +false + +julia> v"1.5.1" ∈ ChainRules.version_spec"1.2" +true + +julia> v"2.0.1" ∈ ChainRules.version_spec"1, 2" +true + +julia> v"1.0.1" ∈ ChainRules.version_spec"1, 2" +true + +julia> v"1.3.0" ∈ ChainRules.version_spec"~1.0, ~1.1" +false + +julia> v"1.1.2" ∈ ChainRules.version_spec"~1.0, ~1.1" +true +""" +macro version_spec_str(str::String) + return Pkg.Types.semver_spec(str) +end + +""" + pkg_version(_module::Module) + +Returns the version of the package that defined a given module. +Does not work on the current module, or on standard libraries +""" +pkg_version(_module::Module) + +@static if VERSION ∈ version_spec"~1.0" + function pkg_version(_module::Module) + pkg_id = Base.PkgId(_module) + env = Pkg.Types.Context().env + pkg_info = Pkg.Types.manifest_info(env, pkg_id.uuid) + return VersionNumber(pkg_info["version"]) + end +elseif VERSION ∈ version_spec"~1.1" + function pkg_version(_module::Module) + pkg_id = Base.PkgId(_module) + env = Pkg.Types.Context().env + pkg_info = Pkg.Types.manifest_info(env, pkg_id.uuid) + return VersionNumber(pkg_info.version) + end +elseif VERSION ∈ version_spec"~1.2" + function pkg_version(_module::Module) + pkg_id = Base.PkgId(_module) + env = Pkg.Types.Context().env + pkg_info = Pkg.Types.manifest_info(env, pkg_id.uuid) + return pkg_info.version + end +elseif VERSION ∈ version_spec"~1.3" + function pkg_version(_module::Module) + pkg_id = Base.PkgId(_module) + env = Pkg.Types.Context().env + pkg_info = Pkg.Types.manifest_info(env, pkg_id.uuid) + return pkg_info.version + end +else # tested in 1.4.0-DEV.265 + function pkg_version(_module::Module) + pkg_id = Base.PkgId(_module) + ctx = Pkg.Types.Context() + pkg_info = Pkg.Types.manifest_info(ctx, pkg_id.uuid) + return pkg_info.version + end +end diff --git a/test/glue_utils.jl b/test/glue_utils.jl new file mode 100644 index 000000000..b14f9dfe5 --- /dev/null +++ b/test/glue_utils.jl @@ -0,0 +1,4 @@ +@testset "Package version checking" begin + # This test needs to be updated when we allow a new version of ChainRulesCore + @test ChainRules.pkg_version(ChainRulesCore) ∈ ChainRules.version_spec"0.3" +end diff --git a/test/runtests.jl b/test/runtests.jl index 7379bb2f5..6792f1281 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -18,6 +18,8 @@ include("test_util.jl") println("Testing ChainRules.jl") @testset "ChainRules" begin + include("glue_utils.jl") + exit() include("helper_functions.jl") @testset "rulesets" begin @testset "Base" begin From 8f25500d396420f20735781abfd05a7b1707df29 Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Mon, 14 Oct 2019 19:25:18 +0100 Subject: [PATCH 2/3] simplify checking of versions --- src/glue_utils.jl | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/glue_utils.jl b/src/glue_utils.jl index c544d284b..3b059732e 100644 --- a/src/glue_utils.jl +++ b/src/glue_utils.jl @@ -50,14 +50,7 @@ elseif VERSION ∈ version_spec"~1.1" pkg_info = Pkg.Types.manifest_info(env, pkg_id.uuid) return VersionNumber(pkg_info.version) end -elseif VERSION ∈ version_spec"~1.2" - function pkg_version(_module::Module) - pkg_id = Base.PkgId(_module) - env = Pkg.Types.Context().env - pkg_info = Pkg.Types.manifest_info(env, pkg_id.uuid) - return pkg_info.version - end -elseif VERSION ∈ version_spec"~1.3" +elseif VERSION ∈ version_spec"~1.2, ~1.3" function pkg_version(_module::Module) pkg_id = Base.PkgId(_module) env = Pkg.Types.Context().env From 87ba65d04992c16faa8df6cf4b2c9d10573bbf0a Mon Sep 17 00:00:00 2001 From: Lyndon White Date: Mon, 14 Oct 2019 19:28:09 +0100 Subject: [PATCH 3/3] begin splitting the SpecialFunctions into versions [SquashMe] --- src/rulesets/packages/SpecialFunctions.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/rulesets/packages/SpecialFunctions.jl b/src/rulesets/packages/SpecialFunctions.jl index 30088fc25..02e441a47 100644 --- a/src/rulesets/packages/SpecialFunctions.jl +++ b/src/rulesets/packages/SpecialFunctions.jl @@ -3,7 +3,6 @@ using ChainRulesCore using ..SpecialFunctions -@scalar_rule(SpecialFunctions.lgamma(x), SpecialFunctions.digamma(x)) @scalar_rule(SpecialFunctions.erf(x), (2 / sqrt(π)) * exp(-x * x)) @scalar_rule(SpecialFunctions.erfc(x), -(2 / sqrt(π)) * exp(-x * x)) @scalar_rule(SpecialFunctions.erfi(x), (2 / sqrt(π)) * exp(x * x)) @@ -24,4 +23,10 @@ using ..SpecialFunctions @scalar_rule(SpecialFunctions.erfcx(x), (2 * x * Ω) - (2 / sqrt(π))) @scalar_rule(SpecialFunctions.dawson(x), 1 - (2 * x * Ω)) +@static if pkg_version(SpecialFunctions) < v"0.8" + @scalar_rule(SpecialFunctions.lgamma(x), SpecialFunctions.digamma(x)) +else + @scalar_rule(SpecialFunctions.loggamma(x), SpecialFunctions.digamma(x)) +end + end #module