Skip to content

Commit

Permalink
Construct self._val lazily
Browse files Browse the repository at this point in the history
closes #172
  • Loading branch information
bdraco committed Oct 30, 2024
1 parent 41af4a3 commit a3f0b6d
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 104 deletions.
2 changes: 0 additions & 2 deletions tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def test_pickle():
v = pickle.dumps(u1)
u2 = pickle.loads(v)
assert u1._cache
assert not u2._cache
assert hash(u1) == hash(u2)


Expand All @@ -20,5 +19,4 @@ def test_default_style_state():
hash(u)
val = ("test", "test", "test", "test", "test")
u.__setstate__((None, {"_val": val, "_strict": False, "_cache": {"hash": 1}}))
assert not u._cache
assert u._val == val
32 changes: 10 additions & 22 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -2176,35 +2176,23 @@ def test_join_encoded_url():
def test_parsing_populates_cache():
"""Test that parsing a URL populates the cache."""
url = URL("http://user:password@example.com:80/path?a=b#frag")
assert url._cache["raw_user"] == "user"
assert url._cache["raw_password"] == "password"
assert url._cache["raw_host"] == "example.com"
assert url._cache["explicit_port"] == 80
assert url._cache["raw_query_string"] == "a=b"
assert url._cache["raw_fragment"] == "frag"
assert url._cache["scheme"] == "http"
c = url._cache
assert c["raw_user"] == "user"
assert c["raw_password"] == "password"
assert c["raw_host"] == "example.com"
assert c["explicit_port"] == 80
assert c["raw_query_string"] == "a=b"
assert c["raw_fragment"] == "frag"
assert c["scheme"] == "http"
assert c["raw_netloc"] == "user:password@example.com:80"
assert c["_path"] == "/path"
assert url.raw_user == "user"
assert url.raw_password == "password"
assert url.raw_host == "example.com"
assert url.explicit_port == 80
assert url.raw_query_string == "a=b"
assert url.raw_fragment == "frag"
assert url.scheme == "http"
url._cache.clear()
assert url.raw_user == "user"
assert url.raw_password == "password"
assert url.raw_host == "example.com"
assert url.explicit_port == 80
assert url.raw_query_string == "a=b"
assert url.raw_fragment == "frag"
assert url.scheme == "http"
assert url._cache["raw_user"] == "user"
assert url._cache["raw_password"] == "password"
assert url._cache["raw_host"] == "example.com"
assert url._cache["explicit_port"] == 80
assert url._cache["raw_query_string"] == "a=b"
assert url._cache["raw_fragment"] == "frag"
assert url._cache["scheme"] == "http"


@pytest.mark.parametrize(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_url_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_raw_host_empty_cache(benchmark: BenchmarkFixture) -> None:
@benchmark
def _run() -> None:
for _ in range(100):
url._cache = {}
url._cache.pop("raw_host", None)
url.raw_host


Expand Down
Loading

0 comments on commit a3f0b6d

Please sign in to comment.