{"record":{"id":"627a83637c509908","repo":"redis/redis-py","slug":"passing-a-host-requires-passing-a-port-and-vice-v","errorCode":null,"errorMessage":"Passing a host requires passing a port, and vice versa","messagePattern":"Passing a host requires passing a port, and vice versa","errorType":"exception","errorClass":"DataError","httpStatus":null,"severity":"error","filePath":"redis/cluster.py","lineNumber":3018,"sourceCode":"        If host is passed without port, or vice versa, a DataError will be\n        thrown.\n        :type cluster: RedisCluster\n        :type node: ClusterNode\n        :type host: str\n        :type port: int\n        \"\"\"\n        if node is not None:\n            # node is passed by the user\n            self._raise_on_invalid_node(cluster, node, node.host, node.port)\n            pubsub_node = node\n        elif host is not None and port is not None:\n            # host and port passed by the user\n            node = cluster.get_node(host=host, port=port)\n            self._raise_on_invalid_node(cluster, node, host, port)\n            pubsub_node = node\n        elif any([host, port]) is True:\n            # only 'host' or 'port' passed\n            raise DataError(\"Passing a host requires passing a port, and vice versa\")\n        else:\n            # nothing passed by the user. set node to None\n            pubsub_node = None\n\n        self.node = pubsub_node\n\n    def get_pubsub_node(self):\n        \"\"\"\n        Get the node that is being used as the pubsub connection\n        \"\"\"\n        return self.node\n\n    def _raise_on_invalid_node(self, redis_cluster, node, host, port):\n        \"\"\"\n        Raise a RedisClusterException if the node is None or doesn't exist in\n        the cluster.\n        \"\"\"\n        if node is None or redis_cluster.get_node(node_name=node.name) is None:","sourceCodeStart":3000,"sourceCodeEnd":3036,"githubUrl":"https://github.com/redis/redis-py/blob/6a6b581b48225afa0b76912d1028c6035baee932/redis/cluster.py#L3000-L3036","documentation":"Raised as a DataError in the ClusterPubSub constructor when only one of host or port is provided (the other is None). The guard at cluster.py:3016-3018 uses any([host, port]) to detect the partial case. Both must be provided together or neither, because a node address requires both components to be meaningful.","triggerScenarios":"Calling client.pubsub(host='10.0.0.1') without port, or client.pubsub(port=7000) without host. Either triggers the DataError.","commonSituations":"Developer passes a host string but forgets the port, or extracts host and port from a config object where one value is missing.","solutions":["Provide both host and port together: client.pubsub(host='10.0.0.1', port=7000).","If you want the default node, pass neither: client.pubsub().","If you have a ClusterNode, pass node= instead: client.pubsub(node=my_node)."],"exampleFix":"// before\nps = client.pubsub(host='10.0.0.1')\n\n// after\nps = client.pubsub(host='10.0.0.1', port=7000)","handlingStrategy":"validation","validationCode":"# Validate host/port are both provided or both absent\nif bool(host) != bool(port):\n    raise ValueError('Provide both host and port, or neither')","typeGuard":"def host_port_pair_valid(host, port) -> bool:\n    return (host is not None) == (port is not None)","tryCatchPattern":"null","preventionTips":["Always provide host and port together, or omit both to use the default node.","Extract host and port from the same config object to avoid partial values."],"tags":["pubsub","arguments","validation","cluster"],"backgroundTag":null,"analyzedSha":"6a6b581b48225afa0b76912d1028c6035baee932","analyzedAt":"2026-08-10T12:52:44.840Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}