{"record":{"id":"511362193be11dfa","repo":"jax-ml/jax","slug":"new-enum-value-must-be-none-or-in-enum-values-g","errorCode":null,"errorMessage":"new enum value must be None or in {enum_values}, got {new_val} of type {type(new_val)}.","messagePattern":"new enum value must be None or in (.+?), got (.+?) of type (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"jax/_src/config.py","lineNumber":579,"sourceCode":"    help: string, used to populate the flag help information as well as the\n      docstring of the returned context manager.\n\n  Returns:\n    A contextmanager to control the thread-local state value.\n  \"\"\"\n  if default is not None and not isinstance(default, str):\n    raise TypeError(f\"Default value must be of type str or None, got {default} \"\n                    f\"of type {getattr(type(default), '__name__', type(default))}\")\n  name = name.lower()\n  default = os.getenv(name.upper(), default)\n  if default is not None and default not in enum_values:\n    raise ValueError(f\"Invalid value \\\"{default}\\\" for JAX flag {name}\")\n  config._contextmanager_flags.add(name)\n\n  def parser(new_val):\n    if (new_val is not None and\n      (type(new_val) is not str or new_val not in enum_values)):\n      raise ValueError(f\"new enum value must be None or in {enum_values}, \"\n                       f\"got {new_val} of type {type(new_val)}.\")\n    return new_val\n\n  s = State[str | None](\n      name, default, help, update_global_hook, update_thread_local_hook,\n      parser, include_in_jit_key=include_in_jit_key,\n      include_in_trace_context=include_in_trace_context,\n  )\n  config.add_option(\n      name, s, 'enum',\n      meta_args=[],\n      meta_kwargs={\"enum_values\": enum_values, \"help\": help}\n  )\n  setattr(Config, name, property(lambda _: s.value))\n  return s\n\n\ndef enum_class_state[EnumType: enum.Enum](","sourceCodeStart":561,"sourceCodeEnd":597,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/config.py#L561-L597","documentation":"The parser for optional_enum_state rejects new values that are neither None nor a str in enum_values. Raised when entering the context manager or calling config.update for such a flag.","triggerScenarios":"with my_flag(MyEnum.FOO): or jax.config.update('jax_my_flag', 'typo') where 'typo' is not an allowed value.","commonSituations":"Dynamically choosing a config value from user input or an enum object and passing it straight to an optional enum flag.","solutions":["Pass None or the exact string in enum_values (use MyEnum.FOO.value)","Validate/whitelist user-supplied values against the flag's enum_values before applying"],"exampleFix":"// before\nwith jax.my_flag(user_choice):  # user_choice = 'auto'\n    ...\n// after\nif user_choice not in ('foo', 'bar', None):\n    raise ValueError(f'bad choice: {user_choice}')\nwith jax.my_flag(user_choice):\n    ...","handlingStrategy":"validation","validationCode":"if new_val is not None and (type(new_val) is not str or new_val not in enum_values):\n    raise ValueError(f'expected None or one of {enum_values}, got {new_val!r}')","typeGuard":"def is_optional_enum_value(v, values) -> bool:\n    return v is None or (type(v) is str and v in values)","tryCatchPattern":"try:\n    with my_flag(v):\n        ...\nexcept ValueError:\n    with my_flag(None):\n        ...","preventionTips":["Centralize flag-setting behind a helper that validates against enum_values","Never pass raw user input directly to config context managers"],"tags":["jax","config","valueerror","enum"],"backgroundTag":"invalid-config-value","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}