Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loop variable being redefined in cache_configure #1363

Merged
merged 4 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/1363.breaking.rst
33 changes: 33 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ def test_cache_configure_None() -> None:
)


def test_cache_configure_None_including_deprecated() -> None:
msg = (
r"cache_configure\(\) no longer accepts the ip_address_size "
r"or host_validate_size arguments, they are used to set the "
r"encode_host_size instead and will be removed in the future"
)
with pytest.warns(DeprecationWarning, match=msg):
yarl.cache_configure(
idna_decode_size=None,
idna_encode_size=None,
encode_host_size=None,
ip_address_size=None,
host_validate_size=None,
)
assert yarl.cache_info()["idna_decode"].maxsize is None
assert yarl.cache_info()["idna_encode"].maxsize is None
assert yarl.cache_info()["encode_host"].maxsize is None


def test_cache_configure_None_only_deprecated() -> None:
msg = (
r"cache_configure\(\) no longer accepts the ip_address_size "
r"or host_validate_size arguments, they are used to set the "
r"encode_host_size instead and will be removed in the future"
)
with pytest.warns(DeprecationWarning, match=msg):
yarl.cache_configure(
ip_address_size=None,
host_validate_size=None,
)
assert yarl.cache_info()["encode_host"].maxsize is None


def test_cache_configure_explicit() -> None:
yarl.cache_configure(
idna_decode_size=128,
Expand Down
39 changes: 21 additions & 18 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1467,27 +1467,30 @@ def cache_configure(
global _idna_decode, _idna_encode, _encode_host
# ip_address_size, host_validate_size are no longer
# used, but are kept for backwards compatibility.
if encode_host_size is _SENTINEL:
encode_host_size = _DEFAULT_ENCODE_SIZE
if ip_address_size is not _SENTINEL or host_validate_size is not _SENTINEL:
warnings.warn(
"cache_configure() no longer accepts the "
"ip_address_size or host_validate_size arguments, "
"they are used to set the encode_host_size instead "
"and will be removed in the future",
DeprecationWarning,
stacklevel=2,
)

if encode_host_size is not None:
for size in (ip_address_size, host_validate_size):
if size is not _SENTINEL:
warnings.warn(
"cache_configure() no longer accepts the "
"ip_address_size or host_validate_size arguments, "
"they are used to set the encode_host_size instead "
"and will be removed in the future",
DeprecationWarning,
stacklevel=2,
)
if size is None:
encode_host_size = None
break
elif size is _SENTINEL:
size = _DEFAULT_ENCODE_SIZE
if TYPE_CHECKING:
assert not isinstance(size, object)
if size > encode_host_size:
encode_host_size = size
elif encode_host_size is _SENTINEL:
if size is not _SENTINEL:
encode_host_size = size
elif size is not _SENTINEL:
if TYPE_CHECKING:
assert isinstance(size, int)
assert isinstance(encode_host_size, int)
encode_host_size = max(size, encode_host_size)
if encode_host_size is _SENTINEL:
encode_host_size = _DEFAULT_ENCODE_SIZE

if TYPE_CHECKING:
assert not isinstance(encode_host_size, object)
Expand Down