Skip to content

Commit 5ffbbd2

Browse files
committed
Use BlueStyle and JuliaFormatter (#246)
We have been using JuliaFormatter quite successfully for some while in the JuliaGaussianProcesses organization, and I think it would be good to adopt some style guide also in DynamicPPL and other TuringLang packages. This helps to keep the code base consistent and, in my opinion, increases readability and reduces mental overhead. I just chose [BlueStyle](https://github.com/invenia/BlueStyle) here since it is used in JuliaGaussianProcesses. I would be fine with YAS as well, I think a main criterion should just be that is supported by [JuliaFormatter](https://github.com/domluna/JuliaFormatter.jl) since this makes it much easier to format code correctly. Speaking about convenience, I also added a configuration for JuliaFormatter such that one can just run `format(path_to_repo)` to re-format the codebase correctly (actually, that's what I did when putting together the PR). Moreover, I added a Github action that will automatically check and report if PRs are formatted correctly. It will even post suggestions with format fixes (if required) which is even more convenient.
1 parent 4c659e3 commit 5ffbbd2

38 files changed

+828
-788
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Format
2+
3+
on:
4+
push:
5+
branches:
6+
# This is where pull requests from "bors r+" are built.
7+
- staging
8+
# This is where pull requests from "bors try" are built.
9+
- trying
10+
# Build the master branch.
11+
- master
12+
pull_request:
13+
14+
jobs:
15+
format:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- uses: actions/checkout@v2
19+
- uses: julia-actions/setup-julia@latest
20+
with:
21+
version: 1
22+
- name: Format code
23+
run: |
24+
using Pkg
25+
Pkg.add(; name="JuliaFormatter", uuid="98e50ef6-434e-11e9-1051-2b60c6c9e899")
26+
using JuliaFormatter
27+
format("."; verbose=true)
28+
shell: julia --color=yes {0}
29+
- uses: reviewdog/action-suggester@v1
30+
if: github.event_name == 'pull_request'
31+
with:
32+
tool_name: JuliaFormatter
33+
fail_on_error: true

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[![IntegrationTest](https://github.com/TuringLang/DynamicPPL.jl/workflows/IntegrationTest/badge.svg?branch=master)](https://github.com/TuringLang/DynamicPPL.jl/actions?query=workflow%3AIntegrationTest+branch%3Amaster)
66
[![Coverage Status](https://coveralls.io/repos/github/TuringLang/DynamicPPL.jl/badge.svg?branch=master)](https://coveralls.io/github/TuringLang/DynamicPPL.jl?branch=master)
77
[![Codecov](https://codecov.io/gh/TuringLang/DynamicPPL.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/TuringLang/DynamicPPL.jl)
8+
[![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle)
89
[![ColPrac: Contributor's Guide on Collaborative Practices for Community Packages](https://img.shields.io/badge/ColPrac-Contributor's%20Guide-blueviolet)](https://colprac.sciml.ai/)
910
[![Bors enabled](https://bors.tech/images/badge_small.svg)](https://app.bors.tech/repositories/24589)
1011

bors.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ status = [
1515
"test (1.%, windows-latest, x64, 2)",
1616
"test (1, windows-latest, x64, 1)",
1717
"test (1, windows-latest, x64, 2)",
18-
"Turing.jl"
18+
"Turing.jl",
19+
"format"
1920
]
2021
delete_merged_branches = true
2122
# Require at least on approval of a project member.

src/DynamicPPL.jl

Lines changed: 91 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -5,101 +5,101 @@ using AbstractPPL
55
using Distributions
66
using Bijectors
77

8-
import AbstractMCMC
9-
import ChainRulesCore
10-
import NaturalSort
11-
import MacroTools
12-
import ZygoteRules
8+
using AbstractMCMC: AbstractMCMC
9+
using ChainRulesCore: ChainRulesCore
10+
using NaturalSort: NaturalSort
11+
using MacroTools: MacroTools
12+
using ZygoteRules: ZygoteRules
1313

14-
import Random
14+
using Random: Random
1515

16-
import Base: Symbol,
17-
==,
18-
hash,
19-
getindex,
20-
setindex!,
21-
push!,
22-
show,
23-
isempty,
24-
empty!,
25-
getproperty,
26-
setproperty!,
27-
keys,
28-
haskey
16+
import Base:
17+
Symbol,
18+
==,
19+
hash,
20+
getindex,
21+
setindex!,
22+
push!,
23+
show,
24+
isempty,
25+
empty!,
26+
getproperty,
27+
setproperty!,
28+
keys,
29+
haskey
2930

3031
# VarInfo
31-
export AbstractVarInfo,
32-
VarInfo,
33-
UntypedVarInfo,
34-
TypedVarInfo,
35-
getlogp,
36-
setlogp!,
37-
acclogp!,
38-
resetlogp!,
39-
get_num_produce,
40-
set_num_produce!,
41-
reset_num_produce!,
42-
increment_num_produce!,
43-
set_retained_vns_del_by_spl!,
44-
is_flagged,
45-
set_flag!,
46-
unset_flag!,
47-
setgid!,
48-
updategid!,
49-
setorder!,
50-
istrans,
51-
link!,
52-
invlink!,
53-
tonamedtuple,
54-
# VarName (reexport from AbstractPPL)
55-
VarName,
56-
inspace,
57-
subsumes,
58-
@varname,
59-
# Compiler
60-
@model,
61-
# Utilities
62-
vectorize,
63-
reconstruct,
64-
reconstruct!,
65-
Sample,
66-
init,
67-
vectorize,
68-
# Model
69-
Model,
70-
getmissings,
71-
getargnames,
72-
generated_quantities,
73-
# Samplers
74-
Sampler,
75-
SampleFromPrior,
76-
SampleFromUniform,
77-
# Contexts
78-
DefaultContext,
79-
LikelihoodContext,
80-
PriorContext,
81-
MiniBatchContext,
82-
PrefixContext,
83-
assume,
84-
dot_assume,
85-
observer,
86-
dot_observe,
87-
tilde,
88-
dot_tilde,
89-
# Pseudo distributions
90-
NamedDist,
91-
NoDist,
92-
# Prob macros
93-
@prob_str,
94-
@logprob_str,
95-
# Convenience functions
96-
logprior,
97-
logjoint,
98-
pointwise_loglikelihoods,
99-
# Convenience macros
100-
@addlogprob!,
101-
@submodel
102-
32+
export AbstractVarInfo,
33+
VarInfo,
34+
UntypedVarInfo,
35+
TypedVarInfo,
36+
getlogp,
37+
setlogp!,
38+
acclogp!,
39+
resetlogp!,
40+
get_num_produce,
41+
set_num_produce!,
42+
reset_num_produce!,
43+
increment_num_produce!,
44+
set_retained_vns_del_by_spl!,
45+
is_flagged,
46+
set_flag!,
47+
unset_flag!,
48+
setgid!,
49+
updategid!,
50+
setorder!,
51+
istrans,
52+
link!,
53+
invlink!,
54+
tonamedtuple,
55+
# VarName (reexport from AbstractPPL)
56+
VarName,
57+
inspace,
58+
subsumes,
59+
@varname,
60+
# Compiler
61+
@model,
62+
# Utilities
63+
vectorize,
64+
reconstruct,
65+
reconstruct!,
66+
Sample,
67+
init,
68+
vectorize,
69+
# Model
70+
Model,
71+
getmissings,
72+
getargnames,
73+
generated_quantities,
74+
# Samplers
75+
Sampler,
76+
SampleFromPrior,
77+
SampleFromUniform,
78+
# Contexts
79+
DefaultContext,
80+
LikelihoodContext,
81+
PriorContext,
82+
MiniBatchContext,
83+
PrefixContext,
84+
assume,
85+
dot_assume,
86+
observer,
87+
dot_observe,
88+
tilde,
89+
dot_tilde,
90+
# Pseudo distributions
91+
NamedDist,
92+
NoDist,
93+
# Prob macros
94+
@prob_str,
95+
@logprob_str,
96+
# Convenience functions
97+
logprior,
98+
logjoint,
99+
pointwise_loglikelihoods,
100+
# Convenience macros
101+
@addlogprob!,
102+
@submodel
103103

104104
# Reexport
105105
using Distributions: loglikelihood
@@ -112,7 +112,6 @@ function getspace end
112112
abstract type AbstractVarInfo <: AbstractModelTrace end
113113
abstract type AbstractContext end
114114

115-
116115
include("utils.jl")
117116
include("selector.jl")
118117
include("model.jl")

src/compat/ad.jl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
# See https://github.com/TuringLang/Turing.jl/issues/1199
22
ChainRulesCore.@non_differentiable push!(
3-
vi::VarInfo,
4-
vn::VarName,
5-
r,
6-
dist::Distribution,
7-
gidset::Set{Selector}
3+
vi::VarInfo, vn::VarName, r, dist::Distribution, gidset::Set{Selector}
84
)
95

106
ChainRulesCore.@non_differentiable updategid!(
11-
vi::AbstractVarInfo,
12-
vn::VarName,
13-
spl::Sampler,
7+
vi::AbstractVarInfo, vn::VarName, spl::Sampler
148
)
159

1610
# https://github.com/TuringLang/Turing.jl/issues/1595
1711
ZygoteRules.@adjoint function dot_observe(
18-
spl::Union{SampleFromPrior, SampleFromUniform},
12+
spl::Union{SampleFromPrior,SampleFromUniform},
1913
dists::AbstractArray{<:Distribution},
2014
value::AbstractArray,
2115
vi,

0 commit comments

Comments
 (0)