Skip to content

Commit 664e6ba

Browse files
committed
Fix support for empty units
When units are "", Unitful doesn't know what to and fails. It doesn't fail normally, instead, it fails with a `MethodError`. This commit treats that case specially.
1 parent 0ff18d1 commit 664e6ba

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ julia> reordered_var.dims |> keys |> collect
3939
control the number of characters on each line of the legend of the box plot
4040
- Use default marker size instead of a marker size of 20 when plotting other models beside
4141
`CliMA` on the box plot
42+
- Fix support for `""` in units.
4243

4344
v0.5.8
4445
------

ext/ClimaAnalysisUnitfulExt.jl

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Try converting `value` to a `Uniftul` object. If unsuccessful, just return it.
1515
"""
1616
function Var._maybe_convert_to_unitful(value)
1717
value isa Unitful.Units && return value
18+
# "" cannot be parsed but returns a wrong error (MethodError on lookup_units),
19+
# so we handle it manually
20+
value == "" && return value
21+
1822
# This function in inherently type-unstable
1923
try
2024
return Unitful.uparse(value)

test/test_Var.jl

+7
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,13 @@ end
784784
data,
785785
)
786786

787+
var_empty_unit = ClimaAnalysis.OutputVar(
788+
Dict{String, Any}("units" => ""),
789+
Dict("long" => long),
790+
dim_attributes,
791+
data,
792+
)
793+
787794
@test ClimaAnalysis.has_units(var_with_unitful)
788795

789796
# Convert to cm/s

0 commit comments

Comments
 (0)