Add URL.joinpath(*elements, encoding=False) to build a URL with multiple new path elements#704
Conversation
Codecov Report
@@ Coverage Diff @@
## master #704 +/- ##
=======================================
Coverage 99.73% 99.73%
=======================================
Files 4 4
Lines 750 750
Branches 170 212 +42
=======================================
Hits 748 748
Misses 2 2
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
|
How different is this new method compared to using the following? >>> url = URL("http://example.com/foo")
>>> url / "bar" / "ham" / "spam/sub"
URL('http://example.com/foo/bar/ham/spam/sub') |
You can't use that expression with a dynamic list of path elements, not without having to use an explicit loop. Without this method you can't extend a series of URLs in a list comprehension, say: >>> import random
>>> from yarl import URL
>>> urls = [URL("http://example.com/foo"), URL("http://example.com/bar")]
>>> names = ("terry_jones", "eric_idle", "john_cleese", "micheal_palin", "graham_chapman", "terry_gilliam")
>>> [url.joinpath("spam", *random.choices(names, k=random.randint(1, 3))) for url in urls]
[URL("http://example.com/foo/spam/micheal_palin"), URL("http://example.com/bar/spam/eric_idle/terry_gilliam")]This is modelled on |
|
@mjpieters I've rebased the PR and it still passes but there's still a few things I'd like improved so I won't be merging it right now. |
|
@webknjaz all comments addressed now. |
|
The pull request looks good but merge conflict should be resolved first. |
…ple new path elements This is analogous to `Path(...).joinpath(...)`, except that it will not accept elements that start with `/`.
Done! I'll merge it now that it's been green-lighted. |
|
Thanks @mjpieters ! |
What do these changes do?
This adds a new method,
URL.joinpath(), which creates a new URL object withthe arguments added as path elements:
The method accepts an
encodedflag so you can pass in already-encoded pathelements.
Are there changes in behavior for the user?
There are no changes in behaviour for existing methods, but I did refactor
.__truediv__to share code with the implementation for.joinpath().Checklist
CHANGESfolder<issue_id>.<type>(e.g.588.bugfix)issue_idchange it to the pr id after creating the PR.feature: Signifying a new feature..bugfix: Signifying a bug fix..doc: Signifying a documentation improvement..removal: Signifying a deprecation or removal of public API..misc: A ticket has been closed, but it is not of interest to users.Fix issue with non-ascii contents in doctest text files.