`__truediv__` shouldn't mutate the URL
ksonj opened this issue · comments
I'm a big fan of being able to do
>>> p = furl.furl("https://example.com")
>>> p / "some-path"
furl("https://example.com/some-path")
What really freaks me out though, is that this operation mutates p
:
>>> p
furl("https://example.com/some-path")
To be consistent with pathlib.Path
, this should produce a new object and leave p
be:
>>> p = furl.furl("https://example.com")
>>> p / "some-path"
furl("https://example.com/some-path")
>>> p
furl("https://example.com")
So what I am suggesting is to remove the equivalence furl.add(s) == furl.__truediv__(s)
.
If a shorthand for mutating the url in place is needed, we should use __itruediv__
(i.e. p /= "some-path"
) instead. So:
>>> p = furl.furl("https://example.com")
>>> p / "some-path" # Returns furl
furl('https://example.com/some-path')
>>> p
furl('https://example.com')
>>> p /= "some-path" # Returns None
>>> p
furl('https://example.com/some-path')
I am volunteering to prepare a PR, if this is approved of.
I agree.
This is an oversight in the API. __trudiv__
should not mutate the original
furl object.
Usage like
>>> from furl import furl
>>> f = furl('http://www.google.com/')
>>> p1 = f / '11111'
>>> p2 = f / '22222'
>>> f.url, p1.url, p2.url
'http://www.google.com/', 'http://www.google.com/11111', 'http://www.google.com/22222'
should be accomodated and encouraged.
I am volunteering to prepare a PR, if this is approved of.
Awesome. A pull request to resolve this is most welcome. Let me know how I can
help!
This change shipped in furl v2.0.0. Upgrade with
pip install furl --upgrade
Thank you again, @ksonj. Don't hesitate to let me know if there's anything I can
do for you.