diff --git a/CHANGES/5107.bugfix b/CHANGES/5107.bugfix new file mode 100644 index 00000000000..4287bfad126 --- /dev/null +++ b/CHANGES/5107.bugfix @@ -0,0 +1 @@ +Only depend on typing_extensions for Python <3.8 diff --git a/aiohttp/client.py b/aiohttp/client.py index cc45eb584a8..4179ebb184d 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -30,7 +30,6 @@ import attr from multidict import CIMultiDict, MultiDict, MultiDictProxy, istr -from typing_extensions import Final from yarl import URL from . import hdrs, http, payload @@ -86,7 +85,7 @@ from .http_websocket import WSHandshakeError, WSMessage, ws_ext_gen, ws_ext_parse from .streams import FlowControlDataQueue from .tracing import Trace, TraceConfig -from .typedefs import JSONEncoder, LooseCookies, LooseHeaders, StrOrURL +from .typedefs import Final, JSONEncoder, LooseCookies, LooseHeaders, StrOrURL __all__ = ( # client_exceptions diff --git a/aiohttp/hdrs.py b/aiohttp/hdrs.py index d7d8e5000f3..a619f2543e4 100644 --- a/aiohttp/hdrs.py +++ b/aiohttp/hdrs.py @@ -2,10 +2,15 @@ # After changing the file content call ./tools/gen.py # to regenerate the headers parser +import sys from typing import Set from multidict import istr -from typing_extensions import Final + +if sys.version_info >= (3, 8): + from typing import Final +else: + from typing_extensions import Final METH_ANY: Final[str] = "*" METH_CONNECT: Final[str] = "CONNECT" diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 04595785bd6..393b55abdf7 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -23,6 +23,7 @@ from typing import ( Any, Callable, + ContextManager, Dict, Generator, Generic, @@ -45,12 +46,11 @@ import async_timeout import attr from multidict import MultiDict, MultiDictProxy -from typing_extensions import Protocol from yarl import URL from . import hdrs from .log import client_logger, internal_logger -from .typedefs import PathLike # noqa +from .typedefs import PathLike, Protocol # noqa __all__ = ("BasicAuth", "ChainMapProxy") @@ -63,11 +63,6 @@ idna_ssl.patch_match_hostname() -try: - from typing import ContextManager -except ImportError: - from typing_extensions import ContextManager - def all_tasks( loop: Optional[asyncio.AbstractEventLoop] = None, diff --git a/aiohttp/http_parser.py b/aiohttp/http_parser.py index 20b813cb8d8..508e1cf4e6d 100644 --- a/aiohttp/http_parser.py +++ b/aiohttp/http_parser.py @@ -22,7 +22,6 @@ ) from multidict import CIMultiDict, CIMultiDictProxy, istr -from typing_extensions import Final from yarl import URL from . import hdrs @@ -39,7 +38,7 @@ from .http_writer import HttpVersion, HttpVersion10 from .log import internal_logger from .streams import EMPTY_PAYLOAD, StreamReader -from .typedefs import RawHeaders +from .typedefs import Final, RawHeaders try: import brotli diff --git a/aiohttp/http_websocket.py b/aiohttp/http_websocket.py index e5925c410db..991a149d09f 100644 --- a/aiohttp/http_websocket.py +++ b/aiohttp/http_websocket.py @@ -11,11 +11,10 @@ from struct import Struct from typing import Any, Callable, List, Optional, Pattern, Set, Tuple, Union, cast -from typing_extensions import Final - from .base_protocol import BaseProtocol from .helpers import NO_EXTENSIONS from .streams import DataQueue +from .typedefs import Final __all__ = ( "WS_CLOSED_MESSAGE", diff --git a/aiohttp/locks.py b/aiohttp/locks.py index ce5b9c6f731..f3456af781d 100644 --- a/aiohttp/locks.py +++ b/aiohttp/locks.py @@ -1,11 +1,6 @@ import asyncio import collections -from typing import Any, Optional - -try: - from typing import Deque -except ImportError: - from typing_extensions import Deque +from typing import Any, Deque, Optional class EventResultOrError: diff --git a/aiohttp/payload.py b/aiohttp/payload.py index 3cba1c89740..097bf5da1c3 100644 --- a/aiohttp/payload.py +++ b/aiohttp/payload.py @@ -23,7 +23,6 @@ ) from multidict import CIMultiDict -from typing_extensions import Final from . import hdrs from .abc import AbstractStreamWriter @@ -35,7 +34,7 @@ sentinel, ) from .streams import StreamReader -from .typedefs import JSONEncoder, _CIMultiDict +from .typedefs import Final, JSONEncoder, _CIMultiDict __all__ = ( "PAYLOAD_REGISTRY", diff --git a/aiohttp/streams.py b/aiohttp/streams.py index 04910e272a7..a20e27c0fac 100644 --- a/aiohttp/streams.py +++ b/aiohttp/streams.py @@ -1,18 +1,12 @@ import asyncio import collections import warnings -from typing import Awaitable, Callable, Generic, List, Optional, Tuple, TypeVar - -from typing_extensions import Final +from typing import Awaitable, Callable, Deque, Generic, List, Optional, Tuple, TypeVar from .base_protocol import BaseProtocol from .helpers import BaseTimerContext, set_exception, set_result from .log import internal_logger - -try: # pragma: no cover - from typing import Deque -except ImportError: - from typing_extensions import Deque +from .typedefs import Final __all__ = ( "EMPTY_PAYLOAD", diff --git a/aiohttp/tracing.py b/aiohttp/tracing.py index 4b04b67f28e..0d119ebbc58 100644 --- a/aiohttp/tracing.py +++ b/aiohttp/tracing.py @@ -9,9 +9,8 @@ from .client_reqrep import ClientResponse if TYPE_CHECKING: # pragma: no cover - from typing_extensions import Protocol - from .client import ClientSession + from .typedefs import Protocol _ParamT_contra = TypeVar("_ParamT_contra", contravariant=True) diff --git a/aiohttp/typedefs.py b/aiohttp/typedefs.py index 1b68a242af5..0e5051910e5 100644 --- a/aiohttp/typedefs.py +++ b/aiohttp/typedefs.py @@ -7,6 +7,16 @@ from multidict import CIMultiDict, CIMultiDictProxy, MultiDict, MultiDictProxy, istr from yarl import URL +# These are for other modules to use (to avoid repeating the conditional import). +if sys.version_info >= (3, 8): + from typing import Final as Final, Protocol as Protocol, TypedDict as TypedDict +else: + from typing_extensions import ( # noqa: F401 + Final, + Protocol as Protocol, + TypedDict as TypedDict, + ) + DEFAULT_JSON_ENCODER = json.dumps DEFAULT_JSON_DECODER = json.loads diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index ff904dd57b0..64348e6c79f 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -15,11 +15,9 @@ cast, ) -from typing_extensions import Final - from . import hdrs from .abc import AbstractStreamWriter -from .typedefs import LooseHeaders +from .typedefs import Final, LooseHeaders from .web_exceptions import ( HTTPNotModified, HTTPPartialContent, diff --git a/aiohttp/web_request.py b/aiohttp/web_request.py index a7f2ee66412..3e4f7c50d9f 100644 --- a/aiohttp/web_request.py +++ b/aiohttp/web_request.py @@ -27,7 +27,6 @@ import attr from multidict import CIMultiDict, CIMultiDictProxy, MultiDict, MultiDictProxy -from typing_extensions import Final from yarl import URL from . import hdrs @@ -39,6 +38,7 @@ from .streams import EmptyStreamReader, StreamReader from .typedefs import ( DEFAULT_JSON_DECODER, + Final, JSONDecoder, LooseHeaders, RawHeaders, diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index 59b0f8b03a9..2e42a61d17b 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -33,14 +33,13 @@ cast, ) -from typing_extensions import Final, TypedDict from yarl import URL, __version__ as yarl_version # type: ignore[attr-defined] from . import hdrs from .abc import AbstractMatchInfo, AbstractRouter, AbstractView from .helpers import DEBUG from .http import HttpVersion11 -from .typedefs import PathLike +from .typedefs import Final, PathLike, TypedDict from .web_exceptions import ( HTTPException, HTTPExpectationFailed, diff --git a/aiohttp/web_ws.py b/aiohttp/web_ws.py index 31c52a93cf7..16b0a1747cf 100644 --- a/aiohttp/web_ws.py +++ b/aiohttp/web_ws.py @@ -8,7 +8,6 @@ import async_timeout import attr from multidict import CIMultiDict -from typing_extensions import Final from . import hdrs from .abc import AbstractStreamWriter @@ -28,7 +27,7 @@ ) from .log import ws_logger from .streams import EofStream, FlowControlDataQueue -from .typedefs import JSONDecoder, JSONEncoder +from .typedefs import Final, JSONDecoder, JSONEncoder from .web_exceptions import HTTPBadRequest, HTTPException from .web_request import BaseRequest from .web_response import StreamResponse diff --git a/setup.py b/setup.py index 254fa13ab74..839010faae9 100644 --- a/setup.py +++ b/setup.py @@ -70,7 +70,7 @@ def build_extension(self, ext): 'asynctest==0.13.0; python_version<"3.8"', "yarl>=1.0,<2.0", 'idna-ssl>=1.0; python_version<"3.7"', - "typing_extensions>=3.7.4", + 'typing_extensions>=3.7.4; python_version<"3.8"', "frozenlist>=1.1.1", "aiosignal>=1.1.2", ]