Skip to content

Unitful-like API shims#199

Open
MilesCranmerBot wants to merge 16 commits intoJuliaPhysics:mainfrom
MilesCranmerBot:unitful-api-compat
Open

Unitful-like API shims#199
MilesCranmerBot wants to merge 16 commits intoJuliaPhysics:mainfrom
MilesCranmerBot:unitful-api-compat

Conversation

@MilesCranmerBot
Copy link
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
Copy link
Contributor

github-actions bot commented Feb 14, 2026

Benchmark Results (Julia v1.10)

Time benchmarks
main 5e3592a... main / 5e3592a...
Quantity/creation/Quantity(x) 2.03 ± 0.003 ns 2.32 ± 0.003 ns 0.877 ± 0.0017
Quantity/creation/Quantity(x, length=y) 2.6 ± 0.006 ns 2.61 ± 0.006 ns 0.998 ± 0.0032
Quantity/with_numbers/*real 2.33 ± 0.003 ns 2.32 ± 0.002 ns 1 ± 0.0016
Quantity/with_numbers/^int 7.49 ± 2 ns 7.5 ± 2 ns 1 ± 0.38
Quantity/with_numbers/^int * real 7.78 ± 2.3 ns 7.53 ± 2 ns 1.03 ± 0.41
Quantity/with_quantity/+y 3.76 ± 0.014 ns 3.76 ± 0.016 ns 0.999 ± 0.0056
Quantity/with_quantity//y 2.33 ± 0.28 ns 2.61 ± 0.005 ns 0.893 ± 0.11
Quantity/with_self/dimension 2.32 ± 0.003 ns 2.04 ± 0.003 ns 1.14 ± 0.0022
Quantity/with_self/inv 2.33 ± 0.009 ns 2.61 ± 0.003 ns 0.893 ± 0.0036
Quantity/with_self/ustrip 2.31 ± 0.004 ns 2.03 ± 0.004 ns 1.14 ± 0.003
QuantityArray/broadcasting/multi_array_of_quantities 0.158 ± 0.006 ms 0.157 ± 0.0053 ms 1 ± 0.051
QuantityArray/broadcasting/multi_normal_array 0.0525 ± 0.0011 ms 0.0525 ± 0.00099 ms 1 ± 0.029
QuantityArray/broadcasting/multi_quantity_array 0.17 ± 0.012 ms 0.169 ± 0.011 ms 1 ± 0.098
QuantityArray/broadcasting/x^2_array_of_quantities 0.0398 ± 0.003 ms 0.0371 ± 0.0033 ms 1.07 ± 0.13
QuantityArray/broadcasting/x^2_normal_array 8.31 ± 1.1 μs 7.96 ± 1 μs 1.04 ± 0.19
QuantityArray/broadcasting/x^2_quantity_array 8.93 ± 0.93 μs 8.94 ± 0.98 μs 1 ± 0.15
QuantityArray/broadcasting/x^4_array_of_quantities 0.0954 ± 0.0036 ms 0.0947 ± 0.0033 ms 1.01 ± 0.052
QuantityArray/broadcasting/x^4_normal_array 0.0495 ± 0.001 ms 0.0465 ± 0.00095 ms 1.06 ± 0.031
QuantityArray/broadcasting/x^4_quantity_array 0.0498 ± 0.0026 ms 0.0496 ± 0.001 ms 1 ± 0.056
time_to_load 0.199 ± 0.0012 s 0.2 ± 0.00061 s 0.997 ± 0.0069
Memory benchmarks
main 5e3592a... main / 5e3592a...
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
Copy link
Contributor

github-actions bot commented Feb 14, 2026

Benchmark Results (Julia v1)

Time benchmarks
main 5e3592a... main / 5e3592a...
Quantity/creation/Quantity(x) 2.79 ± 0 ns 2.79 ± 0 ns 1 ± 0
Quantity/creation/Quantity(x, length=y) 3.11 ± 0.01 ns 3.42 ± 0.01 ns 0.909 ± 0.004
Quantity/with_numbers/*real 3.11 ± 0.01 ns 4.03 ± 0.01 ns 0.771 ± 0.0031
Quantity/with_numbers/^int 12.4 ± 0.99 ns 12.1 ± 0.99 ns 1.03 ± 0.12
Quantity/with_numbers/^int * real 11.8 ± 1 ns 12 ± 1.1 ns 0.977 ± 0.12
Quantity/with_quantity/+y 4.04 ± 0.01 ns 4.04 ± 0.011 ns 1 ± 0.0037
Quantity/with_quantity//y 3.11 ± 0 ns 3.11 ± 0.001 ns 1 ± 0.00032
Quantity/with_self/dimension 3.11 ± 0.01 ns 3.11 ± 0.01 ns 1 ± 0.0046
Quantity/with_self/inv 3.11 ± 0.01 ns 3.11 ± 0.001 ns 1 ± 0.0032
Quantity/with_self/ustrip 2.79 ± 0.01 ns 3.71 ± 0.011 ns 0.754 ± 0.0035
QuantityArray/broadcasting/multi_array_of_quantities 0.112 ± 0.0047 ms 0.112 ± 0.003 ms 1 ± 0.05
QuantityArray/broadcasting/multi_normal_array 0.0471 ± 0.00026 ms 0.0497 ± 0.00024 ms 0.947 ± 0.0069
QuantityArray/broadcasting/multi_quantity_array 0.056 ± 0.00021 ms 0.056 ± 0.00026 ms 1 ± 0.006
QuantityArray/broadcasting/x^2_array_of_quantities 22.2 ± 9.2 μs 23.7 ± 7.3 μs 0.938 ± 0.48
QuantityArray/broadcasting/x^2_normal_array 2.2 ± 2.2 μs 3.27 ± 2.1 μs 0.673 ± 0.82
QuantityArray/broadcasting/x^2_quantity_array 3.54 ± 1 μs 4.66 ± 1.6 μs 0.759 ± 0.34
QuantityArray/broadcasting/x^4_array_of_quantities 0.0814 ± 0.00077 ms 0.0814 ± 0.00093 ms 1 ± 0.015
QuantityArray/broadcasting/x^4_normal_array 0.0435 ± 0.0002 ms 0.0435 ± 0.00018 ms 1 ± 0.0062
QuantityArray/broadcasting/x^4_quantity_array 0.0529 ± 0.0058 ms 0.0529 ± 0.0002 ms 0.999 ± 0.11
time_to_load 0.189 ± 0.0018 s 0.188 ± 0.00061 s 1.01 ± 0.01
Memory benchmarks
main 5e3592a... main / 5e3592a...
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.149 k allocs: 11.2 kB 0.145 k allocs: 11 kB 1.02

@codecov
Copy link

codecov bot commented Feb 15, 2026

Codecov Report

❌ Patch coverage is 83.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 99.00%. Comparing base (505cb31) to head (5e3592a).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/utils.jl 75.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #199      +/-   ##
==========================================
- Coverage   99.14%   99.00%   -0.15%     
==========================================
  Files          21       23       +2     
  Lines        1292     1308      +16     
==========================================
+ Hits         1281     1295      +14     
- Misses         11       13       +2     

☔ View full report in Codecov by Sentry.
📢 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
Member

@codex review

Copy link
Member

@MilesCranmer MilesCranmer left a comment

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

Copy link

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

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".

Co-authored-by: Miles Cranmer <miles.cranmer@gmail.com>
@MilesCranmerBot
Copy link
Contributor Author

MilesCranmerBot commented Feb 15, 2026

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
Copy link
Contributor Author

MilesCranmerBot commented Mar 8, 2026

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 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
Member

Choose a reason for hiding this comment

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

@MilesCranmerBot not needed

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