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

Allow string subclasses in truediv operations #884

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/871.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Accept string subclasses in truediv operations (``URL / segment``)
13 changes: 13 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from urllib.parse import SplitResult

import pytest
Expand Down Expand Up @@ -719,6 +720,18 @@ def test_div_path_starting_from_slash_is_forbidden():
url / "/to/others"


class StrEnum(str, Enum):
spam = "ham"

def __str__(self):
return self.value


def test_div_path_srting_subclass():
url = URL("http://example.com/path/") / StrEnum.spam
assert str(url) == "http://example.com/path/ham"


def test_div_bad_type():
url = URL("http://example.com/path/")
with pytest.raises(TypeError):
Expand Down
4 changes: 2 additions & 2 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ def __gt__(self, other):
return self._val > other._val

def __truediv__(self, name):
if not type(name) is str:
if not isinstance(name, str):
return NotImplemented
return self._make_child((name,))
return self._make_child((str(name),))

def __mod__(self, query):
return self.update_query(query)
Expand Down