Skip to content

fix: give alternate unit suffixes their own keys - #1314

Open
Sanjays2402 wants to merge 3 commits into
google:masterfrom
Sanjays2402:fix/duplicate-unit-suffix-keys
Open

fix: give alternate unit suffixes their own keys#1314
Sanjays2402 wants to merge 3 commits into
google:masterfrom
Sanjays2402:fix/duplicate-unit-suffix-keys

Conversation

@Sanjays2402

Copy link
Copy Markdown

Fixes #1250

When a UNECE symbol column lists aliases (% or pct), the generator emitted both descriptors under the same Python key, so the second overwrote the first — units.PERCENT resolved to pct and the % descriptor was unreachable as a module attribute. KILOGRAM_PER_LITRE, DECITONNE and RACK_UNIT had the same collision.

units_from_xls.py now keeps the canonical key for the first suffix and derives a distinct key for each alternate (PERCENT_PCT, etc.); units.py is regenerated to match. Lookup via units.Unit('%') / units.Unit('pct') is unchanged. New test/util/units_test.py fails on master and passes with the fix.

Units whose UNECE symbol column lists aliases (e.g. "% or pct") were
emitted twice under the SAME Python name, so the second assignment
overwrote the first. units.PERCENT therefore resolved to the "pct"
descriptor and the "%" one was unreachable as a module attribute.
The same collision affected KILOGRAM_PER_LITRE, DECITONNE and RACK_UNIT.

units_from_xls.py now keeps the canonical key for the first suffix and
derives a distinct key for each alternate suffix; units.py is updated to
match. Adds test/util/units_test.py covering the primary suffix, the
distinct alias keys, and lookup by every suffix.

Fixes google#1250
@google-cla

google-cla Bot commented Jul 25, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@Sanjays2402

Copy link
Copy Markdown
Author

@googlebot I signed it!

1 similar comment
@Sanjays2402

Copy link
Copy Markdown
Author

@googlebot I signed it!

@Sanjays2402

Copy link
Copy Markdown
Author

I've signed the Google Individual Contributor License Agreement as an individual contributor, and the cla/google check is now passing.

This should be ready for review whenever a maintainer has a moment. Happy to rebase or adjust anything if needed. Thanks!

@glados-verma

Copy link
Copy Markdown
Collaborator

There are a few problems with this approach, including:

  1. The alias names have "jitter" e.g. KILOGRAM_PER_LITRE_KG_PER_L
  2. The approach doesn't care for which one should be considered the "primary" alias

Given the small number of units that have aliases, we might as well create a lookup such units.

Only four UNECE rows list more than one symbol. Deriving a module-level
name for each alternate produced awkward keys like
KILOGRAM_PER_LITRE_KG_PER_L. Keep the canonical key for the primary
symbol and add alternates to ALL_UNITS anonymously so they remain
reachable via units.Unit('<symbol>'). An explicit ALIAS_UNIT_KEYS table
names the one alias worth exposing, PERCENT_PCT.
@Sanjays2402

Copy link
Copy Markdown
Author

Both fair, and the jitter one was the bigger problem — KILOGRAM_PER_LITRE_KG_PER_L is not a name anyone would type.

Reworked in b81fdd5 along the lines you suggested. The generator now keeps the canonical key for the first symbol listed in the sheet (that's the primary one) and appends alternates to ALL_UNITS anonymously, so they stay reachable via units.Unit('kg/L') / Unit('dtn') / Unit('RU') but get no module-level name. A small explicit ALIAS_UNIT_KEYS table names the one alias I think is genuinely worth exposing, PERCENT_PCT, since pct is common in test configs — it's a two-line dict so adding another later is trivial.

Net effect on the generated module: PERCENT is % again, KILOGRAM_PER_LITRE is kg/l, DECITONNE is dt, RACK_UNIT is U, and none of the four are overwritten by their alias. Diff to units.py is now 4 hunks instead of the previous key-renaming spread.

Tests updated to match (primary keeps its symbol / alternates resolve by lookup only). 4 pass here; they fail 3/4 against current master.

Happy to drop PERCENT_PCT too and make the rule purely "only the primary symbol gets a name" if you'd rather have zero exceptions.

@glados-verma
glados-verma self-requested a review August 5, 2026 22:15
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.

PERCENT double declaration

2 participants