Skip to content

Commit

Permalink
Support External Classes' __rtruediv__ (#831)
Browse files Browse the repository at this point in the history
return NotImplemented when __truediv__ is called with a non-string.
  • Loading branch information
michaeljpeters committed Apr 19, 2023
1 parent 164cab0 commit 32cfc2f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/832.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Made :py:meth:`URL.__truediv__` return ``NotImplemented`` if called with an unsupported type — by :user:`michaeljpeters`.
6 changes: 6 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,12 @@ def test_div_path_starting_from_slash_is_forbidden():
url / "/to/others"


def test_div_bad_type():
url = URL("http://example.com/path/")
with pytest.raises(TypeError):
url / 3


def test_div_cleanup_query_and_fragment():
url = URL("http://example.com/path?a=1#frag")
assert str(url / "to") == "http://example.com/path/to"
Expand Down
2 changes: 2 additions & 0 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ def __gt__(self, other):
return self._val > other._val

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

def __mod__(self, query):
Expand Down

0 comments on commit 32cfc2f

Please sign in to comment.