Skip to content

Commit 7d8059f

Browse files
committed
Better approach suggested by @sigmavirus24
This required adding a test to cover the line where the validator has already been created.
1 parent b2b9937 commit 7d8059f

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/rfc3986/_mixin.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ def authority_info(self):
5656
def _match_subauthority(self):
5757
return misc.SUBAUTHORITY_MATCHER.match(self.authority)
5858

59-
def _get_base_uri_validator(self):
60-
try:
61-
return self._base_uri_validator
62-
except AttributeError:
63-
self._base_uri_validator = (
64-
validators.Validator().require_presence_of("scheme")
65-
)
66-
return self._base_uri_validator
59+
@property
60+
def _validator(self):
61+
v = getattr(self, "_cached_validator", None)
62+
if v is not None:
63+
return v
64+
self._cached_validator = validators.Validator().require_presence_of(
65+
"scheme"
66+
)
67+
return self._cached_validator
6768

6869
@property
6970
def host(self):
@@ -272,7 +273,7 @@ def resolve_with(self, base_uri, strict=False):
272273
base_uri = type(self).from_string(base_uri)
273274

274275
try:
275-
self._get_base_uri_validator().validate(base_uri)
276+
self._validator.validate(base_uri)
276277
except exc.ValidationError:
277278
raise exc.ResolutionError(base_uri)
278279

tests/test_uri.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,14 @@ def test_resolve_pathless_query(self, basic_uri):
363363
assert T.path is None
364364
assert T.query == "query"
365365

366+
def test_resolve_twice_for_cached_validator(self, basic_uri):
367+
R = URIReference.from_string("")
368+
B = URIReference.from_string(basic_uri)
369+
T1 = R.resolve_with(B)
370+
T2 = R.resolve_with(B)
371+
assert T1 == T2
372+
assert T1 == B
373+
366374

367375
def test_empty_querystrings_persist():
368376
url = "https://httpbin.org/get?"

0 commit comments

Comments
 (0)