Skip to content

Unitful-like API shims - #199

Open
MilesCranmerBot wants to merge 17 commits into
JuliaPhysics:mainfrom
MilesCranmerBot:unitful-api-compat
Open

Unitful-like API shims#199
MilesCranmerBot wants to merge 17 commits into
JuliaPhysics:mainfrom
MilesCranmerBot:unitful-api-compat

Conversation

@MilesCranmerBot

Copy link
Copy Markdown
Contributor

Adds a small set of Unitful-shaped API shims (wrappers/aliases) to ease downstream compatibility while keeping DynamicQuantities semantics intact.

Included:

  • dimensions(x) alias for dimension(x)
  • unit(q) (and unit(::Number))
  • isunitless/isdimensionless
  • upreferred(x) no-op
  • uparse(unit, x/s)
  • ustrip(::Type{T}, unit, q)

Tests: local Pkg.test() passed.

MilesCranmerBot and others added 2 commits February 14, 2026 21:41
Add common Unitful-shaped helpers (unit/dimensions/isunitless/upreferred/uparse).

Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
@github-actions

github-actions Bot commented Feb 14, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1.10)

Time benchmarks
main 2576490... main / 2576490...
Quantity/creation/Quantity(x) 3.48 ± 0.01 ns 3.13 ± 0.01 ns 1.11 ± 0.0048
Quantity/creation/Quantity(x, length=y) 4.52 ± 1 ns 3.49 ± 0.01 ns 1.3 ± 0.3
Quantity/with_numbers/*real 3.49 ± 1 ns 3.13 ± 0.01 ns 1.11 ± 0.33
Quantity/with_numbers/^int 8.39 ± 2.8 ns 8.04 ± 2.4 ns 1.04 ± 0.47
Quantity/with_numbers/^int * real 8.39 ± 2.4 ns 8.39 ± 2.4 ns 1 ± 0.41
Quantity/with_quantity/+y 4.15 ± 0.069 ns 4.15 ± 0.05 ns 1 ± 0.021
Quantity/with_quantity//y 3.49 ± 0.01 ns 3.49 ± 0.01 ns 1 ± 0.0041
Quantity/with_self/dimension 3.48 ± 0.001 ns 3.13 ± 0.01 ns 1.11 ± 0.0036
Quantity/with_self/inv 3.48 ± 0.01 ns 3.49 ± 0.01 ns 1 ± 0.0041
Quantity/with_self/ustrip 3.13 ± 0.001 ns 3.13 ± 0.001 ns 1 ± 0.00045
QuantityArray/broadcasting/multi_array_of_quantities 0.151 ± 0.01 ms 0.151 ± 0.01 ms 1 ± 0.098
QuantityArray/broadcasting/multi_normal_array 0.0561 ± 0.00024 ms 0.0563 ± 0.0035 ms 0.996 ± 0.062
QuantityArray/broadcasting/multi_quantity_array 0.173 ± 0.0019 ms 0.173 ± 0.0016 ms 1 ± 0.014
QuantityArray/broadcasting/x^2_array_of_quantities 25.8 ± 1.6 μs 26.1 ± 1.5 μs 0.988 ± 0.083
QuantityArray/broadcasting/x^2_normal_array 3.87 ± 0.38 μs 3.91 ± 0.37 μs 0.99 ± 0.14
QuantityArray/broadcasting/x^2_quantity_array 8.01 ± 0.45 μs 7.99 ± 0.44 μs 1 ± 0.079
QuantityArray/broadcasting/x^4_array_of_quantities 0.0872 ± 0.0024 ms 0.0872 ± 0.0016 ms 1 ± 0.034
QuantityArray/broadcasting/x^4_normal_array 0.0526 ± 0.0033 ms 0.0561 ± 0.0029 ms 0.937 ± 0.077
QuantityArray/broadcasting/x^4_quantity_array 0.0563 ± 0.0034 ms 0.0562 ± 0.0035 ms 1 ± 0.087
time_to_load 0.208 ± 0.00099 s 0.209 ± 0.0016 s 0.995 ± 0.0089
Memory benchmarks
main 2576490... main / 2576490...
Quantity/creation/Quantity(x) 0 allocs: 0 B 0 allocs: 0 B
Quantity/creation/Quantity(x, length=y) 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/*real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int * real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity/+y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity//y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/dimension 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/inv 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/ustrip 0 allocs: 0 B 0 allocs: 0 B
QuantityArray/broadcasting/multi_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/multi_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/multi_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^2_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^2_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^2_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^4_array_of_quantities 2 allocs: 0.382 MB 2 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^4_normal_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
QuantityArray/broadcasting/x^4_quantity_array 2 allocs: 0.0763 MB 2 allocs: 0.0763 MB 1
time_to_load 0.153 k allocs: 14.5 kB 0.153 k allocs: 14.5 kB 1

@github-actions

github-actions Bot commented Feb 14, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1)

Time benchmarks
main 2576490... main / 2576490...
Quantity/creation/Quantity(x) 3.41 ± 0.001 ns 2.79 ± 0.009 ns 1.22 ± 0.0039
Quantity/creation/Quantity(x, length=y) 3.1 ± 0.01 ns 3.41 ± 0.01 ns 0.909 ± 0.004
Quantity/with_numbers/*real 4.02 ± 0.91 ns 3.1 ± 0.01 ns 1.3 ± 0.29
Quantity/with_numbers/^int 12.1 ± 0.94 ns 12.1 ± 1 ns 0.999 ± 0.12
Quantity/with_numbers/^int * real 12.1 ± 1.3 ns 12 ± 0.99 ns 1.01 ± 0.14
Quantity/with_quantity/+y 4.04 ± 0.041 ns 4.04 ± 0.001 ns 1 ± 0.01
Quantity/with_quantity//y 3.11 ± 0.01 ns 3.41 ± 0.01 ns 0.912 ± 0.004
Quantity/with_self/dimension 3.41 ± 0.001 ns 3.1 ± 0.01 ns 1.1 ± 0.0036
Quantity/with_self/inv 3.1 ± 0.01 ns 3.1 ± 0.01 ns 0.997 ± 0.0045
Quantity/with_self/ustrip 3.1 ± 0.009 ns 2.79 ± 0.01 ns 1.11 ± 0.0051
QuantityArray/broadcasting/multi_array_of_quantities 0.113 ± 0.0027 ms 0.109 ± 0.0053 ms 1.03 ± 0.055
QuantityArray/broadcasting/multi_normal_array 0.0497 ± 0.00024 ms 0.0472 ± 0.00026 ms 1.05 ± 0.0077
QuantityArray/broadcasting/multi_quantity_array 0.0535 ± 0.0031 ms 0.053 ± 0.0031 ms 1.01 ± 0.083
QuantityArray/broadcasting/x^2_array_of_quantities 23.8 ± 7.6 μs 25.3 ± 6.9 μs 0.938 ± 0.39
QuantityArray/broadcasting/x^2_normal_array 2.57 ± 2.3 μs 2.96 ± 3.1 μs 0.868 ± 1.2
QuantityArray/broadcasting/x^2_quantity_array 3.58 ± 1.1 μs 6.69 ± 0.39 μs 0.534 ± 0.16
QuantityArray/broadcasting/x^4_array_of_quantities 0.0817 ± 0.0016 ms 0.0817 ± 0.0012 ms 1 ± 0.024
QuantityArray/broadcasting/x^4_normal_array 0.0436 ± 0.00016 ms 0.0436 ± 0.00016 ms 1 ± 0.0052
QuantityArray/broadcasting/x^4_quantity_array 0.0468 ± 0.0092 ms 0.0529 ± 0.0087 ms 0.884 ± 0.23
time_to_load 0.189 ± 0.0013 s 0.192 ± 0.001 s 0.987 ± 0.0084
Memory benchmarks
main 2576490... main / 2576490...
Quantity/creation/Quantity(x) 0 allocs: 0 B 0 allocs: 0 B
Quantity/creation/Quantity(x, length=y) 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/*real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_numbers/^int * real 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity/+y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_quantity//y 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/dimension 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/inv 0 allocs: 0 B 0 allocs: 0 B
Quantity/with_self/ustrip 0 allocs: 0 B 0 allocs: 0 B
QuantityArray/broadcasting/multi_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/multi_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/multi_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^2_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^2_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^2_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^4_array_of_quantities 3 allocs: 0.382 MB 3 allocs: 0.382 MB 1
QuantityArray/broadcasting/x^4_normal_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
QuantityArray/broadcasting/x^4_quantity_array 3 allocs: 0.0764 MB 3 allocs: 0.0764 MB 1
time_to_load 0.145 k allocs: 11 kB 0.145 k allocs: 11 kB 1

@codecov

codecov Bot commented Feb 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.02%. Comparing base (505cb31) to head (2576490).
⚠️ Report is 23 commits behind head on main.

Files with missing lines Patch % Lines
src/utils.jl 77.77% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #199      +/-   ##
==========================================
- Coverage   99.14%   99.02%   -0.13%     
==========================================
  Files          21       23       +2     
  Lines        1292     1327      +35     
==========================================
+ Hits         1281     1314      +33     
- Misses         11       13       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

MilesCranmerBot and others added 9 commits February 15, 2026 09:27
Add missing docstrings and adjust Documenter filters so the Documentation job passes.
Unitful exports  (singular); remove the  alias/export and update docs/tests accordingly.
Drop dimensions alias; make uparse parse unit strings only; stop exporting NoUnits/dimensionless sentinels.
Give isdimensionless its own docstring (alias of isunitless) to avoid confusing copied docs.
Move the Unitful-like shim assertions next to existing unit/uparse/ustrip coverage.
@MilesCranmer

Copy link
Copy Markdown
Member

@codex review

@MilesCranmer MilesCranmer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is looking good. Will be useful for improving API compatibility with Unitful

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4ffb5f868c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/utils.jl Outdated
Comment thread src/utils.jl Outdated
Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
@MilesCranmerBot

MilesCranmerBot commented Feb 15, 2026

Copy link
Copy Markdown
Contributor Author

Fixed: simplified unit shim to just unit(t) = oneunit(t) (covers quantities + numbers without extra overloads). Pushed in 1c82806.

MilesCranmerBot and others added 2 commits March 8, 2026 18:06
@MilesCranmerBot

MilesCranmerBot commented Mar 8, 2026

Copy link
Copy Markdown
Contributor Author

Agreed — matching Unitful: I made isunitless/isdimensionless scalar-only (arrays now throw MethodError; use broadcasting: isunitless.(x)). Pushed in commit 5e3592a to branch unitful-api-compat.

Comment thread src/utils.jl Outdated
Comment on lines +434 to +439

!!! note
Like Unitful.jl, `isunitless` is only defined for scalars. For arrays, use broadcasting:
`isunitless.(x)`.

See also [`isdimensionless`](@ref).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@MilesCranmerBot not needed

Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants