Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Unitful.uparse() #639

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/user.jl
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,12 @@ julia> uparse("1.0*dB")
1.0 dB
```
"""
function uparse(str; unit_context=Unitful)
function uparse(str; unit_context=vcat(Unitful, Unitful.unitmodules))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for this change, I just spent way too long trying to figure out why the imported unit was not parsing, the error message of lookup_units also was not super helpful

ex = Meta.parse(str)
eval(lookup_units(unit_context, ex))
end

const allowed_funcs = [:*, :/, :^, :sqrt, :√, :+, :-, ://]
const allowed_funcs = [:*, :/, :^, :sqrt, :√, :+, :-, ://, :colon, :(:)]
function lookup_units(unitmods, ex::Expr)
if ex.head == :call
ex.args[1] in allowed_funcs ||
Expand All @@ -684,6 +684,11 @@ function lookup_units(unitmods, ex::Expr)
end
end
return ex
elseif ex.head == :macrocall
if ex.args[1] != Symbol("@u_str")
throw(ArgumentError("Cannot use macro $(ex.args[1]), only use macro @u_str"))
end
return eval(ex)
else
throw(ArgumentError("Expr head $(ex.head) must equal :call or :tuple"))
end
Expand Down
12 changes: 12 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1950,10 +1950,22 @@ end

# Tests for unit extension modules in unit parsing
@test_throws ArgumentError uparse("foo", unit_context=Unitful)
@test uparse("foo") === u"foo" #This work, because default `unit_context` is now `vcat(Unitful, Unitful.unitmodules)`
@test uparse("foo", unit_context=FooUnits) === u"foo"
@test uparse("foo", unit_context=[Unitful, FooUnits]) === u"foo"
@test uparse("foo", unit_context=[FooUnits, Unitful]) === u"foo"

module BazUnits
using Unitful
@unit baz "baz" MyBaz 1u"m" false
end
# Tests unit parsing for unit extension modules in which Unitful.register(BazUnits) was not done
@test_throws ArgumentError uparse("baz", unit_context=Unitful)
@test_throws ArgumentError uparse("baz") #This not work, because BazUnits is not registered to Unitful.unitmodules ( and default `unit_context` argument )
@test uparse("baz", unit_context=BazUnits) === BazUnits.baz
@test uparse("baz", unit_context=[Unitful, BazUnits]) === BazUnits.baz
@test uparse("baz", unit_context=[BazUnits, Unitful]) === BazUnits.baz

# Test for #272
module OnlyUstrImported
import Unitful: @u_str
Expand Down