{"record":{"id":"d3ac7a9ba51ca689","repo":"RustPython/RustPython","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/RustPython/RustPython/blob/aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd/Lib/_threading_local.py#L69-L105","documentation":"Raised by threading.local.__new__ when you instantiate threading.local (or a subclass) with positional or keyword arguments while the class still uses object.__init__. Thread-local setup happens entirely in __new__, so there is no __init__ to receive the arguments; CPython rejects them to match object's own behavior for argumentless constructors.","triggerScenarios":"`class Ctx(threading.local): pass` followed by `Ctx('a')` or `Ctx(user='alice')`. Also a subclass that overrides __new__ but not __init__ and is then called with arguments.","commonSituations":"Developers porting a config/context class to threading.local and passing initial state in the constructor; metaclass or decorator code that forwards *args/**kw to every class it creates.","solutions":["Define an __init__ on the local subclass that accepts the arguments (it runs per-thread on first attribute access)","Instantiate with no arguments and set attributes afterwards: ctx = Ctx(); ctx.user = 'alice'","If a base local class must stay argument-less, funnel construction through a factory function that sets defaults"],"exampleFix":"// before\nclass Ctx(threading.local):\n    pass\nctx = Ctx(user='alice')  # TypeError\n\n// after\nclass Ctx(threading.local):\n    def __init__(self, user=None):\n        self.user = user\nctx = Ctx(user='alice')","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 arguments; define __init__ to accept them')\n    return cls(*args, **kw)","typeGuard":"def accepts_init_args(cls) -> bool:\n    return cls.__init__ is not object.__init__","tryCatchPattern":"try:\n    ctx = Ctx(user='alice')\nexcept TypeError as e:\n    if 'Initialization arguments' in str(e):\n        ctx = Ctx(); ctx.user = 'alice'\n    else:\n        raise","preventionTips":["Always define __init__ in threading.local subclasses you intend to construct with arguments","Remember attributes set in a local subclass __init__ are re-initialized per thread","Never blindly forward *args/**kw into arbitrary classes in factories; check the constructor first"],"tags":["threading","thread-local","constructor","typeerror"],"backgroundTag":"invalid-constructor-arguments","analyzedSha":"aaeab4f754b4f40efc0c8ab39cf7c4a3c35a8cfd","analyzedAt":"2026-08-17T00:37:52.100Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}