feat: add case-insensitive choice matching - #44
Merged
Conversation
Add an ``ignore_case`` flag to ``option()`` and ``argument()``. When set and the value is constrained by ``Literal[...]`` choices, the supplied value is matched case-insensitively and normalised to the spelling declared in the ``Literal`` (e.g. ``--colors on`` yields ``"ON"`` for ``Literal["AUTO", "ON", "OFF"]``). Unknown values are still rejected, and a case-insensitive match is only honoured when unambiguous. The default behaviour remains case-sensitive, so existing options are unaffected. This lets tools that accept case-insensitive enum-style values (e.g. Robot Framework's console colour/marker settings) use ``Literal`` choices — gaining shell-completion of the allowed values and early validation — without rejecting the lowercase spellings users commonly type. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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 an
ignore_caseflag tooption()andargument(). When set and the value is constrained byLiteral[...]choices, the value is matched case-insensitively and normalised to the spelling declared in theLiteral.invalid value 'purple'; choose from ...).list[Literal[...]], methodvalueparameters, positionalargument()s, and values coming from TOML/env — the check lives incoerce_value.Motivation
Tools often accept case-insensitive enum-style values whose canonical form is a fixed spelling (e.g. Robot Framework's
--consolecolors auto|on|off|ansi, stored canonically as uppercase). Until now, usingLiteralfor those would reject the lowercase spellings users type, so such options had to fall back to plainstrand lose validation. Withignore_case=Truethey can useLiteraland gain shell completion of the allowed choices and early validation, while still accepting any casing and handing downstream code the canonical spelling.Tests
tests/test_choices.py: scalar normalisation, unknown-value rejection, list choices, arguments, TOML source, and a guard that the default stays case-sensitive.mypy --strictclean; ruff lint + format clean.Docs
ignore_caseexample.[Unreleased]Features entry.