diff --git a/CHANGES/4218.misc b/CHANGES/4218.misc new file mode 100644 index 00000000000..1cb60297f81 --- /dev/null +++ b/CHANGES/4218.misc @@ -0,0 +1 @@ +Add two more types to LooseCookies. diff --git a/CHANGES/4250.misc b/CHANGES/4250.misc new file mode 100644 index 00000000000..1c6cbe559d0 --- /dev/null +++ b/CHANGES/4250.misc @@ -0,0 +1 @@ +Add two more types to LooseCookies with tests for the allowed types. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index b7e05b07a13..6af2ebd63ea 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -1,6 +1,7 @@ - Contributors - ---------------- A. Jesse Jiryu Davis +Adam Bannister Adam Cooper Adam Mills Adrián Chaves diff --git a/aiohttp/cookiejar.py b/aiohttp/cookiejar.py index ac102bfc487..2d858de0e18 100644 --- a/aiohttp/cookiejar.py +++ b/aiohttp/cookiejar.py @@ -124,7 +124,7 @@ def update_cookies(self, return if isinstance(cookies, Mapping): - cookies = cookies.items() # type: ignore + cookies = cookies.items() for name, cookie in cookies: if not isinstance(cookie, Morsel): diff --git a/aiohttp/typedefs.py b/aiohttp/typedefs.py index 3c42f6abe30..9154993cdbb 100644 --- a/aiohttp/typedefs.py +++ b/aiohttp/typedefs.py @@ -29,7 +29,7 @@ _CIMultiDictProxy = CIMultiDictProxy[str] _MultiDict = MultiDict[str] _MultiDictProxy = MultiDictProxy[str] - from http.cookies import BaseCookie # noqa + from http.cookies import BaseCookie, Morsel # noqa else: _CIMultiDict = CIMultiDict _CIMultiDictProxy = CIMultiDictProxy @@ -43,8 +43,20 @@ _CIMultiDictProxy] RawHeaders = Tuple[Tuple[bytes, bytes], ...] StrOrURL = Union[str, URL] -LooseCookies = Union[Iterable[Tuple[str, 'BaseCookie[str]']], - Mapping[str, 'BaseCookie[str]'], 'BaseCookie[str]'] + +StrBaseCookieTuples = Iterable[Tuple[str, 'BaseCookie[str]']] +StrMorselTuples = Iterable[Tuple[str, 'Morsel[str]']] +StrToBaseCookieMapping = Mapping[str, 'BaseCookie[str]'] +StrToMorselMapping = Mapping[str, 'Morsel[Any]'] +LooseCookies = Union[ + StrBaseCookieTuples, + StrMorselTuples, + Iterable[Tuple[str, str]], + StrToBaseCookieMapping, + StrToMorselMapping, + Mapping[str, str], + 'BaseCookie[str]', +] if sys.version_info >= (3, 6): diff --git a/tests/test_cookiejar.py b/tests/test_cookiejar.py index a024364b787..082dce676ce 100644 --- a/tests/test_cookiejar.py +++ b/tests/test_cookiejar.py @@ -3,7 +3,7 @@ import itertools import pathlib import unittest -from http.cookies import SimpleCookie +from http.cookies import SimpleCookie, BaseCookie, Morsel from unittest import mock import pytest @@ -662,3 +662,20 @@ async def test_dummy_cookie_jar() -> None: next(iter(dummy_jar)) assert not dummy_jar.filter_cookies(URL("http://example.com/")) dummy_jar.clear() + + +async def test_loose_cookies_types() -> None: + jar = CookieJar() + + accepted_types = { + 'StrBaseCookieTuples': [('str', BaseCookie())], + 'StrMorselTuples': [('str', Morsel())], + 'StrStrTuples': [('str', 'str'), ], + 'StrToBaseCookieMapping': {'str': BaseCookie()}, + 'StrToMorselMapping': {'str': Morsel()}, + 'StrToStrMapping': {'str': 'str'}, + 'BaseCookie[str]': SimpleCookie(), + } + + for name, loose_cookies_type in accepted_types.items(): + jar.update_cookies(cookies=loose_cookies_type)