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

Avoid quoting empty strings when building URLs #1126

Merged
merged 3 commits into from
Sep 7, 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/1126.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of :meth:`URL.build() <yarl.URL.build>` when the path, query string, or fragment is an empty string -- by :user:`bdraco`.
13 changes: 7 additions & 6 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,22 +349,23 @@ def build(
user, password, host, port, encode=not encoded, encode_host=not encoded
)
if not encoded:
path = cls._PATH_QUOTER(path)
if netloc:
path = cls._PATH_QUOTER(path) if path else path
if path and netloc:
path = cls._normalize_path(path)

cls._validate_authority_uri_abs_path(host=host, path=path)
query_string = cls._QUERY_QUOTER(query_string)
fragment = cls._FRAGMENT_QUOTER(fragment)
query_string = (
cls._QUERY_QUOTER(query_string) if query_string else query_string
)
fragment = cls._FRAGMENT_QUOTER(fragment) if fragment else fragment

url = cls(
SplitResult(scheme, netloc, path, query_string, fragment), encoded=True
)

if query:
return url.with_query(query)
else:
return url
return url

def __init_subclass__(cls):
raise TypeError(f"Inheriting a class {cls!r} from URL is forbidden")
Expand Down
Loading