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

added bytes conversion #582

Merged
merged 4 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ ignore_missing_imports = true
[mypy-pytest]
ignore_missing_imports = true

[mypy-multidict]
ignore_missing_imports = true
asvetlov marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def read(name):

if not NO_EXTENSIONS:
print("**********************")
print("* Accellerated build *")
print("* Accelerated build *")
print("**********************")
setup(ext_modules=extensions, **args)
else:
Expand Down
12 changes: 10 additions & 2 deletions tests/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MyURL(URL): # type: ignore[misc]
pass

assert (
"Inheritance a class "
"Inheriting a class "
"<class 'test_url.test_inheritance.<locals>.MyURL'> "
"from URL is forbidden" == str(ctx.value)
)
Expand Down Expand Up @@ -1025,6 +1025,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 Expand Up @@ -1223,7 +1231,7 @@ def test_join_from_rfc_3986_normal(url, expected):


@pytest.mark.skipif(
sys.version_info < (3, 5), reason="Python 3.4 doen't support abnormal cases"
sys.version_info < (3, 5), reason="Python 3.4 doesn't support abnormal cases"
)
@pytest.mark.parametrize("url,expected", ABNORMAL)
def test_join_from_rfc_3986_abnormal(url, expected):
Expand Down
5 changes: 4 additions & 1 deletion yarl/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def build(
return url

def __init_subclass__(cls):
raise TypeError("Inheritance a class {!r} from URL is forbidden".format(cls))
raise TypeError("Inheriting a class {!r} from URL is forbidden".format(cls))

def __str__(self):
val = self._val
Expand All @@ -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")
asvetlov marked this conversation as resolved.
Show resolved Hide resolved

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