File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -224,6 +224,9 @@ def __deepcopy__(self, memo: dict) -> Self:
224
224
def __eq__ (self , other : Any ) -> bool :
225
225
return self .__class__ is other .__class__ and self ._url == other ._url
226
226
227
+ def __hash__ (self ) -> int :
228
+ return hash (self ._url )
229
+
227
230
@classmethod
228
231
def build (
229
232
cls ,
@@ -370,6 +373,9 @@ def __deepcopy__(self, memo: dict) -> Self:
370
373
def __eq__ (self , other : Any ) -> bool :
371
374
return self .__class__ is other .__class__ and self ._url == other ._url
372
375
376
+ def __hash__ (self ) -> int :
377
+ return hash (self ._url )
378
+
373
379
@classmethod
374
380
def build (
375
381
cls ,
Original file line number Diff line number Diff line change @@ -1111,3 +1111,21 @@ def test_serialize_as_any() -> None:
1111
1111
ta = TypeAdapter (Any )
1112
1112
assert ta .dump_python (HttpUrl ('https://example.com' )) == HttpUrl ('https://example.com/' )
1113
1113
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
You can’t perform that action at this time.
0 commit comments