Skip to content

Commit

Permalink
Allow string subclasses in truediv operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mjpieters committed Jun 7, 2023
1 parent 15f2455 commit 6ddd3e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
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

0 comments on commit 6ddd3e6

Please sign in to comment.