Skip to content

Commit

Permalink
added bytes conversion (#582)
Browse files Browse the repository at this point in the history
* added bytes conversion, fixed some typos and mypy ignore for multidict

* added CHANGES file

* Update setup.cfg

Co-authored-by: Christian Leichsenring <christian.leichsenring@semalytix.com>
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
  • Loading branch information
3 people authored Oct 5, 2021
1 parent 3745b3d commit 248f242
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/582.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `__bytes__()` magic method so that `bytes(url)` will work and use optimal ASCII encoding.
8 changes: 8 additions & 0 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,14 @@ def test_from_non_ascii_path():
) == str(url)


def test_bytes():
url = URL("http://example.com/путь/туда")
assert (
b"http://example.com/%D0%BF%D1%83%D1%82%D1%8C/%D1%82%D1%83%D0%B4%D0%B0"
== bytes(url)
)


def test_from_ascii_query_parts():
url = URL(
"http://example.com/"
Expand Down
3 changes: 3 additions & 0 deletions yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ def __str__(self):
def __repr__(self):
return "{}('{}')".format(self.__class__.__name__, str(self))

def __bytes__(self):
return str(self).encode("ascii")

def __eq__(self, other):
if not type(other) is URL:
return NotImplemented
Expand Down

0 comments on commit 248f242

Please sign in to comment.