diff --git a/CHANGES/1126.misc.rst b/CHANGES/1126.misc.rst new file mode 100644 index 000000000..ba21def5b --- /dev/null +++ b/CHANGES/1126.misc.rst @@ -0,0 +1 @@ +Improved performance of :meth:`URL.build() ` when the path, query string, or fragment is an empty string -- by :user:`bdraco`. diff --git a/yarl/_url.py b/yarl/_url.py index 35c0e5b6c..16b0f0355 100644 --- a/yarl/_url.py +++ b/yarl/_url.py @@ -349,13 +349,15 @@ 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 @@ -363,8 +365,7 @@ def build( 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")