Skip to content

Commit

Permalink
Make Url and MultiHostUrl subclassable (pydantic#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu committed Jun 12, 2023
1 parent d488194 commit 55de89a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/url.rs
Expand Up @@ -12,7 +12,7 @@ use crate::SchemaValidator;

static SCHEMA_DEFINITION_URL: GILOnceCell<SchemaValidator> = GILOnceCell::new();

#[pyclass(name = "Url", module = "pydantic_core._pydantic_core")]
#[pyclass(name = "Url", module = "pydantic_core._pydantic_core", subclass)]
#[derive(Clone)]
#[cfg_attr(debug_assertions, derive(Debug))]
pub struct PyUrl {
Expand Down Expand Up @@ -147,7 +147,7 @@ impl PyUrl {
}
}

#[pyclass(name = "MultiHostUrl", module = "pydantic_core._pydantic_core")]
#[pyclass(name = "MultiHostUrl", module = "pydantic_core._pydantic_core", subclass)]
#[derive(Clone)]
#[cfg_attr(debug_assertions, derive(Debug))]
pub struct PyMultiHostUrl {
Expand Down
10 changes: 10 additions & 0 deletions tests/serializers/test_url.py
Expand Up @@ -86,3 +86,13 @@ def test_custom_serializer():

multi_host_url = MultiHostUrl('https://ex.com,ex.org/path')
assert s.to_python(multi_host_url) == multi_host_url


@pytest.mark.parametrize('base_class', [Url, MultiHostUrl])
def test_url_subclass(base_class):
class MyUrl(base_class):
def some_method(self):
return self.path + '-success'

m = MyUrl('http://ex.com/path')
assert m.some_method() == '/path-success'

0 comments on commit 55de89a

Please sign in to comment.