Skip to content

Commit 32cfc2f

Browse files
Support External Classes' __rtruediv__ (#831)
return NotImplemented when __truediv__ is called with a non-string.
1 parent 164cab0 commit 32cfc2f

3 files changed

Lines changed: 9 additions & 0 deletions

File tree

CHANGES/832.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Made :py:meth:`URL.__truediv__` return ``NotImplemented`` if called with an unsupported type — by :user:`michaeljpeters`.

tests/test_url.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,12 @@ def test_div_path_starting_from_slash_is_forbidden():
715715
url / "/to/others"
716716

717717

718+
def test_div_bad_type():
719+
url = URL("http://example.com/path/")
720+
with pytest.raises(TypeError):
721+
url / 3
722+
723+
718724
def test_div_cleanup_query_and_fragment():
719725
url = URL("http://example.com/path?a=1#frag")
720726
assert str(url / "to") == "http://example.com/path/to"

yarl/_url.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ def __gt__(self, other):
341341
return self._val > other._val
342342

343343
def __truediv__(self, name):
344+
if not type(name) is str:
345+
return NotImplemented
344346
return self._make_child((name,))
345347

346348
def __mod__(self, query):

0 commit comments

Comments
 (0)