Skip to content

Commit 6ac5c3e

Browse files
committed
fix: remove double defined constructor for node class
- Remove double-defined constructor in Node class, leading to errors when initializing with a string - Add `from_url` class method for creating Node instances from URLs - Enhance type hints for Node constructor parameters - Update Configuration class to use new Node.from_url method
1 parent e88f756 commit 6ac5c3e

1 file changed

Lines changed: 23 additions & 13 deletions

File tree

src/typesense/configuration.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1+
from typing import Literal
12
from .exceptions import ConfigError
23
from .logger import logger
34
from urllib.parse import urlparse
45

5-
class Node(object):
6-
def __init__(self, url):
7-
parsed = urlparse(url);
8-
if not parsed.hostname:
9-
raise ConfigError('Node URL does not contain the host name.')
10-
if not parsed.port:
11-
raise ConfigError('Node URL does not contain the port.')
12-
if not parsed.scheme:
13-
raise ConfigError('Node URL does not contain the protocol.')
14-
self.__init__(parsed.hostname, parsed.port, parsed.path, parsed.scheme)
156

16-
def __init__(self, host, port, path, protocol):
7+
class Node(object):
8+
def __init__(
9+
self,
10+
host: str,
11+
port: str | int,
12+
path: str,
13+
protocol: Literal['http', 'https'] | str,
14+
):
1715
self.host = host
1816
self.port = port
1917
self.path = path
@@ -22,6 +20,18 @@ def __init__(self, host, port, path, protocol):
2220
# Used to skip bad hosts
2321
self.healthy = True
2422

23+
@classmethod
24+
def from_url(cls, url: str) -> 'Node':
25+
parsed = urlparse(url)
26+
if not parsed.hostname:
27+
raise ConfigError('Node URL does not contain the host name.')
28+
if not parsed.port:
29+
raise ConfigError('Node URL does not contain the port.')
30+
if not parsed.scheme:
31+
raise ConfigError('Node URL does not contain the protocol.')
32+
33+
return cls(parsed.hostname, parsed.port, parsed.path, parsed.scheme)
34+
2535
def url(self):
2636
return '{0}://{1}:{2}{3}'.format(self.protocol, self.host, self.port, self.path)
2737

@@ -34,7 +44,7 @@ def __init__(self, config_dict):
3444
self.nodes = []
3545
for node_config in config_dict.get('nodes', []):
3646
if isinstance(node_config, str):
37-
node = Node(node_config)
47+
node = Node.from_url(node_config)
3848
else:
3949
node = Node(node_config['host'], node_config['port'], node_config.get('path', ''), node_config['protocol'])
4050
self.nodes.append(node)
@@ -43,7 +53,7 @@ def __init__(self, config_dict):
4353
if not nearest_node:
4454
self.nearest_node = None
4555
elif isinstance(nearest_node, str):
46-
self.nearest_node = Node(nearest_node)
56+
self.nearest_node = Node.from_url(nearest_node)
4757
else:
4858
self.nearest_node = Node(nearest_node['host'], nearest_node['port'], nearest_node.get('path', ''), nearest_node['protocol'])
4959

0 commit comments

Comments
 (0)