{"record":{"id":"425bcd06e9a03021","repo":"python/cpython","slug":"socket-modifier-keyword-arguments-can-not-be-used","errorCode":null,"errorMessage":"socket modifier keyword arguments can not be used when sock is specified. ({problems})","messagePattern":"socket modifier keyword arguments can not be used when sock is specified\\. \\((.+?)\\)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/asyncio/base_events.py","lineNumber":1407,"sourceCode":"                                       local_addr=None, remote_addr=None, *,\n                                       family=0, proto=0, flags=0,\n                                       reuse_port=None,\n                                       allow_broadcast=None, sock=None):\n        \"\"\"Create datagram connection.\"\"\"\n        if sock is not None:\n            if sock.type == socket.SOCK_STREAM:\n                raise ValueError(\n                    f'A datagram socket was expected, got {sock!r}')\n            if (local_addr or remote_addr or\n                    family or proto or flags or\n                    reuse_port or allow_broadcast):\n                # show the problematic kwargs in exception msg\n                opts = dict(local_addr=local_addr, remote_addr=remote_addr,\n                            family=family, proto=proto, flags=flags,\n                            reuse_port=reuse_port,\n                            allow_broadcast=allow_broadcast)\n                problems = ', '.join(f'{k}={v}' for k, v in opts.items() if v)\n                raise ValueError(\n                    f'socket modifier keyword arguments can not be used '\n                    f'when sock is specified. ({problems})')\n            sock.setblocking(False)\n            r_addr = None\n        else:\n            if not (local_addr or remote_addr):\n                if family == 0:\n                    raise ValueError('unexpected address family')\n                addr_pairs_info = (((family, proto), (None, None)),)\n            elif hasattr(socket, 'AF_UNIX') and family == socket.AF_UNIX:\n                for addr in (local_addr, remote_addr):\n                    if addr is not None and not isinstance(addr, str):\n                        raise TypeError('string is expected')\n\n                if local_addr and local_addr[0] not in (0, '\\x00'):\n                    try:\n                        if stat.S_ISSOCK(os.stat(local_addr).st_mode):\n                            os.remove(local_addr)","sourceCodeStart":1389,"sourceCodeEnd":1425,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/asyncio/base_events.py#L1389-L1425","documentation":"Raised by create_datagram_endpoint() when a pre-created socket (sock=) is supplied together with address-modifying keyword arguments such as local_addr, remote_addr, family, proto, flags, reuse_port, or allow_broadcast. When you supply the socket you fully control its configuration, so the loop refuses to double-configure it; the message lists the offending kwargs.","triggerScenarios":"loop.create_datagram_endpoint(factory, sock=sock, local_addr=('0.0.0.0', 9999)) or any combination of sock= with a truthy value among local_addr/remote_addr/family/proto/flags/reuse_port/allow_broadcast.","commonSituations":"Migrating code from address-based to socket-based creation and leaving the old local_addr/remote_addr kwargs in place; templates that set family=socket.AF_INET 'for safety' on all endpoints.","solutions":["Remove the address/modifier kwargs and configure the socket yourself (bind it, set options) before passing sock=","Or drop sock= and let the loop create the socket from local_addr/remote_addr/family/etc."],"exampleFix":"# before\nsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\nsock.bind(('0.0.0.0', 9999))\nawait loop.create_datagram_endpoint(factory, sock=sock, local_addr=('0.0.0.0', 9999))\n\n# after\nsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)\nsock.bind(('0.0.0.0', 9999))\nawait loop.create_datagram_endpoint(factory, sock=sock)","handlingStrategy":"validation","validationCode":"DATAGRAM_MODIFIERS = ('local_addr', 'remote_addr', 'family', 'proto', 'flags', 'reuse_port', 'allow_broadcast')\nif sock is not None:\n    conflicts = [k for k in DATAGRAM_MODIFIERS if locals().get(k)]\n    assert not conflicts, f'remove {conflicts} when passing sock=',","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Decide one mode per call: either sock= or address kwargs, never both","Keep socket setup (bind, options) next to the sock creation so address kwargs feel redundant and get removed"],"tags":["asyncio","udp","datagram","api-misuse"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}