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

Add new method without_query_params to drop query keys #898

Merged
merged 40 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e906337
Method to drop query keys
hongquan Jun 23, 2023
7bd90cc
Merge branch 'master' into feature/drop-query-keys
hongquan Jul 16, 2023
f21e907
Rename the method to `without_query_keys`
hongquan Jul 16, 2023
7adaed1
Merge branch 'master' into feature/drop-query-keys
hongquan Aug 30, 2023
49c3b82
Add documentation about without_query_keys
hongquan Aug 30, 2023
b9bb3ae
Method to drop query keys
hongquan Jun 23, 2023
2bee71e
Rename the method to `without_query_keys`
hongquan Jul 16, 2023
01091a6
Add documentation about without_query_keys
hongquan Aug 30, 2023
e41411a
Merge branch 'master' into feature/drop-query-keys
hongquan Nov 21, 2023
4b0cffd
Merge branch 'feature/drop-query-keys' of github.com:hongquan/yarl in…
hongquan Nov 21, 2023
ebe8b19
🎨 Rename to without_query_params
hongquan Nov 21, 2023
014612f
Make sure without_query_params always returns new object
hongquan Dec 4, 2023
b0ed8a1
Merge branch 'master' into feature/drop-query-keys
hongquan Dec 4, 2023
fb757a5
Revert irrelevant changes
hongquan Dec 5, 2023
ec6a779
Not to copy original query dict
hongquan Dec 5, 2023
c604443
Revert irrelevant changes
hongquan Dec 5, 2023
cd947ba
Merge branch 'master' into feature/drop-query-keys
hongquan Jan 15, 2024
a310b8c
Add without_query_params to type stubs.
hongquan Jan 17, 2024
239afbd
Merge branch 'master' into feature/drop-query-keys
hongquan Jan 17, 2024
417c773
Merge branch 'master' into feature/drop-query-keys
hongquan Feb 6, 2024
abc7d72
Merge branch 'master' into feature/drop-query-keys
bdraco Sep 1, 2024
e64a2fe
Merge branch 'master' into feature/drop-query-keys
hongquan Sep 4, 2024
9caba2b
Merge branch 'feature/drop-query-keys' of github.com:hongquan/yarl in…
hongquan Sep 4, 2024
b0e264f
Adjust test cases to prevent double encoding.
hongquan Sep 4, 2024
dd08f4c
Simplify & operand
hongquan Sep 5, 2024
36b2367
Merge branch 'master' into feature/drop-query-keys
bdraco Sep 5, 2024
7c986c8
Return original URL when no query params to be dropped.
hongquan Sep 6, 2024
fe2772e
Update yarl/_url.py
bdraco Sep 6, 2024
86c34dc
Update yarl/_url.py
bdraco Sep 6, 2024
0bc4eef
Update docs/api.rst
bdraco Sep 6, 2024
6a90990
Apply suggestions from code review
bdraco Sep 6, 2024
7779465
Update docs/api.rst
bdraco Sep 6, 2024
dedd71c
Update docs/api.rst
bdraco Sep 6, 2024
e6cf2f2
Update docs/api.rst
bdraco Sep 6, 2024
672d341
Update docs/api.rst
bdraco Sep 6, 2024
f838f5e
Apply suggestions from code review
bdraco Sep 6, 2024
a01ea9f
Merge branch 'master' into feature/drop-query-keys
bdraco Sep 6, 2024
fbcf6f2
Update CHANGES/898.feature.rst
bdraco Sep 6, 2024
f7aabce
changelog
bdraco Sep 6, 2024
91a8f62
Merge branch 'master' into feature/drop-query-keys
bdraco Sep 6, 2024
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/898.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``without_query_params`` method, to drop some parameters from query string -- by :user:`hongquan`.
bdraco marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,12 @@ section generates a new :class:`URL` instance.
Support subclasses of :class:`int` (except :class:`bool`) and :class:`float`
as a query parameter value.

.. method:: URL.without_query_params(*keys)
bdraco marked this conversation as resolved.
Show resolved Hide resolved
bdraco marked this conversation as resolved.
Show resolved Hide resolved

Return a new URL whose *query* part does not contain specified keys.

It does nothing if none of specified keys are present in the query.
bdraco marked this conversation as resolved.
Show resolved Hide resolved
bdraco marked this conversation as resolved.
Show resolved Hide resolved

.. method:: URL.with_fragment(fragment)

Return a new URL with *fragment* replaced, auto-encode *fragment* if needed.
Expand Down
40 changes: 39 additions & 1 deletion tests/test_url_query.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Tuple
from typing import List, Sequence, Tuple
from urllib.parse import parse_qs, urlencode

import pytest
Expand Down Expand Up @@ -171,3 +171,41 @@ def test_query_from_empty_update_query(

if "b" in original_url.query:
assert new_url.query["b"] == original_url.query["b"]


@pytest.mark.parametrize(
("original_query_string", "keys_to_drop", "expected_query_string"),
[
("a=10&b=M%C3%B9a+xu%C3%A2n&u%E1%BB%91ng=cafe", ["a"], "b=Mùa xuân&uống=cafe"),
("a=10&b=M%C3%B9a+xu%C3%A2n", ["b"], "a=10"),
("a=10&b=M%C3%B9a+xu%C3%A2n&c=30", ["b"], "a=10&c=30"),
(
"a=10&b=M%C3%B9a+xu%C3%A2n&u%E1%BB%91ng=cafe",
["uống"],
"a=10&b=Mùa xuân",
),
("a=10&b=M%C3%B9a+xu%C3%A2n", ["a", "b"], ""),
],
)
def test_without_query_params(
original_query_string: str, keys_to_drop: Sequence[str], expected_query_string: str
):
url = URL(f"http://example.com?{original_query_string}")
bdraco marked this conversation as resolved.
Show resolved Hide resolved
new_url = url.without_query_params(*keys_to_drop)
assert new_url.query_string == expected_query_string
assert new_url is not url
hongquan marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize(
("original_query_string", "keys_to_drop"),
[
("a=10&b=M%C3%B9a+xu%C3%A2n&c=30", ["invalid_key"]),
("a=10&b=M%C3%B9a+xu%C3%A2n", []),
],
)
def test_skip_dropping_query_params(
original_query_string: str, keys_to_drop: Sequence[str]
):
url = URL(f"http://example.com?{original_query_string}")
bdraco marked this conversation as resolved.
Show resolved Hide resolved
new_url = url.without_query_params(*keys_to_drop)
assert new_url is url
14 changes: 13 additions & 1 deletion yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class CacheInfo(TypedDict):


class _SplitResultDict(TypedDict, total=False):
bdraco marked this conversation as resolved.
Show resolved Hide resolved

scheme: str
netloc: str
path: str
Expand Down Expand Up @@ -1163,6 +1162,19 @@ def update_query(self, *args: Any, **kwargs: Any) -> "URL":
self._val._replace(query=self._get_str_query(query) or ""), encoded=True
)

def without_query_params(self, *query_params: str) -> "URL":
bdraco marked this conversation as resolved.
Show resolved Hide resolved
"""Remove some keys from query part and return new URL."""
params_to_remove = set(query_params) & self.query.keys()
if not params_to_remove:
return self
return self.with_query(
tuple(
(name, value)
for name, value in self.query.items()
if name not in params_to_remove
)
)

def with_fragment(self, fragment: Optional[str]) -> "URL":
"""Return a new URL with fragment replaced.

Expand Down
Loading