8000 hashing support for urls (#10975) · pydantic/pydantic@02229a6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 02229a6

Browse files
committed
hashing support for urls (#10975)
1 parent a9cf39c commit 02229a6

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

pydantic/networks.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ def __deepcopy__(self, memo: dict) -> Self:
224224
def __eq__(self, other: Any) -> bool:
225225
return self.__class__ is other.__class__ and self._url == other._url
226226

227+
def __hash__(self) -> int:
228+
return hash(self._url)
229+
227230
@classmethod
228231
def build(
229232
cls,
@@ -370,6 +373,9 @@ def __deepcopy__(self, memo: dict) -> Self:
370373
def __eq__(self, other: Any) -> bool:
371374
return self.__class__ is other.__class__ and self._url == other._url
372375

376+
def __hash__(self) -> int:
377+
return hash(self._url)
378+
373379
@classmethod
374380
def build(
375381
cls,

tests/test_networks.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,3 +1111,21 @@ def test_serialize_as_any() -> None:
11111111
ta = TypeAdapter(Any)
11121112
assert ta.dump_python(HttpUrl('https://example.com')) == HttpUrl('https://example.com/')
11131113
assert ta.dump_json('https://example.com') == b'"https://example.com"'
1114+
1115+
1116+
def test_any_url_hashable() -> None:
1117+
example_url_1a = AnyUrl('https://example1.com')
1118+
example_url_1b = AnyUrl('https://example1.com')
1119+
example_url_2 = AnyUrl('https://example2.com')
1120+
1121+
assert hash(example_url_1a) == hash(example_url_1b)
1122+
assert hash( 695D example_url_1a) != hash(example_url_2)
1123+
assert len({example_url_1a, example_url_1b, example_url_2}) == 2
1124+
1125+
example_multi_host_url_1a = PostgresDsn('postgres://user:pass@host1:5432,host2:5432/app')
1126+
example_multi_host_url_1b = PostgresDsn('postgres://user:pass@host1:5432,host2:5432/app')
1127+
example_multi_host_url_2 = PostgresDsn('postgres://user:pass@host1:5432,host3:5432/app')
1128+
1129+
assert hash(example_multi_host_url_1a) == hash(example_multi_host_url_1b)
1130+
assert hash(example_multi_host_url_1a) != hash(example_multi_host_url_2)
1131+
assert len({example_multi_host_url_1a, example_multi_host_url_1b, example_multi_host_url_2}) == 2

0 commit comments

Comments
 (0)
0