Add us.states.enumeration() returning a dynamic Enum#99
Merged
Conversation
Adds a sibling to `mapping()` that builds an `Enum` keyed by `State.abbr` with member values pulled from a caller-chosen `State` attribute. Default state list matches `mapping()` (`STATES_AND_TERRITORIES`); `value_field` defaults to `name`. Closes APR-32. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Member
Author
|
Review notes:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new
us.states.enumeration()helper that dynamically constructs anEnumof states keyed by state abbreviation. It mirrors the call shape ofus.states.mapping(): an optionalstatesiterable selects whichStateobjects to include (defaulting toSTATES_AND_TERRITORIES), and avalue_fieldargument picks whichStateattribute supplies each member's value.The enum member name is always the state's
abbr—abbris guaranteed to be a valid Python identifier, so users can't accidentally produce invalid member names by picking avalue_fieldlike"name"("New York") or"fips"("01").Closes #28
Example
Approach
enumeration()lives next tomapping()inus/states.pyand delegates to the stdlib functional API:Enum("States", {s.abbr: getattr(s, value_field) for s in states}). No changes tomapping()or toState.Test plan
uv run pytest us/tests/test_us.pypassesvalue_field("name") returns expected member valuesstates=iterable produces an enum of exactly that subsetvalue_field("fips") values pass through unchanged