Skip to content

Refuse shadowing ids in install_unit(), and add override to redefine database ids - #452

Merged
Enchufa2 merged 5 commits into
mainfrom
fix-install-shadowing
Nov 18, 2026
Merged

Enchufa2 merged 5 commits into
mainfrom
fix-install-shadowing

Conversation

@Enchufa2

Copy link
Copy Markdown
Member

Addresses #451. Follows up on #449.

Problem 1: installs that silently redefine or never reach a unit

install_unit() only checked whether an id was already taken in the map of its own kind. But udunits reads an identifier by name (case-insensitive), then by symbol, then the same after stripping a prefix. So on main:

install_unit(name = "m", def = "1 mile")
set_units(set_units(1, m), ft)      #> 5280 [ft]       meter redefined

install_unit("km", "1 mile")
set_units(set_units(1, km), m)      #> 1609.344 [m]    kilometre redefined

install_unit("gal", "1 gallon")     # accepted, but the database name "gal" wins
set_units(set_units(1, gal), L)     #> Error: cannot convert gal into L

Fix:

  • Refuse an id that udunits already reads as a different unit, by whatever route.
  • Refuse a name with a case variant that is read as a different unit. A name KG would make kg and every prefixed form of it read as the new unit. Names with at most 12 letters try every variant (a few ms). No database symbol, prefixed or not, is longer than 8 characters, so longer names are only checked against installed symbols.
  • After mapping, every id must be read back as the new unit (e.g. zz1 is read as zz¹). Otherwise everything is rolled back.
  • remove_unit() also decides by what udunits reads an id as, so remove_unit("km") now warns instead of silently doing nothing.

Problem 2: no way to redefine a database id (#451)

Before #449, remove_unit() followed by install_unit() could give a database id a new meaning. That is no longer possible. This PR adds install_unit(..., override = TRUE):

install_unit("gal", "1 gallon", override = TRUE)
#> redefined symbol "gal" (was 0.01 meter·second⁻²)
set_units(set_units(1, gal), L)     #> 3.785412 [L]
remove_unit("gal")                  # "gal" is cm/s^2 again
  • The database mappings that give the id another meaning are set aside: the id in both maps (a leftover name would win over a new symbol), and the old unit's own symbol or name when it is that id (otherwise, after overriding m, meter would still print as [m] and be read back as the new unit). They are recorded under the installed id, and remove_unit() restores them after undoing the install.
  • Setting aside, mapping and checking run as one transaction, rolled back on any failure.
  • A message reports what each id meant before, by whatever route (e.g. km, by prefix).
  • Still refused with override = TRUE: other ids that a new name would shadow (KG → kg), and ids installed by install_unit() ("remove it first"). Case variants of the name itself (e.g. microN for micron) are not collateral.
  • Prefixed forms follow the new meaning (kgal). Existing units objects that use the id are read with the new meaning too. Both are documented in ?install_unit.

Testing

  • New tests for every case above. Full suite: 827 tests, 0 failures. covr: no uncovered lines in src/user_units.cpp or R/user_conversion.R.
  • Sweep: every one of the 535 symbols and names in the database is overridden with a new base unit and then removed. All are accepted except %, which is refused on purpose. Afterwards, every id parses to the same unit and every unit has the same own symbol and name in all three encodings (UTF-8, Latin-1 and ASCII sessions).

Commits

  1. Refuse ids that udunits already reads as a different unit.
  2. Warn in remove_unit() about any id udunits reads as a unit.
  3. Add override argument to install_unit().
  4. NEWS.

🤖 Generated with Claude Code

Enchufa2 and others added 4 commits November 14, 2026 12:55
install_unit() only checked the map of the same kind, but udunits reads an
identifier by name (whatever its case), then by symbol, then the same after
stripping a prefix. So a new name "m" or symbol "km" silently redefined a
database unit, and a symbol "gal" was accepted but never reached, since the
database name "gal" wins over it.

- Refuse an id that udunits currently reads as a different unit.
- Refuse a name with a case variant that is read as a different unit (e.g.
  "KG" would shadow "kg"). Names with at most 12 letters try every variant;
  no database symbol, prefixed or not, is longer than 8 characters, so
  longer names are checked against installed symbols only.
- After mapping, every id must be read back as the new unit (e.g. "zz1" is
  read as zz^1); otherwise roll back and refuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
remove_unit() only warned about ids it had not installed when they were
in the map of their own kind, so removing "km" (read by prefix), a name
"m" (a database symbol) or a symbol "gal" (a database name) did nothing
silently. Decide by what udunits reads the id as instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Before #449, remove_unit() on a database id followed by install_unit() was
the only way to give such an id another meaning, and it is no longer
possible (#451). install_unit(..., override = TRUE) does it in one step
and reversibly:

- The database mappings that give an id another meaning are set aside:
  the id in both the symbol and name maps, and the unit's own symbol or
  name when it is that id. They are recorded under the installed id, and
  remove_unit() puts them back after undoing the install.
- Setting aside, mapping and the read-back check run as one transaction,
  rolled back on any failure.
- What each id meant before is reported with a message, by whatever route
  (e.g. "km" by prefix).
- Ids installed by install_unit() must be removed first, and other ids a
  new name would shadow are still refused; case variants of the name
  itself (e.g. "microN" for "micron") are not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.81%. Comparing base (3652916) to head (40dfac3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #452      +/-   ##
==========================================
+ Coverage   94.22%   94.81%   +0.59%     
==========================================
  Files          22       22              
  Lines        1315     1465     +150     
==========================================
+ Hits         1239     1389     +150     
  Misses         76       76              

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

A database mapping set aside by an override was filed under whichever
installed id set it aside first. When an id was overridden as both a
symbol and a name, removing the symbol first tried to put the database
name back while the new name still held it; the failure went unnoticed,
and the database name was lost once the new name was removed (#451).

Any installed id, of either kind, that would set a mapping aside needs it
out of the way: symbols as they are, names whatever their case. So when
an id is removed, a mapping it set aside is handed to such an id if one
is left, and put back only when none is. This works in any removal order,
for ids installed in one call or in separate ones, and keeps the other
kind's override working when only one kind is removed.

- A mapping that cannot be put back is reported instead of ignored.
- remove_unit() no longer warns "left unchanged" about an id that the
  same call put back, e.g. remove_unit("gal", "gal") after overriding
  only the symbol.
- The override message and the docs say that overriding a symbol sets
  aside the name spelled the same in any letter case.

Supersedes #453, whose test is included.

Co-Authored-By: Robin Shannon <32329438+vorpalvorpal@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Enchufa2
Enchufa2 merged commit 2129d28 into main Nov 18, 2026
30 checks passed
@Enchufa2
Enchufa2 deleted the fix-install-shadowing branch November 18, 2026 14:09
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.

1 participant