Skip to content

Commit 7abc54c

Browse files
sjkellysostock
andauthored
Add Aqua and resolve some method ambiguities (#606)
* add Aqua and resolve some ambiguities This resolves several ambiguities detected by Aqua. * Update test/runtests.jl Co-authored-by: Sebastian Stock <[email protected]> * Update src/types.jl Co-authored-by: Sebastian Stock <[email protected]> * Update Project.toml --------- Co-authored-by: Sebastian Stock <[email protected]>
1 parent 00cf44b commit 7abc54c

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
99
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1010

1111
[compat]
12+
Aqua = "0.6"
1213
ConstructionBase = "1"
1314
julia = "1"
1415

1516
[extras]
17+
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
1618
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1719
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1820
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1921

2022
[targets]
21-
test = ["LinearAlgebra", "Test", "Random"]
23+
test = ["Aqua", "LinearAlgebra", "Test", "Random"]

src/logarithm.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,24 +202,24 @@ Base. -(x::Gain, y::Level) = throw(ArgumentError("cannot subtract a level from a
202202
leveltype(x::Level{L,S}) where {L,S} = Level{L,S}
203203
Base. *(x::Level, y::Number) = (leveltype(x))(x.val * y)
204204
Base. *(x::Level, y::Bool) = (leveltype(x))(x.val * y) # for method ambiguity
205-
Base. *(x::Level, y::Quantity) = *(x.val, y)
205+
Base. *(x::Level, y::AbstractQuantity) = *(x.val, y)
206206
Base. *(x::Level, y::Level) = *(x.val, y.val)
207207
Base. *(x::Level, y::Gain) = *(promote(x,y)...)
208208

209209
Base. *(x::Number, y::Level) = *(y,x)
210210
Base. *(x::Bool, y::Level) = *(y,x) # for method ambiguity
211-
Base. *(x::Quantity, y::Level) = *(y,x) # for method ambiguity
211+
Base. *(x::AbstractQuantity, y::Level) = *(y,x) # for method ambiguity
212212

213213
gaintype(::Gain{L,S}) where {L,S} = Gain{L,S}
214214
Base. *(x::Gain, y::Number) = (gaintype(x))(x.val * y)
215215
Base. *(x::Gain, y::Bool) = (gaintype(x))(x.val * y) # for method ambiguity
216-
Base. *(x::Gain, y::Quantity) = *(y,x)
216+
Base. *(x::Gain, y::AbstractQuantity) = *(y,x)
217217
Base. *(x::Gain, y::Level) = *(promote(x,y)...)
218218
Base. *(x::Gain, y::Gain) = *(promote(x,y)...)
219219

220220
Base. *(x::Number, y::Gain) = *(y,x)
221221
Base. *(x::Bool, y::Gain) = *(y,x) # for method ambiguity
222-
Base. *(x::Quantity, y::Gain) =
222+
Base. *(x::AbstractQuantity, y::Gain) =
223223
isrootpower(x) ? uconvertrp(NoUnits, y) * x : uconvertp(NoUnits, y) * x
224224

225225
for (op1,op2) in ((:*, :+), (:/, :-))
@@ -242,18 +242,18 @@ Base. /(x::Gain, y::Level) = throw(ArgumentError("cannot divide a gain by a leve
242242

243243
Base. /(x::Level, y::Number) = (leveltype(x))(linear(x) / y)
244244
Base. //(x::Level, y::Number) = (leveltype(x))(linear(x) // y)
245-
Base. /(x::Level, y::Quantity) = linear(x) / y
246-
Base. //(x::Level, y::Quantity) = linear(x) // y
245+
Base. /(x::Level, y::AbstractQuantity) = linear(x) / y
246+
Base. //(x::Level, y::AbstractQuantity) = linear(x) // y
247247
Base. /(x::Level, y::Level) = linear(x) / linear(y)
248248
Base. //(x::Level, y::Level) = linear(x) // linear(y)
249249
Base. //(x::Level, y::Complex) = linear(x) // y # ambiguity resolution
250250

251251
Base. //(x::Number, y::Level) = x // linear(y)
252-
Base. /(x::Quantity, y::Level) = x / linear(y)
253-
Base. //(x::Quantity, y::Level) = x // linear(y)
254-
Base. /(x::Quantity, y::Gain) =
252+
Base. /(x::AbstractQuantity, y::Level) = x / linear(y)
253+
Base. //(x::AbstractQuantity, y::Level) = x // linear(y)
254+
Base. /(x::AbstractQuantity, y::Gain) =
255255
isrootpower(x) ? x / uconvertrp(NoUnits, y) : x / uconvertp(NoUnits, y)
256-
Base. //(x::Quantity, y::Gain) =
256+
Base. //(x::AbstractQuantity, y::Gain) =
257257
isrootpower(x) ? x // uconvertrp(NoUnits, y) : x // uconvertp(NoUnits, y)
258258

259259
Base. //(x::Level, y::Units) = x/y

src/quantities.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ for (_x,_y) in [(:fma, :_fma), (:muladd, :_muladd)]
189189
@eval @inline ($_x)(x::AbstractQuantity, y::Number, z::AbstractQuantity) = ($_y)(x,y,z)
190190

191191
# Post-promotion
192-
@eval @inline ($_x)(x::AbstractQuantity{A}, y::AbstractQuantity{B}, z::AbstractQuantity{C}) where {
193-
A <: Number, B <: Number, C <: Number} = ($_y)(x,y,z)
192+
@eval @inline ($_x)(x::AbstractQuantity, y::AbstractQuantity, z::AbstractQuantity) = ($_y)(x,y,z)
194193

195194
# It seems like most of this is optimized out by the compiler, including the
196195
# apparent runtime check of dimensions, which does not appear in @code_llvm.

src/types.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ For example, given a gain of `20dB`, we have `val===20`. This type differs from
267267
struct Gain{L, S, T<:Real} <: LogScaled{L}
268268
val::T
269269
end
270+
Unitful.Gain{L, S, T}(x::Unitful.Gain{L, S, T}) where {L,S,T<:Real} = x # for ambiguity resolution
270271

271272
"""
272273
struct MixedUnits{T<:LogScaled, U<:Units}

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,3 +2145,7 @@ finally
21452145
rm(load_path, recursive=true)
21462146
rm(load_cache_path, recursive=true)
21472147
end
2148+
2149+
using Aqua
2150+
2151+
Aqua.test_all(Unitful, ambiguities=false, unbound_args=false, piracy=VERSIONv"1.8")

0 commit comments

Comments
 (0)