yarl
yarl copied to clipboard
__truediv__ to append a path drops final /
Describe the bug
We have an internal api that cares about whether the url ends in a '/'. This may be a poor design, but such things exist.
When a path is added to a url with the / operator, the final '/' is stripped.
import yarl
from yarl import URL
url = URL(f'http://example.com') / 'foo/bar/baz/'
print(url)
To Reproduce
from yarl import URL
url = URL(f'http://example.com') / 'foo/bar/baz/'
assert str(url).endswith('/'), "where did my slash go?"
url = URL(f'http://example.com') / 'foo' / 'bar'/ 'baz' / ''
assert str(url).endswith('/'), "still no slash?"
Expected behavior
The final slash should be preserved when appending a path with the truediv operator. This would be in accord with the Principle of Least Surprise.
Logs/tracebacks
>>> from yarl import URL
>>> url = URL(f'http://example.com') / 'foo/bar/baz/'
>>> assert str(url).endswith('/'), "where did my slash go?"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: where did my slash go?
>>>
>>> url = URL(f'http://example.com') / 'foo' / 'bar'/ 'baz' / ''
>>> assert str(url).endswith('/'), "still no slash?"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError: still no slash?
Python Version
$ python --version
Python 3.11.4
multidict Version
$ python -m pip show multidict
Name: multidict
Version: 6.0.4
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache 2
Location: /home/matthew/venvs/wxp/lib/python3.11/site-packages
Requires:
Required-by: aiohttp, yarl
yarl Version
$ python -m pip show yarl
Name: yarl
Version: 1.9.2
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl/
Author: Andrew Svetlov
Author-email: [email protected]
License: Apache-2.0
Location: /home/matthew/venvs/wxp/lib/python3.11/site-packages
Requires: idna, multidict
Required-by: aio-pika, aiohttp, aiormq
OS
$ uname -a Linux mattswxp 5.15.0-76-generic #83-Ubuntu SMP Thu Jun 15 19:16:32 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
Additional context
No response
Code of Conduct
- [X] I agree to follow the aio-libs Code of Conduct
Regression from 1.8.2
>>> yarl.__version__
'1.8.2'
>>> URL("https://example.inv/Foo/Bar/") / "test/"
URL('https://example.inv/Foo/Bar/test/')
"bi-sected" this to https://github.com/aio-libs/yarl/pull/704 @mjpieters want to assist?
So does pathlib though:
>>> print(pathlib.Path("foo") / "bar/baz/")
foo/bar/baz
Made a fix in https://github.com/aio-libs/yarl/pull/1023