Skip to content

Commit 40a236a

Browse files
skip @inferred tests for PkgEval (#165)
They can also be manually disabled by setting `CHAINRULES_TEST_INFERRED` to `false`. I have tested that all tests still pass with `JULIA_PKGEVAL` set to `true`, do we need to test for that by spawning a new Julia process? Once this is merged, I plan to replace occurences of `@inferred` in ChainRules with `@maybe_inferred` as well, therefore I exported that macro. Co-authored-by: Lyndon White <oxinabox@ucc.asn.au>
1 parent 3bde970 commit 40a236a

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

docs/src/index.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,36 @@ Test Failed at REPL[52]:1
147147
ERROR: There was an error during testing
148148
```
149149
which should have passed the test.
150+
151+
## Inference tests
152+
153+
By default, all functions for testing rules check whether the output type (as well as that of the pullback for `rrule`s) can be completely inferred, such that everything is type stable:
154+
155+
```jldoctest ex
156+
julia> function ChainRulesCore.rrule(::typeof(abs), x)
157+
abs_pullback(Δ) = (NoTangent(), x >= 0 ? Δ : big(-1.0) * Δ)
158+
return abs(x), abs_pullback
159+
end
160+
161+
julia> test_rrule(abs, 1.)
162+
test_rrule: abs on Float64: Error During Test at /home/runner/work/ChainRulesTestUtils.jl/ChainRulesTestUtils.jl/src/testers.jl:170
163+
Got exception outside of a @test
164+
return type Tuple{ChainRulesCore.NoTangent, Float64} does not match inferred return type Tuple{ChainRulesCore.NoTangent, Union{Float64, BigFloat}}
165+
[...]
166+
```
167+
168+
This can be disabled on a per-rule basis using the `check_inferred` keyword argument:
169+
170+
```jldoctest ex
171+
julia> test_rrule(abs, 1.; check_inferred=false)
172+
Test Summary: | Pass Total
173+
test_rrule: abs on Float64 | 5 5
174+
Test.DefaultTestSet("test_rrule: abs on Float64", Any[], 5, false, false)
175+
```
176+
177+
This behavior can also be overridden globally by setting the environment variable `CHAINRULES_TEST_INFERRED` before ChainRulesTestUtils is loaded or by changing `ChainRulesTestUtils.TEST_INFERRED[]` from inside Julia.
178+
ChainRulesTestUtils can detect whether a test is run as part of [PkgEval](https://github.com/JuliaCI/PkgEval.jl)and in this case disables inference tests automatically. Packages can use [`@maybe_inferred`](@ref) to get the same behavior for other inference tests.
179+
150180
# API Documentation
151181

152182
```@autodocs

src/ChainRulesTestUtils.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,22 @@ using Test
1212
import FiniteDifferences: rand_tangent
1313

1414
const _fdm = central_fdm(5, 1; max_range=1e-2)
15+
const TEST_INFERRED = Ref(true)
1516

1617
export TestIterator
1718
export test_approx, test_scalar, test_frule, test_rrule, generate_well_conditioned_matrix
1819
export
20+
export @maybe_inferred
1921

22+
function __init__()
23+
TEST_INFERRED[] = if haskey(ENV, "CHAINRULES_TEST_INFERRED")
24+
parse(Bool, "CHAINRULES_TEST_INFERRED")
25+
else
26+
!parse(Bool, get(ENV, "JULIA_PKGEVAL", "false"))
27+
end
28+
29+
!TEST_INFERRED[] && @warn "inference tests have been disabled"
30+
end
2031

2132
include("generate_tangent.jl")
2233
include("data_generation.jl")

src/testers.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,17 @@ function _ensure_not_running_on_functor(f, name)
246246
end
247247
end
248248

249+
"""
250+
@maybe_inferred f(...)
251+
252+
Like `@inferred`, but does not check the return type if tests are run as part of PkgEval or
253+
if the environment variable `CHAINRULES_TEST_INFERRED` is set to `false`.
254+
"""
255+
macro maybe_inferred(ex)
256+
inferred = Expr(:macrocall, GlobalRef(Test, Symbol("@inferred")), __source__, ex)
257+
return :(TEST_INFERRED[] ? $(esc(inferred)) : $(esc(ex)))
258+
end
259+
249260
"""
250261
_test_inferred(f, args...; kwargs...)
251262
@@ -254,9 +265,9 @@ knowing how many `kwargs` there are.
254265
"""
255266
function _test_inferred(f, args...; kwargs...)
256267
if isempty(kwargs)
257-
@inferred f(args...)
268+
@maybe_inferred f(args...)
258269
else
259-
@inferred f(args...; kwargs...)
270+
@maybe_inferred f(args...; kwargs...)
260271
end
261272
end
262273

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ using LinearAlgebra
55
using Random
66
using Test
77

8+
# in these meta tests, we always want to use `@inferred`
9+
ChainRulesTestUtils.TEST_INFERRED[] = true
10+
811
@testset "ChainRulesTestUtils.jl" begin
912
include("meta_testing_tools.jl")
1013
include("iterator.jl")

0 commit comments

Comments
 (0)