From 637c4e16cd43eed0d70c7868c8c6415967de5673 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Tue, 29 Dec 2020 21:43:01 +0000 Subject: [PATCH 01/10] Import from typing if available. --- aiohttp/client.py | 6 +++++- aiohttp/hdrs.py | 6 +++++- aiohttp/helpers.py | 6 +++--- aiohttp/http_parser.py | 6 +++++- aiohttp/http_websocket.py | 7 +++++-- aiohttp/locks.py | 7 +------ aiohttp/payload.py | 6 +++++- aiohttp/streams.py | 8 +++----- aiohttp/tracing.py | 5 ++++- aiohttp/web_fileresponse.py | 7 +++++-- aiohttp/web_request.py | 6 +++++- aiohttp/web_urldispatcher.py | 6 +++++- aiohttp/web_ws.py | 6 +++++- setup.py | 2 +- 14 files changed, 57 insertions(+), 27 deletions(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index f0f21f5faca..37ad411928f 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 @@ -88,6 +87,11 @@ from .tracing import Trace, TraceConfig from .typedefs import JSONEncoder, LooseCookies, LooseHeaders, StrOrURL +try: + from typing import Final +except ImportError: + from typing_extensions import Final + __all__ = ( # client_exceptions "ClientConnectionError", diff --git a/aiohttp/hdrs.py b/aiohttp/hdrs.py index d7d8e5000f3..38b03c18a1a 100644 --- a/aiohttp/hdrs.py +++ b/aiohttp/hdrs.py @@ -5,7 +5,11 @@ from typing import Set from multidict import istr -from typing_extensions import Final + +try: + from typing import Final +except ImportError: + 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 a6b14025827..5b2e613a9e5 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -23,6 +23,7 @@ from typing import ( Any, Callable, + ContextManager, Dict, Generator, Generic, @@ -45,7 +46,6 @@ import async_timeout import attr from multidict import MultiDict, MultiDictProxy -from typing_extensions import Protocol from yarl import URL from . import hdrs @@ -64,9 +64,9 @@ idna_ssl.patch_match_hostname() try: - from typing import ContextManager + from typing import Protocol except ImportError: - from typing_extensions import ContextManager + from typing_extensions import Protocol def all_tasks( diff --git a/aiohttp/http_parser.py b/aiohttp/http_parser.py index d3011633165..18a86e20992 100644 --- a/aiohttp/http_parser.py +++ b/aiohttp/http_parser.py @@ -20,7 +20,6 @@ ) from multidict import CIMultiDict, CIMultiDictProxy, istr -from typing_extensions import Final from yarl import URL from . import hdrs @@ -39,6 +38,11 @@ from .streams import EMPTY_PAYLOAD, StreamReader from .typedefs import RawHeaders +try: + from typing import Final +except ImportError: + from typing_extensions import Final + try: import brotli diff --git a/aiohttp/http_websocket.py b/aiohttp/http_websocket.py index aaa169d92d4..2a1455f50b5 100644 --- a/aiohttp/http_websocket.py +++ b/aiohttp/http_websocket.py @@ -11,12 +11,15 @@ from struct import Struct from typing import Any, Callable, List, Optional, Pattern, Set, Tuple, Union -from typing_extensions import Final - from .base_protocol import BaseProtocol from .helpers import NO_EXTENSIONS from .streams import DataQueue +try: + from typing import Final +except ImportError: + from typing_extensions import Final + __all__ = ( "WS_CLOSED_MESSAGE", "WS_CLOSING_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 801730ebe63..3a08562f93c 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 @@ -37,6 +36,11 @@ from .streams import StreamReader from .typedefs import JSONEncoder, _CIMultiDict +try: + from typing import Final +except ImportError: + from typing_extensions import Final + __all__ = ( "PAYLOAD_REGISTRY", "get_payload", diff --git a/aiohttp/streams.py b/aiohttp/streams.py index b450143a7fb..78f40ade40f 100644 --- a/aiohttp/streams.py +++ b/aiohttp/streams.py @@ -1,18 +1,16 @@ 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 + from typing import Final except ImportError: - from typing_extensions import Deque + from typing_extensions import Final __all__ = ( "EMPTY_PAYLOAD", diff --git a/aiohttp/tracing.py b/aiohttp/tracing.py index 4b04b67f28e..57888a78248 100644 --- a/aiohttp/tracing.py +++ b/aiohttp/tracing.py @@ -9,7 +9,10 @@ from .client_reqrep import ClientResponse if TYPE_CHECKING: # pragma: no cover - from typing_extensions import Protocol + try: + from typing import Protocol + except ImportError: + from typing_extensions import Protocol from .client import ClientSession diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index 6e475010fc2..5910a0fa8e4 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -15,8 +15,6 @@ cast, ) -from typing_extensions import Final - from . import hdrs from .abc import AbstractStreamWriter from .typedefs import LooseHeaders @@ -28,6 +26,11 @@ ) from .web_response import StreamResponse +try: + from typing import Final +except ImportError: + from typing_extensions import Final + __all__ = ("FileResponse",) if TYPE_CHECKING: # pragma: no cover diff --git a/aiohttp/web_request.py b/aiohttp/web_request.py index 376ead26157..c842d3c6207 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 @@ -47,6 +46,11 @@ from .web_exceptions import HTTPRequestEntityTooLarge from .web_response import StreamResponse +try: + from typing import Final +except ImportError: + from typing_extensions import Final + __all__ = ("BaseRequest", "FileField", "Request") diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index aa4ece7320b..f7ac32afa8e 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -33,7 +33,6 @@ cast, ) -from typing_extensions import Final, TypedDict from yarl import URL, __version__ as yarl_version # type: ignore from . import hdrs @@ -53,6 +52,11 @@ from .web_response import Response, StreamResponse from .web_routedef import AbstractRouteDef +try: + from typing import Final, TypedDict +except ImportError: + from typing_extensions import Final, TypedDict + __all__ = ( "UrlDispatcher", "UrlMappingMatchInfo", diff --git a/aiohttp/web_ws.py b/aiohttp/web_ws.py index 42a8d143752..54db21e3a41 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 @@ -33,6 +32,11 @@ from .web_request import BaseRequest from .web_response import StreamResponse +try: + from typing import Final +except ImportError: + from typing_extensions import Final + __all__ = ( "WebSocketResponse", "WebSocketReady", 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", ] From e0801b81fa914829380befc8133100441457a612 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Tue, 29 Dec 2020 22:22:13 +0000 Subject: [PATCH 02/10] Use sys.version_info instead of try/except. --- aiohttp/client.py | 4 ++-- aiohttp/hdrs.py | 5 +++-- aiohttp/helpers.py | 4 ++-- aiohttp/http_parser.py | 5 +++-- aiohttp/http_websocket.py | 4 ++-- aiohttp/payload.py | 5 +++-- aiohttp/streams.py | 5 +++-- aiohttp/tracing.py | 5 +++-- aiohttp/web_fileresponse.py | 4 ++-- aiohttp/web_request.py | 5 +++-- aiohttp/web_urldispatcher.py | 5 +++-- aiohttp/web_ws.py | 5 +++-- 12 files changed, 32 insertions(+), 24 deletions(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index 37ad411928f..9df0ce7fd5b 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -87,9 +87,9 @@ from .tracing import Trace, TraceConfig from .typedefs import JSONEncoder, LooseCookies, LooseHeaders, StrOrURL -try: +if sys.version_info >= (3, 8): from typing import Final -except ImportError: +else: from typing_extensions import Final __all__ = ( diff --git a/aiohttp/hdrs.py b/aiohttp/hdrs.py index 38b03c18a1a..a619f2543e4 100644 --- a/aiohttp/hdrs.py +++ b/aiohttp/hdrs.py @@ -2,13 +2,14 @@ # After changing the file content call ./tools/gen.py # to regenerate the headers parser +import sys from typing import Set from multidict import istr -try: +if sys.version_info >= (3, 8): from typing import Final -except ImportError: +else: from typing_extensions import Final METH_ANY: Final[str] = "*" diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 5b2e613a9e5..09bd748669d 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -63,9 +63,9 @@ idna_ssl.patch_match_hostname() -try: +if sys.version_info >= (3, 8): from typing import Protocol -except ImportError: +else: from typing_extensions import Protocol diff --git a/aiohttp/http_parser.py b/aiohttp/http_parser.py index 18a86e20992..903270d24d7 100644 --- a/aiohttp/http_parser.py +++ b/aiohttp/http_parser.py @@ -3,6 +3,7 @@ import collections import re import string +import sys import zlib from contextlib import suppress from enum import IntEnum @@ -38,9 +39,9 @@ from .streams import EMPTY_PAYLOAD, StreamReader from .typedefs import RawHeaders -try: +if sys.version_info >= (3, 8): from typing import Final -except ImportError: +else: from typing_extensions import Final try: diff --git a/aiohttp/http_websocket.py b/aiohttp/http_websocket.py index 2a1455f50b5..f7972d732e9 100644 --- a/aiohttp/http_websocket.py +++ b/aiohttp/http_websocket.py @@ -15,9 +15,9 @@ from .helpers import NO_EXTENSIONS from .streams import DataQueue -try: +if sys.version_info >= (3, 8): from typing import Final -except ImportError: +else: from typing_extensions import Final __all__ = ( diff --git a/aiohttp/payload.py b/aiohttp/payload.py index 3a08562f93c..0c9c6f76b8f 100644 --- a/aiohttp/payload.py +++ b/aiohttp/payload.py @@ -4,6 +4,7 @@ import json import mimetypes import os +import sys import warnings from abc import ABC, abstractmethod from itertools import chain @@ -36,9 +37,9 @@ from .streams import StreamReader from .typedefs import JSONEncoder, _CIMultiDict -try: +if sys.version_info >= (3, 8): from typing import Final -except ImportError: +else: from typing_extensions import Final __all__ = ( diff --git a/aiohttp/streams.py b/aiohttp/streams.py index 78f40ade40f..1770638463a 100644 --- a/aiohttp/streams.py +++ b/aiohttp/streams.py @@ -1,5 +1,6 @@ import asyncio import collections +import sys import warnings from typing import Awaitable, Callable, Deque, Generic, List, Optional, Tuple, TypeVar @@ -7,9 +8,9 @@ from .helpers import BaseTimerContext, set_exception, set_result from .log import internal_logger -try: # pragma: no cover +if sys.version_info >= (3, 8): from typing import Final -except ImportError: +else: from typing_extensions import Final __all__ = ( diff --git a/aiohttp/tracing.py b/aiohttp/tracing.py index 57888a78248..16c58fb11e8 100644 --- a/aiohttp/tracing.py +++ b/aiohttp/tracing.py @@ -1,3 +1,4 @@ +import sys from types import SimpleNamespace from typing import TYPE_CHECKING, Awaitable, Optional, Type, TypeVar @@ -9,9 +10,9 @@ from .client_reqrep import ClientResponse if TYPE_CHECKING: # pragma: no cover - try: + if sys.version_info >= (3, 8): from typing import Protocol - except ImportError: + else: from typing_extensions import Protocol from .client import ClientSession diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index 5910a0fa8e4..15283ddbda1 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -26,9 +26,9 @@ ) from .web_response import StreamResponse -try: +if sys.version_info >= (3, 8): from typing import Final -except ImportError: +else: from typing_extensions import Final __all__ = ("FileResponse",) diff --git a/aiohttp/web_request.py b/aiohttp/web_request.py index c842d3c6207..744a2e34396 100644 --- a/aiohttp/web_request.py +++ b/aiohttp/web_request.py @@ -4,6 +4,7 @@ import re import socket import string +import sys import tempfile import types import warnings @@ -46,9 +47,9 @@ from .web_exceptions import HTTPRequestEntityTooLarge from .web_response import StreamResponse -try: +if sys.version_info >= (3, 8): from typing import Final -except ImportError: +else: from typing_extensions import Final __all__ = ("BaseRequest", "FileField", "Request") diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index f7ac32afa8e..f27a5bb6e5d 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -6,6 +6,7 @@ import keyword import os import re +import sys import warnings from contextlib import contextmanager from functools import wraps @@ -52,9 +53,9 @@ from .web_response import Response, StreamResponse from .web_routedef import AbstractRouteDef -try: +if sys.version_info >= (3, 8): from typing import Final, TypedDict -except ImportError: +else: from typing_extensions import Final, TypedDict __all__ = ( diff --git a/aiohttp/web_ws.py b/aiohttp/web_ws.py index 54db21e3a41..badf6a189c0 100644 --- a/aiohttp/web_ws.py +++ b/aiohttp/web_ws.py @@ -3,6 +3,7 @@ import binascii import hashlib import json +import sys from typing import Any, Iterable, Optional, Tuple import async_timeout @@ -32,9 +33,9 @@ from .web_request import BaseRequest from .web_response import StreamResponse -try: +if sys.version_info >= (3, 8): from typing import Final -except ImportError: +else: from typing_extensions import Final __all__ = ( From d44e1e20417773daae26836e8dd336feaca50697 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Tue, 29 Dec 2020 22:25:23 +0000 Subject: [PATCH 03/10] Add change file. --- CHANGES/5107.bugfix | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGES/5107.bugfix 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 From eb2669b0dc6a8fb439ef5bbe54ba740dfa823720 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Mon, 4 Jan 2021 18:12:20 +0000 Subject: [PATCH 04/10] Import from typedefs. --- aiohttp/client.py | 7 +------ aiohttp/hdrs.py | 6 +----- aiohttp/helpers.py | 7 +------ aiohttp/http_parser.py | 8 +------- aiohttp/http_websocket.py | 6 +----- aiohttp/payload.py | 8 +------- aiohttp/streams.py | 7 +------ aiohttp/tracing.py | 7 +------ aiohttp/typedefs.py | 5 +++++ aiohttp/web_fileresponse.py | 7 +------ aiohttp/web_request.py | 7 +------ aiohttp/web_urldispatcher.py | 8 +------- aiohttp/web_ws.py | 8 +------- 13 files changed, 17 insertions(+), 74 deletions(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index 9df0ce7fd5b..a65324e9f71 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -85,12 +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 - -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final +from .typedefs import Final, JSONEncoder, LooseCookies, LooseHeaders, StrOrURL __all__ = ( # client_exceptions diff --git a/aiohttp/hdrs.py b/aiohttp/hdrs.py index a619f2543e4..e1896d60429 100644 --- a/aiohttp/hdrs.py +++ b/aiohttp/hdrs.py @@ -2,15 +2,11 @@ # After changing the file content call ./tools/gen.py # to regenerate the headers parser -import sys from typing import Set from multidict import istr -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final +from .typedefs import Final METH_ANY: Final[str] = "*" METH_CONNECT: Final[str] = "CONNECT" diff --git a/aiohttp/helpers.py b/aiohttp/helpers.py index 09bd748669d..162869d8842 100644 --- a/aiohttp/helpers.py +++ b/aiohttp/helpers.py @@ -50,7 +50,7 @@ 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() -if sys.version_info >= (3, 8): - from typing import Protocol -else: - from typing_extensions import Protocol - def all_tasks( loop: Optional[asyncio.AbstractEventLoop] = None, diff --git a/aiohttp/http_parser.py b/aiohttp/http_parser.py index 903270d24d7..6781e901921 100644 --- a/aiohttp/http_parser.py +++ b/aiohttp/http_parser.py @@ -3,7 +3,6 @@ import collections import re import string -import sys import zlib from contextlib import suppress from enum import IntEnum @@ -37,12 +36,7 @@ from .http_writer import HttpVersion, HttpVersion10 from .log import internal_logger from .streams import EMPTY_PAYLOAD, StreamReader -from .typedefs import RawHeaders - -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final +from .typedefs import Final, RawHeaders try: import brotli diff --git a/aiohttp/http_websocket.py b/aiohttp/http_websocket.py index f7972d732e9..ff490e112cd 100644 --- a/aiohttp/http_websocket.py +++ b/aiohttp/http_websocket.py @@ -14,11 +14,7 @@ from .base_protocol import BaseProtocol from .helpers import NO_EXTENSIONS from .streams import DataQueue - -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final +from .typedefs import Final __all__ = ( "WS_CLOSED_MESSAGE", diff --git a/aiohttp/payload.py b/aiohttp/payload.py index 0c9c6f76b8f..eaef597f3db 100644 --- a/aiohttp/payload.py +++ b/aiohttp/payload.py @@ -4,7 +4,6 @@ import json import mimetypes import os -import sys import warnings from abc import ABC, abstractmethod from itertools import chain @@ -35,12 +34,7 @@ sentinel, ) from .streams import StreamReader -from .typedefs import JSONEncoder, _CIMultiDict - -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final +from .typedefs import Final, JSONEncoder, _CIMultiDict __all__ = ( "PAYLOAD_REGISTRY", diff --git a/aiohttp/streams.py b/aiohttp/streams.py index 1770638463a..2822ede10f7 100644 --- a/aiohttp/streams.py +++ b/aiohttp/streams.py @@ -1,17 +1,12 @@ import asyncio import collections -import sys import warnings 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 - -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final +from .typedefs import Final __all__ = ( "EMPTY_PAYLOAD", diff --git a/aiohttp/tracing.py b/aiohttp/tracing.py index 16c58fb11e8..a4d188f147b 100644 --- a/aiohttp/tracing.py +++ b/aiohttp/tracing.py @@ -1,4 +1,3 @@ -import sys from types import SimpleNamespace from typing import TYPE_CHECKING, Awaitable, Optional, Type, TypeVar @@ -10,11 +9,7 @@ from .client_reqrep import ClientResponse if TYPE_CHECKING: # pragma: no cover - if sys.version_info >= (3, 8): - from typing import Protocol - else: - from typing_extensions import Protocol - + from .typedefs import Protocol from .client import ClientSession _ParamT_contra = TypeVar("_ParamT_contra", contravariant=True) diff --git a/aiohttp/typedefs.py b/aiohttp/typedefs.py index 1b68a242af5..b550e85942e 100644 --- a/aiohttp/typedefs.py +++ b/aiohttp/typedefs.py @@ -7,6 +7,11 @@ from multidict import CIMultiDict, CIMultiDictProxy, MultiDict, MultiDictProxy, istr from yarl import URL +if sys.version_info >= (3, 8): + from typing import Final, Protocol, TypedDict +else: + from typing_extensions import Final, Protocol, TypedDict + DEFAULT_JSON_ENCODER = json.dumps DEFAULT_JSON_DECODER = json.loads diff --git a/aiohttp/web_fileresponse.py b/aiohttp/web_fileresponse.py index 15283ddbda1..207d6a39712 100644 --- a/aiohttp/web_fileresponse.py +++ b/aiohttp/web_fileresponse.py @@ -17,7 +17,7 @@ from . import hdrs from .abc import AbstractStreamWriter -from .typedefs import LooseHeaders +from .typedefs import Final, LooseHeaders from .web_exceptions import ( HTTPNotModified, HTTPPartialContent, @@ -26,11 +26,6 @@ ) from .web_response import StreamResponse -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final - __all__ = ("FileResponse",) if TYPE_CHECKING: # pragma: no cover diff --git a/aiohttp/web_request.py b/aiohttp/web_request.py index 744a2e34396..91b9e6b1deb 100644 --- a/aiohttp/web_request.py +++ b/aiohttp/web_request.py @@ -4,7 +4,6 @@ import re import socket import string -import sys import tempfile import types import warnings @@ -39,6 +38,7 @@ from .streams import EmptyStreamReader, StreamReader from .typedefs import ( DEFAULT_JSON_DECODER, + Final, JSONDecoder, LooseHeaders, RawHeaders, @@ -47,11 +47,6 @@ from .web_exceptions import HTTPRequestEntityTooLarge from .web_response import StreamResponse -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final - __all__ = ("BaseRequest", "FileField", "Request") diff --git a/aiohttp/web_urldispatcher.py b/aiohttp/web_urldispatcher.py index f27a5bb6e5d..e5031141c31 100644 --- a/aiohttp/web_urldispatcher.py +++ b/aiohttp/web_urldispatcher.py @@ -6,7 +6,6 @@ import keyword import os import re -import sys import warnings from contextlib import contextmanager from functools import wraps @@ -40,7 +39,7 @@ 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, @@ -53,11 +52,6 @@ from .web_response import Response, StreamResponse from .web_routedef import AbstractRouteDef -if sys.version_info >= (3, 8): - from typing import Final, TypedDict -else: - from typing_extensions import Final, TypedDict - __all__ = ( "UrlDispatcher", "UrlMappingMatchInfo", diff --git a/aiohttp/web_ws.py b/aiohttp/web_ws.py index badf6a189c0..3ec5d8e7484 100644 --- a/aiohttp/web_ws.py +++ b/aiohttp/web_ws.py @@ -3,7 +3,6 @@ import binascii import hashlib import json -import sys from typing import Any, Iterable, Optional, Tuple import async_timeout @@ -28,16 +27,11 @@ ) 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 -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final - __all__ = ( "WebSocketResponse", "WebSocketReady", From 4801dd17a34945f8436a7f4162cb6920e3d4e2db Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Mon, 4 Jan 2021 18:17:32 +0000 Subject: [PATCH 05/10] Order imports. --- aiohttp/tracing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiohttp/tracing.py b/aiohttp/tracing.py index a4d188f147b..0d119ebbc58 100644 --- a/aiohttp/tracing.py +++ b/aiohttp/tracing.py @@ -9,8 +9,8 @@ from .client_reqrep import ClientResponse if TYPE_CHECKING: # pragma: no cover - from .typedefs import Protocol from .client import ClientSession + from .typedefs import Protocol _ParamT_contra = TypeVar("_ParamT_contra", contravariant=True) From e402da0e1a5e45d1ddd87ebc43e8a6a63c229f66 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Mon, 4 Jan 2021 18:24:02 +0000 Subject: [PATCH 06/10] Lint. --- aiohttp/typedefs.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/aiohttp/typedefs.py b/aiohttp/typedefs.py index b550e85942e..f42e343ff07 100644 --- a/aiohttp/typedefs.py +++ b/aiohttp/typedefs.py @@ -7,10 +7,11 @@ 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, Protocol, TypedDict + from typing import Final, Protocol, TypedDict # noqa: F401 else: - from typing_extensions import Final, Protocol, TypedDict + from typing_extensions import Final, Protocol, TypedDict # noqa: F401 DEFAULT_JSON_ENCODER = json.dumps DEFAULT_JSON_DECODER = json.loads From 0a3ba8e9438535e89885fa1624205a50cd379e30 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Mon, 4 Jan 2021 18:33:18 +0000 Subject: [PATCH 07/10] Update typedefs.py --- aiohttp/typedefs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aiohttp/typedefs.py b/aiohttp/typedefs.py index f42e343ff07..9f552b1bcc2 100644 --- a/aiohttp/typedefs.py +++ b/aiohttp/typedefs.py @@ -9,7 +9,7 @@ # These are for other modules to use (to avoid repeating the conditional import). if sys.version_info >= (3, 8): - from typing import Final, Protocol, TypedDict # noqa: F401 + from typing import Final, Protocol, TypedDict else: from typing_extensions import Final, Protocol, TypedDict # noqa: F401 From 2eab9bbd57fdc114aa2ba0a1e3823fc2f6b581ae Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Mon, 4 Jan 2021 18:42:40 +0000 Subject: [PATCH 08/10] Revert hdrs.py --- aiohttp/hdrs.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/aiohttp/hdrs.py b/aiohttp/hdrs.py index e1896d60429..a619f2543e4 100644 --- a/aiohttp/hdrs.py +++ b/aiohttp/hdrs.py @@ -2,11 +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 .typedefs 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" From a08357cb2272cf8a350640d19fba1fbccf64a330 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 14 Mar 2021 18:24:34 +0000 Subject: [PATCH 09/10] Fix typing. --- aiohttp/typedefs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aiohttp/typedefs.py b/aiohttp/typedefs.py index 9f552b1bcc2..916895a6d6c 100644 --- a/aiohttp/typedefs.py +++ b/aiohttp/typedefs.py @@ -9,9 +9,9 @@ # These are for other modules to use (to avoid repeating the conditional import). if sys.version_info >= (3, 8): - from typing import Final, Protocol, TypedDict + from typing import Final as Final, Protocol as Protocol, TypedDict as TypedDict else: - from typing_extensions import Final, Protocol, TypedDict # noqa: F401 + from typing_extensions import Final, Protocol as Protocol, TypedDict as TypedDict # noqa: F401 DEFAULT_JSON_ENCODER = json.dumps DEFAULT_JSON_DECODER = json.loads From ff09e432ec4c4037b634eff69ec067186ab81b15 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sun, 14 Mar 2021 18:27:51 +0000 Subject: [PATCH 10/10] Line wrap --- aiohttp/typedefs.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/aiohttp/typedefs.py b/aiohttp/typedefs.py index 916895a6d6c..0e5051910e5 100644 --- a/aiohttp/typedefs.py +++ b/aiohttp/typedefs.py @@ -11,7 +11,11 @@ if sys.version_info >= (3, 8): from typing import Final as Final, Protocol as Protocol, TypedDict as TypedDict else: - from typing_extensions import Final, Protocol as Protocol, TypedDict as TypedDict # noqa: F401 + from typing_extensions import ( # noqa: F401 + Final, + Protocol as Protocol, + TypedDict as TypedDict, + ) DEFAULT_JSON_ENCODER = json.dumps DEFAULT_JSON_DECODER = json.loads