From 58187bdb892b3072649d23837efce85d2f79460b Mon Sep 17 00:00:00 2001 From: Christian Leichsenring Date: Tue, 11 May 2021 23:57:59 +0200 Subject: [PATCH 1/3] added bytes conversion, fixed some typos and mypy ignore for multidict --- setup.cfg | 2 ++ setup.py | 2 +- tests/test_url.py | 12 ++++++++++-- yarl/_url.py | 5 ++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/setup.cfg b/setup.cfg index bb54e1394..5dfc62b81 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,3 +30,5 @@ ignore_missing_imports = true [mypy-pytest] ignore_missing_imports = true +[mypy-multidict] +ignore_missing_imports = true diff --git a/setup.py b/setup.py index d47cabcb9..cc81034a5 100644 --- a/setup.py +++ b/setup.py @@ -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: diff --git a/tests/test_url.py b/tests/test_url.py index fa9f89310..071fa1edd 100644 --- a/tests/test_url.py +++ b/tests/test_url.py @@ -15,7 +15,7 @@ class MyURL(URL): # type: ignore[misc] pass assert ( - "Inheritance a class " + "Inheriting a class " ".MyURL'> " "from URL is forbidden" == str(ctx.value) ) @@ -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/" @@ -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): diff --git a/yarl/_url.py b/yarl/_url.py index 99c424514..e5c1e2ce9 100644 --- a/yarl/_url.py +++ b/yarl/_url.py @@ -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 @@ -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 From 0e2bf8bc6a250de161065e96518eb9042a24cd59 Mon Sep 17 00:00:00 2001 From: Christian Leichsenring Date: Wed, 12 May 2021 00:25:34 +0200 Subject: [PATCH 2/3] added CHANGES file --- CHANGES/582.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGES/582.feature diff --git a/CHANGES/582.feature b/CHANGES/582.feature new file mode 100644 index 000000000..2b6766dc2 --- /dev/null +++ b/CHANGES/582.feature @@ -0,0 +1 @@ +Add `__bytes__()` magic method so that `bytes(url)` will work and use optimal ASCII encoding. From 1afed2928b6f58ca0fae6828d87b2b863cf7c990 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Tue, 5 Oct 2021 12:28:56 +0300 Subject: [PATCH 3/3] Update setup.cfg --- setup.cfg | 2 -- 1 file changed, 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index 5dfc62b81..bb54e1394 100644 --- a/setup.cfg +++ b/setup.cfg @@ -30,5 +30,3 @@ ignore_missing_imports = true [mypy-pytest] ignore_missing_imports = true -[mypy-multidict] -ignore_missing_imports = true