Skip to content

Commit

Permalink
Avoid quoting empty strings when building URLs (#1126)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 7, 2024
1 parent 4b26f03 commit 7e94253
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
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

0 comments on commit 7e94253

Please sign in to comment.