Skip to content

Commit 70f73c5

Browse files
st--oxinabox
andauthored
JuliaFormatter & workflow (#482)
* format(".") * format workflow * format with blue style * Update src/rule_definition_tools.jl * Revert "format with blue style" This reverts commit 8ddb4ac. * Revert "format(".")" This reverts commit 2783a90. * format(".") with blue style from scratch * manual format cleanup * Update test/tangent_types/tangent.jl * Revert "format(".") with blue style from scratch" This reverts commit 8b54ef6. * add .JuliaFormatter.toml with style = blue * format(".") - hopefully actually with BlueStyle this time around!!! * Update .github/workflows/format.yml Co-authored-by: Lyndon White <oxinabox@ucc.asn.au> * Apply suggestions from code review Co-authored-by: Lyndon White <oxinabox@ucc.asn.au> * fix suggestion * manual formatting workaround for domluna/JuliaFormatter.jl#484 * Update src/rule_definition_tools.jl Co-authored-by: Lyndon White <oxinabox@ucc.asn.au> * Update src/rule_definition_tools.jl * Update src/rule_definition_tools.jl * Update src/rule_definition_tools.jl Co-authored-by: Lyndon White <oxinabox@ucc.asn.au>
1 parent 834901e commit 70f73c5

26 files changed

+427
-369
lines changed

.JuliaFormatter.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
style = "blue"

.github/workflows/format.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Format suggestions
2+
3+
on:
4+
pull_request:
5+
6+
concurrency:
7+
# Skip intermediate builds: always.
8+
# Cancel intermediate builds: only if it is a pull request build.
9+
group: ${{ github.workflow }}-${{ github.ref }}
10+
cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}
11+
12+
jobs:
13+
format:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- uses: julia-actions/setup-julia@latest
18+
with:
19+
version: 1
20+
- run: |
21+
julia -e 'using Pkg; Pkg.add("JuliaFormatter")'
22+
julia -e 'using JuliaFormatter; format("."; verbose=true)'
23+
- uses: reviewdog/action-suggester@v1
24+
with:
25+
tool_name: JuliaFormatter
26+
fail_on_error: true
27+
filter_mode: added

docs/make.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ DocMeta.setdocmeta!(
1616
@scalar_rule(sin(x), cos(x)) # frule and rrule doctest
1717
@scalar_rule(sincos(x), @setup((sinx, cosx) = Ω), cosx, -sinx) # frule doctest
1818
@scalar_rule(hypot(x::Real, y::Real), (x / Ω, y / Ω)) # rrule doctest
19-
end
19+
end,
2020
)
2121

2222
indigo = DocThemeIndigo.install(ChainRulesCore)
2323

24-
makedocs(
24+
makedocs(;
2525
modules=[ChainRulesCore],
26-
format=Documenter.HTML(
26+
format=Documenter.HTML(;
2727
prettyurls=false,
2828
assets=[indigo],
2929
mathengine=MathJax3(
3030
Dict(
3131
:tex => Dict(
32-
"inlineMath" => [["\$","\$"], ["\\(","\\)"]],
32+
"inlineMath" => [["\$", "\$"], ["\\(", "\\)"]],
3333
"tags" => "ams",
3434
# TODO: remove when using physics package
3535
"macros" => Dict(
@@ -67,7 +67,4 @@ makedocs(
6767
checkdocs=:exports,
6868
)
6969

70-
deploydocs(
71-
repo = "github.com/JuliaDiff/ChainRulesCore.jl.git",
72-
push_preview=true,
73-
)
70+
deploydocs(; repo="github.com/JuliaDiff/ChainRulesCore.jl.git", push_preview=true)

docs/src/assets/make_logo.jl

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,79 +9,76 @@ using Random
99
const bridge_len = 50
1010

1111
function chain(jiggle=0)
12-
shaky_rotate(θ) = rotate+ jiggle*(rand()-0.5))
13-
12+
shaky_rotate(θ) = rotate+ jiggle * (rand() - 0.5))
13+
1414
### 1
1515
shaky_rotate(0)
1616
sethue(Luxor.julia_red)
1717
link()
1818
m1 = getmatrix()
19-
20-
19+
2120
### 2
2221
sethue(Luxor.julia_green)
23-
translate(-50, 130);
24-
shaky_rotate/3);
22+
translate(-50, 130)
23+
shaky_rotate / 3)
2524
link()
2625
m2 = getmatrix()
27-
26+
2827
setmatrix(m1)
2928
sethue(Luxor.julia_red)
3029
overlap(-1.3π)
3130
setmatrix(m2)
32-
31+
3332
### 3
34-
shaky_rotate(-π/3);
35-
translate(-120,80);
33+
shaky_rotate(-π / 3)
34+
translate(-120, 80)
3635
sethue(Luxor.julia_purple)
3736
link()
38-
37+
3938
setmatrix(m2)
4039
setcolor(Luxor.julia_green)
41-
overlap(-1.5π)
40+
return overlap(-1.5π)
4241
end
4342

44-
4543
function link()
4644
sector(50, 90, π, 0, :fill)
4745
sector(Point(0, bridge_len), 50, 90, 0, -π, :fill)
48-
49-
50-
rect(50,-3,40, bridge_len+6, :fill)
51-
rect(-50-40,-3,40, bridge_len+6, :fill)
52-
46+
47+
rect(50, -3, 40, bridge_len + 6, :fill)
48+
rect(-50 - 40, -3, 40, bridge_len + 6, :fill)
49+
5350
sethue("black")
5451
move(Point(-50, bridge_len))
55-
arc(Point(0,0), 50, π, 0, :stoke)
52+
arc(Point(0, 0), 50, π, 0, :stoke)
5653
arc(Point(0, bridge_len), 50, 0, -π, :stroke)
57-
54+
5855
move(Point(-90, bridge_len))
59-
arc(Point(0,0), 90, π, 0, :stoke)
56+
arc(Point(0, 0), 90, π, 0, :stoke)
6057
arc(Point(0, bridge_len), 90, 0, -π, :stroke)
61-
strokepath()
58+
return strokepath()
6259
end
6360

6461
function overlap(ang_end)
65-
sector(Point(0, bridge_len), 50, 90, -0., ang_end, :fill)
62+
sector(Point(0, bridge_len), 50, 90, -0.0, ang_end, :fill)
6663
sethue("black")
6764
arc(Point(0, bridge_len), 50, 0, ang_end, :stoke)
6865
move(Point(90, bridge_len))
6966
arc(Point(0, bridge_len), 90, 0, ang_end, :stoke)
7067

71-
strokepath()
68+
return strokepath()
7269
end
7370

7471
# Actually draw it
7572

7673
function save_logo(filename)
7774
Random.seed!(16)
78-
Drawing(450,450, filename)
75+
Drawing(450, 450, filename)
7976
origin()
80-
translate(50, -130);
77+
translate(50, -130)
8178
chain(0.5)
8279
finish()
83-
preview()
80+
return preview()
8481
end
8582

8683
save_logo("logo.svg")
87-
save_logo("logo.png")
84+
save_logo("logo.png")

src/accumulation.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ end
2626

2727
add!!(x::AbstractArray, y::Thunk) = add!!(x, unthunk(y))
2828

29-
function add!!(x::AbstractArray{<:Any, N}, y::AbstractArray{<:Any, N}) where N
29+
function add!!(x::AbstractArray{<:Any,N}, y::AbstractArray{<:Any,N}) where {N}
3030
return if is_inplaceable_destination(x)
3131
x .+= y
3232
else
3333
x + y
3434
end
3535
end
3636

37-
3837
"""
3938
is_inplaceable_destination(x) -> Bool
4039
@@ -64,7 +63,6 @@ end
6463
is_inplaceable_destination(::LinearAlgebra.Hermitian) = false
6564
is_inplaceable_destination(::LinearAlgebra.Symmetric) = false
6665

67-
6866
function debug_add!(accumuland, t::InplaceableThunk)
6967
returned_value = t.add!(accumuland)
7068
if returned_value !== accumuland
@@ -88,7 +86,7 @@ function Base.showerror(io::IO, err::BadInplaceException)
8886
if err.accumuland == err.returned_value
8987
println(
9088
io,
91-
"Which in this case happenned to be equal. But they are not the same object."
89+
"Which in this case happenned to be equal. But they are not the same object.",
9290
)
9391
end
9492
end

src/compat.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ end
55
if VERSION < v"1.1"
66
# Note: these are actually *better* than the ones in julia 1.1, 1.2, 1.3,and 1.4
77
# See: https://github.com/JuliaLang/julia/issues/34292
8-
function fieldtypes(::Type{T}) where T
8+
function fieldtypes(::Type{T}) where {T}
99
if @generated
1010
ntuple(i -> fieldtype(T, i), fieldcount(T))
1111
else
1212
ntuple(i -> fieldtype(T, i), fieldcount(T))
1313
end
1414
end
1515

16-
function fieldnames(::Type{T}) where T
16+
function fieldnames(::Type{T}) where {T}
1717
if @generated
1818
ntuple(i -> fieldname(T, i), fieldcount(T))
1919
else

src/config.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ that do not support performing forwards mode AD should be `RuleConfig{>:NoForwar
6464
"""
6565
struct NoForwardsMode <: ForwardsModeCapability end
6666

67-
6867
"""
6968
frule_via_ad(::RuleConfig{>:HasForwardsMode}, ȧrgs, f, args...; kwargs...)
7069

src/deprecated.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

src/ignore_derivatives.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ ignore_derivatives(x) = x
4545
Tells the AD system to ignore the expression. Equivalent to `ignore_derivatives() do (...) end`.
4646
"""
4747
macro ignore_derivatives(ex)
48-
return :(ChainRulesCore.ignore_derivatives() do
49-
$(esc(ex))
50-
end)
48+
return :(
49+
ChainRulesCore.ignore_derivatives() do
50+
$(esc(ex))
51+
end
52+
)
5153
end

src/projection.jl

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ ProjectTo{P}() where {P} = ProjectTo{P}(EMPTY_NT)
3232

3333
const Type_kwfunc = Core.kwftype(Type).instance
3434
function (::typeof(Type_kwfunc))(kws::Any, ::Type{ProjectTo{P}}) where {P}
35-
ProjectTo{P}(NamedTuple(kws))
35+
return ProjectTo{P}(NamedTuple(kws))
3636
end
3737

3838
Base.getproperty(p::ProjectTo, name::Symbol) = getproperty(backing(p), name)
@@ -131,7 +131,10 @@ ProjectTo(::AbstractZero) = ProjectTo{NoTangent}() # Any x::Zero in forward pas
131131
# Also, any explicit construction with fields, where all fields project to zero, itself
132132
# projects to zero. This simplifies projectors for wrapper types like Diagonal([true, false]).
133133
const _PZ = ProjectTo{<:AbstractZero}
134-
ProjectTo{P}(::NamedTuple{T, <:Tuple{_PZ, Vararg{<:_PZ}}}) where {P,T} = ProjectTo{NoTangent}()
134+
const _PZ_Tuple = Tuple{_PZ,Vararg{<:_PZ}} # 1 or more ProjectTo{<:AbstractZeros}
135+
function ProjectTo{P}(::NamedTuple{T,<:_PZ_Tuple}) where {P,T}
136+
return ProjectTo{NoTangent}()
137+
end
135138

136139
# Tangent
137140
# We haven't entirely figured out when to convert Tangents to "natural" representations such as
@@ -164,12 +167,16 @@ for T in (Float16, Float32, Float64, ComplexF16, ComplexF32, ComplexF64)
164167
end
165168

166169
# In these cases we can just `convert` as we know we are dealing with plain and simple types
167-
(::ProjectTo{T})(dx::AbstractFloat) where T<:AbstractFloat = convert(T, dx)
168-
(::ProjectTo{T})(dx::Integer) where T<:AbstractFloat = convert(T, dx) #needed to avoid ambiguity
170+
(::ProjectTo{T})(dx::AbstractFloat) where {T<:AbstractFloat} = convert(T, dx)
171+
(::ProjectTo{T})(dx::Integer) where {T<:AbstractFloat} = convert(T, dx) #needed to avoid ambiguity
169172
# simple Complex{<:AbstractFloat}} cases
170-
(::ProjectTo{T})(dx::Complex{<:AbstractFloat}) where {T<:Complex{<:AbstractFloat}} = convert(T, dx)
173+
function (::ProjectTo{T})(dx::Complex{<:AbstractFloat}) where {T<:Complex{<:AbstractFloat}}
174+
return convert(T, dx)
175+
end
171176
(::ProjectTo{T})(dx::AbstractFloat) where {T<:Complex{<:AbstractFloat}} = convert(T, dx)
172-
(::ProjectTo{T})(dx::Complex{<:Integer}) where {T<:Complex{<:AbstractFloat}} = convert(T, dx)
177+
function (::ProjectTo{T})(dx::Complex{<:Integer}) where {T<:Complex{<:AbstractFloat}}
178+
return convert(T, dx)
179+
end
173180
(::ProjectTo{T})(dx::Integer) where {T<:Complex{<:AbstractFloat}} = convert(T, dx)
174181

175182
# Other numbers, including e.g. ForwardDiff.Dual and Symbolics.Sym, should pass through.
@@ -244,9 +251,11 @@ end
244251
# although really Ref() is probably a better structure.
245252
function (project::ProjectTo{AbstractArray})(dx::Number) # ... so we restore from numbers
246253
if !(project.axes isa Tuple{})
247-
throw(DimensionMismatch(
248-
"array with ndims(x) == $(length(project.axes)) > 0 cannot have dx::Number",
249-
))
254+
throw(
255+
DimensionMismatch(
256+
"array with ndims(x) == $(length(project.axes)) > 0 cannot have dx::Number"
257+
),
258+
)
250259
end
251260
return fill(project.element(dx))
252261
end
@@ -298,7 +307,9 @@ function (project::ProjectTo{Adjoint})(dx::AbstractArray)
298307
return adjoint(project.parent(dy))
299308
end
300309

301-
ProjectTo(x::LinearAlgebra.TransposeAbsVec) = ProjectTo{Transpose}(; parent=ProjectTo(parent(x)))
310+
function ProjectTo(x::LinearAlgebra.TransposeAbsVec)
311+
return ProjectTo{Transpose}(; parent=ProjectTo(parent(x)))
312+
end
302313
function (project::ProjectTo{Transpose})(dx::LinearAlgebra.AdjOrTransAbsVec)
303314
return transpose(project.parent(transpose(dx)))
304315
end
@@ -316,10 +327,8 @@ ProjectTo(x::Diagonal) = ProjectTo{Diagonal}(; diag=ProjectTo(x.diag))
316327
(project::ProjectTo{Diagonal})(dx::Diagonal) = Diagonal(project.diag(dx.diag))
317328

318329
# Symmetric
319-
for (SymHerm, chk, fun) in (
320-
(:Symmetric, :issymmetric, :transpose),
321-
(:Hermitian, :ishermitian, :adjoint),
322-
)
330+
for (SymHerm, chk, fun) in
331+
((:Symmetric, :issymmetric, :transpose), (:Hermitian, :ishermitian, :adjoint))
323332
@eval begin
324333
function ProjectTo(x::$SymHerm)
325334
sub = ProjectTo(parent(x))

0 commit comments

Comments
 (0)