{"record":{"id":"c6cac71811804a4e","repo":"python/cpython","slug":"initialization-arguments-are-not-supported","errorCode":null,"errorMessage":"Initialization arguments are not supported","messagePattern":"Initialization arguments are not supported","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"Lib/_threading_local.py","lineNumber":87,"sourceCode":"def _patch(self):\n    impl = object.__getattribute__(self, '_local__impl')\n    try:\n        dct = impl.get_dict()\n    except KeyError:\n        dct = impl.create_dict()\n        args, kw = impl.localargs\n        self.__init__(*args, **kw)\n    with impl.locallock:\n        object.__setattr__(self, '__dict__', dct)\n        yield\n\n\nclass local:\n    __slots__ = '_local__impl', '__dict__'\n\n    def __new__(cls, /, *args, **kw):\n        if (args or kw) and (cls.__init__ is object.__init__):\n            raise TypeError(\"Initialization arguments are not supported\")\n        self = object.__new__(cls)\n        impl = _localimpl()\n        impl.localargs = (args, kw)\n        impl.locallock = RLock()\n        object.__setattr__(self, '_local__impl', impl)\n        # We need to create the thread dict in anticipation of\n        # __init__ being called, to make sure we don't call it\n        # again ourselves.\n        impl.create_dict()\n        return self\n\n    def __getattribute__(self, name):\n        with _patch(self):\n            return object.__getattribute__(self, name)\n\n    def __setattr__(self, name, value):\n        if name == '__dict__':\n            raise AttributeError(","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_threading_local.py#L69-L105","documentation":"threading.local's pure-Python fallback local.__new__ rejects constructor arguments when the subclass does not define its own __init__ (cls.__init__ is object.__init__). Since object.__init__ would silently ignore the args, the class instead fails fast with this TypeError to avoid silently dropping them.","triggerScenarios":"class MyLocal(threading.local): pass; MyLocal(42) or MyLocal(x=1). Also plain threading.local(x=1) — note _threading_local.local specifically (the _thread._local C version raises 'any exceptions from __init__ are propagated' style errors differently).","commonSituations":"Mistaking threading.local for a dict-like holder that accepts initial values; subclassing local and expecting constructor kwargs to be stored per-thread.","solutions":["Define __init__ in the subclass that accepts the arguments: class MyLocal(threading.local): def __init__(self, x): self.x = x","Or set attributes after construction: loc = MyLocal(); loc.x = 1"],"exampleFix":"# before\nclass Ctx(threading.local):\n    pass\nCtx(user='bob')  # TypeError: Initialization arguments are not supported\n\n# after\nclass Ctx(threading.local):\n    def __init__(self, user):\n        self.user = user\nCtx(user='bob')","handlingStrategy":"validation","validationCode":"import threading\n\ndef make_local(cls, /, *args, **kw):\n    if args or kw and cls.__init__ is object.__init__:\n        raise TypeError(f'{cls.__name__} takes no constructor arguments; define __init__')\n    return cls(*args, **kw)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Define __init__ in every threading.local subclass that needs parameters","Remember thread-locals hold per-thread state set on first access, not constructor state","Cover construction in the subclass's unit test"],"tags":["threading","thread-local","typeerror","cpython"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}