Skip to content

Commit 0ff7563

Browse files
authored
make Url and MultiHostUrl pickleable (pydantic#676)
1 parent 4420be3 commit 0ff7563

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/url.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ impl PyUrl {
145145
pub fn __deepcopy__(&self, py: Python, _memo: &PyDict) -> Py<PyAny> {
146146
self.clone().into_py(py)
147147
}
148+
149+
fn __getnewargs__(&self) -> (&str,) {
150+
(self.__str__(),)
151+
}
148152
}
149153

150154
#[pyclass(name = "MultiHostUrl", module = "pydantic_core._pydantic_core", subclass)]
@@ -305,6 +309,10 @@ impl PyMultiHostUrl {
305309
pub fn __deepcopy__(&self, py: Python, _memo: &PyDict) -> Py<PyAny> {
306310
self.clone().into_py(py)
307311
}
312+
313+
fn __getnewargs__(&self) -> (String,) {
314+
(self.__str__(),)
315+
}
308316
}
309317

310318
fn host_to_dict<'a>(py: Python<'a>, lib_url: &Url) -> PyResult<&'a PyDict> {

tests/serializers/test_url.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pickle
2+
13
import pytest
24

35
from pydantic_core import MultiHostUrl, SchemaSerializer, SchemaValidator, Url, core_schema
@@ -96,3 +98,10 @@ def some_method(self):
9698

9799
m = MyUrl('http://ex.com/path')
98100
assert m.some_method() == '/path-success'
101+
102+
103+
@pytest.mark.parametrize('value', (Url('https://example.com'), MultiHostUrl('https://example.com,example.org/path')))
104+
def test_url_pickle(value):
105+
pickled = pickle.dumps(value)
106+
unpickled = pickle.loads(pickled)
107+
assert value == unpickled

0 commit comments

Comments
 (0)