Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__truediv__ to append a path drops final / #926

Open
1 task done
mscout1 opened this issue Aug 31, 2023 · 2 comments
Open
1 task done

__truediv__ to append a path drops final / #926

mscout1 opened this issue Aug 31, 2023 · 2 comments
Labels

Comments

@mscout1
Copy link

mscout1 commented Aug 31, 2023

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: andrew.svetlov@gmail.com
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: andrew.svetlov@gmail.com
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

  • I agree to follow the aio-libs Code of Conduct
@mscout1 mscout1 added the bug label Aug 31, 2023
@commonism
Copy link
Contributor

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 #704
@mjpieters want to assist?

@Dreamsorcerer
Copy link
Member

So does pathlib though:

>>> print(pathlib.Path("foo") / "bar/baz/")
foo/bar/baz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants