Skip to content

Commit 36d062d

Browse files
authored
Allow non-Function args to retry (#30382)
I hit this while trying to use a `PyObject`
1 parent 236c69b commit 36d062d

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

NEWS.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ New library functions
3333
Standard library changes
3434
------------------------
3535

36-
* The `extrema` function now accepts a function argument in the same manner as `minimum` and
37-
`maximum` ([#30323]).
38-
* `hasmethod` can now check for matching keyword argument names ([#30712]).
39-
* `startswith` and `endswith` now accept a `Regex` for the second argument ([#29790]).
36+
* The `extrema` function now accepts a function argument in the same manner as `minimum` and
37+
`maximum` ([#30323]).
38+
* `hasmethod` can now check for matching keyword argument names ([#30712]).
39+
* `startswith` and `endswith` now accept a `Regex` for the second argument ([#29790]).
40+
* `retry` supports arbitrary callable objects ([#30382]).
4041

4142
#### LinearAlgebra
4243

@@ -86,6 +87,7 @@ Deprecated or removed
8687
[#30323]: https://github.com/JuliaLang/julia/issues/30323
8788
[#30349]: https://github.com/JuliaLang/julia/issues/30349
8889
[#30372]: https://github.com/JuliaLang/julia/issues/30372
90+
[#30382]: https://github.com/JuliaLang/julia/issues/30382
8991
[#30583]: https://github.com/JuliaLang/julia/issues/30583
9092
[#30584]: https://github.com/JuliaLang/julia/issues/30584
9193
[#30593]: https://github.com/JuliaLang/julia/issues/30593

base/error.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,16 @@ length(ebo::ExponentialBackOff) = ebo.n
221221
eltype(::Type{ExponentialBackOff}) = Float64
222222

223223
"""
224-
retry(f::Function; delays=ExponentialBackOff(), check=nothing) -> Function
224+
retry(f; delays=ExponentialBackOff(), check=nothing) -> Function
225225
226226
Return an anonymous function that calls function `f`. If an exception arises,
227227
`f` is repeatedly called again, each time `check` returns `true`, after waiting the
228228
number of seconds specified in `delays`. `check` should input `delays`'s
229229
current state and the `Exception`.
230230
231+
!!! compat "Julia 1.2"
232+
Before Julia 1.2 this signature was restricted to `f::Function`.
233+
231234
# Examples
232235
```julia
233236
retry(f, delays=fill(5.0, 3))
@@ -237,7 +240,7 @@ retry(http_get, check=(s,e)->e.status == "503")(url)
237240
retry(read, check=(s,e)->isa(e, IOError))(io, 128; all=false)
238241
```
239242
"""
240-
function retry(f::Function; delays=ExponentialBackOff(), check=nothing)
243+
function retry(f; delays=ExponentialBackOff(), check=nothing)
241244
(args...; kwargs...) -> begin
242245
y = iterate(delays)
243246
while y !== nothing

test/error.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,7 @@ end
7777
foo_kwargs(x; y=5) = x + y
7878
@test retry(foo_kwargs)(3) == 8
7979
@test retry(foo_kwargs)(3; y=4) == 7
80+
81+
# non-Functions
82+
@test retry(Float64)(1) === 1.0
8083
end

0 commit comments

Comments
 (0)