argparse behavior change with string choices in Python 3.12+

XMLWordPrintableJSON

    • Type: Sub-task
    • Resolution: Fixed
    • Priority: Major - P3
    • 8.3.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • None
    • DevProd Correctness
    • Fully Compatible
    • Correctness 2026-01-26, Correctness 2026-02-09
    • None
    • None
    • None
    • None
    • None
    • None
    • None

      Error:

      argument --fuzzMongodConfigs: invalid choice: 'normal' (choose from n, o, r, m, a, l)
      

      Description:

      Python 3.12+ changed how argparse validates the choices parameter, exposing a latent bug in our code.

      The Bug in Our Code:
      We have choices=("normal") in our argparse argument definition. In Python, ("normal") without a trailing comma is NOT a tuple—it's just the string "normal" with parentheses. To create a single-element tuple, you need ("normal",) with a trailing comma.

      Why It Worked in Python 3.11:

      # Python 3.11 argparse _check_value() method:
      if action.choices is not None and value not in action.choices:
          raise error
      
      # With choices="normal" (a string), this becomes:
      if "normal" not in "normal":  # False, because substring match
          raise error  # Doesn't raise - validation passes
      

      The in operator with strings checks for substrings, so "normal" in "normal" is True, making the validation pass accidentally.

      Why It Fails in Python 3.12+:

      # Python 3.12+ argparse _check_value() method:
      if isinstance(choices, str):
          choices = iter(choices)  # Convert string to character iterator
      if value not in choices:
          raise error
      
      # With choices="normal" (a string), this becomes:
      choices = iter("normal")  # ['n', 'o', 'r', 'm', 'a', 'l']
      if "normal" not in ['n', 'o', 'r', 'm', 'a', 'l']:  # True!
          raise error  # Raises error - validation fails
      

      Python 3.12+ now explicitly converts string choices into an iterator over individual characters. The full string "normal" is not in the list of individual characters ['n', 'o', 'r', 'm', 'a', 'l'], so validation fails with the error message showing each character as a separate choice.

      References:

            Assignee:
            Nicholas Jefferies
            Reporter:
            Nicholas Jefferies
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: