JuliaPhysics / JuliaPhysics/Unitful.jl

[bug] `register` does not work with the module being used `as` alias

Open
#654 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
675
Forks
124
Avg merge
3h 38m
Merged PRs (30d)
1

Description

When using
```julia
Unitful.register(@__MODULE__)
```
the registered units, are linked to the module instead of the identifier. For this reason, the `@u_str` macro does not work when the module is imported with `as`.
# MWE
With the module
```julia
module MM
using Unitful

function __init__()
Unitful.register(@__MODULE__)
end

@unit px "px" Pixel 36u"nm" false
end
```
and in the REPL
```julia
In [1]: using MM: MM as M

In [2]: using Unitful

In [3]: u"px"
ERROR: LoadError: ArgumentError: Symbol `px` was found in the globally registered unit module MM
but was not in the provided list of unit modules Unitful.

(Consider `using MM` in your module if you are using `@u_str`?)
Stacktrace:
[1] lookup_units(unitmods::Vector{Module}, sym::Symbol)
@ Unitful ~/.julia/packages/Unitful/orvol/src/user.jl:701
[2] var"@u_str"(__source__::LineNumberNode, __module__::Module, unit::Any)
@ Unitful ~/.julia/packages/Unitful/orvol/src/user.jl:639
in expression starting at REPL[3]:1
```
but when you import it using the name where it is registered, it works as normal
```julia
In [4]: using MM

In [5]: u"px"
> px
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with src/user.jl around lookup_units at line 701 and the @u_str macro at line 639, then reproduce the module-alias MWE from the issue. Done means that after importing MM as M, u"px" resolves the registered unit without requiring the original module name.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.