Skip to content

Commit 6f60df7

Browse files
committed
fix: fix up some code, handle new errors etc
1 parent 498b1b7 commit 6f60df7

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

python/pydantic_core/_pydantic_core.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class MultiHostUrl(SupportsAllComparisons):
225225
user: Optional[str] = None,
226226
password: Optional[str] = None,
227227
host: str,
228+
hosts: Optional[dict] = None,
228229
port: Optional[str] = None,
229230
path: Optional[str] = None,
230231
query: Optional[str] = None,

src/url.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,25 +373,30 @@ impl PyMultiHostUrl {
373373
password: Option<&str>,
374374
port: Option<&str>,
375375
) -> PyResult<&'a PyAny> {
376-
let mut url = if hosts.is_some() {
376+
let mut url = if hosts.is_some() && host.is_some() {
377+
return Err(PyValueError::new_err("Only one of `host` or `hosts` may be set"));
378+
}
379+
else if hosts.is_some() {
377380
// check all of host / user / password / port empty
378381
// build multi-host url
379382
todo!()
380-
} else if let Some(host) = host {
383+
}
384+
else if let Some(host) = host {
381385
let user_password = match (user, password) {
382386
(Some(user), None) => format!("{user}@"),
383387
(None, Some(password)) => format!(":{password}@"),
384388
(Some(user), Some(password)) => format!("{user}:{password}@"),
385389
(None, None) => String::new(),
386390
};
387-
format!("{scheme}://{user_password}{host}")
391+
let mut single_host_url = format!("{scheme}://{user_password}{host}");
392+
if let Some(port) = port {
393+
single_host_url.push(':');
394+
single_host_url.push_str(port);
395+
};
396+
single_host_url
388397
} else {
389398
return Err(PyValueError::new_err("expected either `host` or `hosts` to be set"));
390399
};
391-
if let Some(port) = port {
392-
url.push(':');
393-
url.push_str(port);
394-
}
395400
if let Some(path) = path {
396401
url.push('/');
397402
url.push_str(path);

tests/validators/test_url.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,46 @@ def test_multi_url_build() -> None:
12331233
assert str(url) == 'postgresql://testuser:[email protected]:5432/database?sslmode=require#test'
12341234

12351235

1236+
def test_multi_url_build_host_and_hosts_set() -> None:
1237+
hosts = [
1238+
{
1239+
'host': '127.0.0.1:5432',
1240+
'password': 'testpassword',
1241+
'username': 'testuser',
1242+
'port': '5432'
1243+
},
1244+
{
1245+
'host': '127.0.0.1:5432',
1246+
'password': 'testpassword',
1247+
'username': 'testuser',
1248+
'port': '5432'
1249+
},
1250+
]
1251+
with pytest.raises(ValueError):
1252+
MultiHostUrl.build(
1253+
scheme='postgresql',
1254+
user='testuser',
1255+
password='testpassword',
1256+
host='127.0.0.1',
1257+
hosts=hosts,
1258+
port='5432',
1259+
path='database',
1260+
query='sslmode=require',
1261+
fragment='test',
1262+
)
1263+
1264+
def test_multi_url_build_neither_host_and_hosts_set() -> None:
1265+
with pytest.raises(ValueError):
1266+
MultiHostUrl.build(
1267+
scheme='postgresql',
1268+
user='testuser',
1269+
password='testpassword',
1270+
port='5432',
1271+
path='database',
1272+
query='sslmode=require',
1273+
fragment='test',
1274+
)
1275+
12361276
def test_url_build() -> None:
12371277
url = Url.build(
12381278
scheme='postgresql',

0 commit comments

Comments
 (0)