{"record":{"id":"60848de0384c2d6d","repo":"python/cpython","slug":"key-is-an-invalid-keyword-argument-for-this-fu","errorCode":null,"errorMessage":"'{key}' is an invalid keyword argument for this function","messagePattern":"'(.+?)' is an invalid keyword argument for this function","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_pydecimal.py","lineNumber":422,"sourceCode":"    >>> with localcontext():\n    ...     ctx = getcontext()\n    ...     ctx.prec += 2\n    ...     print(ctx.prec)\n    ...\n    30\n    >>> with localcontext(ExtendedContext):\n    ...     print(getcontext().prec)\n    ...\n    9\n    >>> print(getcontext().prec)\n    28\n    \"\"\"\n    if ctx is None:\n        ctx = getcontext()\n    ctx_manager = _ContextManager(ctx)\n    for key, value in kwargs.items():\n        if key not in _context_attributes:\n            raise TypeError(f\"'{key}' is an invalid keyword argument for this function\")\n        setattr(ctx_manager.new_context, key, value)\n    return ctx_manager\n\n\ndef IEEEContext(bits, /):\n    \"\"\"\n    Return a context object initialized to the proper values for one of the\n    IEEE interchange formats.  The argument must be a multiple of 32 and less\n    than IEEE_CONTEXT_MAX_BITS.\n    \"\"\"\n    if bits <= 0 or bits > IEEE_CONTEXT_MAX_BITS or bits % 32:\n        raise ValueError(\"argument must be a multiple of 32, \"\n                         f\"with a maximum of {IEEE_CONTEXT_MAX_BITS}\")\n\n    ctx = Context()\n    ctx.prec = 9 * (bits//32) - 2\n    ctx.Emax = 3 * (1 << (bits//16 + 3))\n    ctx.Emin = 1 - ctx.Emax","sourceCodeStart":404,"sourceCodeEnd":440,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_pydecimal.py#L404-L440","documentation":"decimal.localcontext(**kwargs) applies keyword overrides to the managed context, but only for keys present in _context_attributes (prec, rounding, Emin, Emax, capitals, clamp, flags, traps, etc.). Any other keyword raises TypeError naming the invalid key, matching the context constructor's behavior.","triggerScenarios":"localcontext(precision=28) (correct name is prec); localcontext(round='DOWN') (correct is rounding); typos like localcontext(precs=10); passing context constructor args that are not context attributes (e.g. context-specific Emax spelling mistakes).","commonSituations":"Migrating from the C decimal or other languages where 'precision' is the word; copy-pasting between Context(prec=...) and localcontext(precision=...); version drift when new attributes were expected but the Python build is older.","solutions":["Use the canonical names: prec (not precision), rounding (not round), Emin/Emax, capitals, clamp, traps, flags","Check decimal._context_attributes / help(Context) for the accepted set on your version","If passing user config, whitelist and map keys to valid names before calling"],"exampleFix":"// before\nwith localcontext(precision=10):\n    ...\n\n# after\nwith localcontext(prec=10):\n    ...","handlingStrategy":"validation","validationCode":"from decimal import _context_attributes\n\nALLOWED = set(_context_attributes)  # prec, rounding, Emin, Emax, ...\n\ndef clean_ctx_kwargs(kwargs: dict) -> dict:\n    alias = {'precision': 'prec', 'round': 'rounding'}\n    out = {}\n    for k, v in kwargs.items():\n        k = alias.get(k, k)\n        if k not in ALLOWED:\n            raise ValueError(f'unknown context attribute: {k}')\n        out[k] = v\n    return out","typeGuard":"from decimal import _context_attributes\n\ndef is_valid_ctx_kwarg(key: str) -> bool:\n    return key in _context_attributes","tryCatchPattern":null,"preventionTips":["Use prec, not precision; rounding, not round","Validate user-supplied context keys against decimal._context_attributes","Check help(decimal.Context) for the attribute list on your version"],"tags":["python","decimal","type-error","configuration"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}