python/cpython · error · TypeError

Initialization arguments are not supported

Error message

Initialization arguments are not supported

What it means

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.

Source

Thrown at Lib/_threading_local.py:87

def _patch(self):
    impl = object.__getattribute__(self, '_local__impl')
    try:
        dct = impl.get_dict()
    except KeyError:
        dct = impl.create_dict()
        args, kw = impl.localargs
        self.__init__(*args, **kw)
    with impl.locallock:
        object.__setattr__(self, '__dict__', dct)
        yield


class local:
    __slots__ = '_local__impl', '__dict__'

    def __new__(cls, /, *args, **kw):
        if (args or kw) and (cls.__init__ is object.__init__):
            raise TypeError("Initialization arguments are not supported")
        self = object.__new__(cls)
        impl = _localimpl()
        impl.localargs = (args, kw)
        impl.locallock = RLock()
        object.__setattr__(self, '_local__impl', impl)
        # We need to create the thread dict in anticipation of
        # __init__ being called, to make sure we don't call it
        # again ourselves.
        impl.create_dict()
        return self

    def __getattribute__(self, name):
        with _patch(self):
            return object.__getattribute__(self, name)

    def __setattr__(self, name, value):
        if name == '__dict__':
            raise AttributeError(

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Define __init__ in the subclass that accepts the arguments: class MyLocal(threading.local): def __init__(self, x): self.x = x
  2. Or set attributes after construction: loc = MyLocal(); loc.x = 1

Example fix

# before
class Ctx(threading.local):
    pass
Ctx(user='bob')  # TypeError: Initialization arguments are not supported

# after
class Ctx(threading.local):
    def __init__(self, user):
        self.user = user
Ctx(user='bob')
Defensive patterns

Strategy: validation

Validate before calling

import threading

def make_local(cls, /, *args, **kw):
    if args or kw and cls.__init__ is object.__init__:
        raise TypeError(f'{cls.__name__} takes no constructor arguments; define __init__')
    return cls(*args, **kw)

Prevention

When it happens

Trigger: 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).

Common situations: Mistaking threading.local for a dict-like holder that accepts initial values; subclassing local and expecting constructor kwargs to be stored per-thread.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/c6cac71811804a4e. Report an issue: GitHub.