Skip to content

Commit 63535bf

Browse files
committed
Upgrade for python 3.8 removal
1 parent e52f380 commit 63535bf

39 files changed

+212
-213
lines changed

sanic_ext/bootstrap.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import os
44

55
from types import SimpleNamespace
6-
from typing import Any, Callable, Dict, List, Mapping, Optional, Type, Union
6+
from typing import Any, Callable, Dict, List, Optional, Type, Union
7+
from collections.abc import Mapping
78
from warnings import warn
89

910
from sanic import Sanic, __version__
@@ -42,7 +43,7 @@
4243

4344

4445
class Extend:
45-
_pre_registry: List[Union[Type[Extension], Extension]] = []
46+
_pre_registry: list[Union[type[Extension], Extension]] = []
4647

4748
if TEMPLATING_ENABLED:
4849
environment: Environment
@@ -52,9 +53,9 @@ def __init__(
5253
self,
5354
app: Sanic,
5455
*,
55-
extensions: Optional[List[Union[Type[Extension], Extension]]] = None,
56+
extensions: Optional[list[Union[type[Extension], Extension]]] = None,
5657
built_in_extensions: bool = True,
57-
config: Optional[Union[Config, Dict[str, Any]]] = None,
58+
config: Optional[Union[Config, dict[str, Any]]] = None,
5859
**kwargs,
5960
) -> None:
6061
"""
@@ -81,7 +82,7 @@ def __init__(
8182
self._constant_registry: Optional[ConstantRegistry] = None
8283
self._openapi: Optional[SpecificationBuilder] = None
8384
self.app = app
84-
self.extensions: List[Extension] = []
85+
self.extensions: list[Extension] = []
8586
self.sanic_version = sanic_version
8687
app._ext = self
8788
app.ctx._dependencies = SimpleNamespace()
@@ -128,7 +129,7 @@ def _display(self):
128129

129130
def injection(
130131
self,
131-
type: Type,
132+
type: type,
132133
constructor: Optional[Callable[..., Any]] = None,
133134
) -> None:
134135
warn(
@@ -140,7 +141,7 @@ def injection(
140141

141142
def add_dependency(
142143
self,
143-
type: Type,
144+
type: type,
144145
constructor: Optional[Callable[..., Any]] = None,
145146
request_arg: Optional[str] = None,
146147
) -> None:
@@ -215,7 +216,7 @@ def template(self, template_name: str, **kwargs):
215216
return self.templating.template(template_name, **kwargs)
216217

217218
@classmethod
218-
def register(cls, extension: Union[Type[Extension], Extension]) -> None:
219+
def register(cls, extension: Union[type[Extension], Extension]) -> None:
219220
cls._pre_registry.append(extension)
220221

221222
@classmethod

sanic_ext/config.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import os
44

5-
from typing import Any, Dict, List, Optional, Sequence, Union
5+
from typing import Any, Dict, List, Optional, Union
6+
from collections.abc import Sequence
67

78
from sanic import Sanic
89
from sanic.config import Config as SanicConfig
@@ -23,7 +24,7 @@ def __init__(
2324
cors_expose_headers: str = "",
2425
cors_max_age: int = 5,
2526
cors_methods: str = "",
26-
cors_origins: Union[str, List[str]] = "",
27+
cors_origins: Union[str, list[str]] = "",
2728
cors_send_wildcard: bool = False,
2829
cors_supports_credentials: bool = False,
2930
cors_vary_header: bool = True,
@@ -44,7 +45,7 @@ def __init__(
4445
injection_load_custom_constants: bool = False,
4546
logging: bool = False,
4647
logging_queue_max_size: int = 4096,
47-
loggers: List[str] = [
48+
loggers: list[str] = [
4849
"sanic.access",
4950
"sanic.error",
5051
"sanic.root",
@@ -72,7 +73,7 @@ def __init__(
7273
oas_uri_to_redoc: str = "/redoc",
7374
oas_uri_to_swagger: str = "/swagger",
7475
oas_url_prefix: str = "/docs",
75-
swagger_ui_configuration: Optional[Dict[str, Any]] = None,
76+
swagger_ui_configuration: Optional[dict[str, Any]] = None,
7677
templating_path_to_templates: Union[
7778
str, os.PathLike, Sequence[Union[str, os.PathLike]]
7879
] = "templates",

sanic_ext/extensions/base.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __setitem__(self, key: Any, value: Any) -> None:
1818

1919

2020
class Extension(ABC):
21-
_name_registry: Dict[str, Type[Extension]] = NoDuplicateDict()
21+
_name_registry: dict[str, type[Extension]] = NoDuplicateDict()
2222
_started: bool
2323
name: str
2424
app: Sanic
@@ -65,7 +65,7 @@ def included(self):
6565
@classmethod
6666
def create(
6767
cls,
68-
extension: Union[Type[Extension], Extension],
68+
extension: Union[type[Extension], Extension],
6969
app: Sanic,
7070
config: Config,
7171
) -> Extension:

sanic_ext/extensions/http/cors.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727

2828
@dataclass(frozen=True)
2929
class CORSSettings:
30-
allow_headers: FrozenSet[str]
31-
allow_methods: FrozenSet[str]
32-
allow_origins: Tuple[re.Pattern, ...]
30+
allow_headers: frozenset[str]
31+
allow_methods: frozenset[str]
32+
allow_origins: tuple[re.Pattern, ...]
3333
always_send: bool
3434
automatic_options: bool
35-
expose_headers: FrozenSet[str]
35+
expose_headers: frozenset[str]
3636
max_age: str
3737
send_wildcard: bool
3838
supports_credentials: bool
@@ -86,9 +86,9 @@ async def _assign_cors_settings(app, _):
8686
def cors(
8787
*,
8888
origin: Union[str, Default] = _default,
89-
expose_headers: Union[List[str], Default] = _default,
90-
allow_headers: Union[List[str], Default] = _default,
91-
allow_methods: Union[List[str], Default] = _default,
89+
expose_headers: Union[list[str], Default] = _default,
90+
allow_headers: Union[list[str], Default] = _default,
91+
allow_methods: Union[list[str], Default] = _default,
9292
supports_credentials: Union[bool, Default] = _default,
9393
max_age: Union[str, int, timedelta, Default] = _default,
9494
):
@@ -227,10 +227,10 @@ def _add_credentials_header(request: Request, response: HTTPResponse) -> None:
227227

228228
def _add_allow_header(request: Request, response: HTTPResponse) -> None:
229229
with_credentials = _is_request_with_credentials(request)
230-
request_headers = set(
230+
request_headers = {
231231
h.strip().lower()
232232
for h in request.headers.get(REQUEST_HEADERS_HEADER, "").split(",")
233-
)
233+
}
234234
allow_headers = _get_from_cors_ctx(
235235
request, "_cors_allow_headers", request.app.ctx.cors.allow_headers
236236
)
@@ -297,16 +297,16 @@ def _add_vary_header(request: Request, response: HTTPResponse) -> None:
297297
response.headers[VARY_HEADER] = "origin"
298298

299299

300-
def _get_allow_origins(app: Sanic) -> Tuple[re.Pattern, ...]:
300+
def _get_allow_origins(app: Sanic) -> tuple[re.Pattern, ...]:
301301
origins = app.config.CORS_ORIGINS
302302
return _parse_allow_origins(origins)
303303

304304

305305
def _parse_allow_origins(
306-
value: Union[str, re.Pattern, List[Union[str, re.Pattern]]],
307-
) -> Tuple[re.Pattern, ...]:
306+
value: Union[str, re.Pattern, list[Union[str, re.Pattern]]],
307+
) -> tuple[re.Pattern, ...]:
308308
origins: Optional[
309-
Union[List[str], List[re.Pattern], List[Union[str, re.Pattern]]]
309+
Union[list[str], list[re.Pattern], list[Union[str, re.Pattern]]]
310310
] = None
311311
if value and isinstance(value, str):
312312
if value == "*":
@@ -326,7 +326,7 @@ def _parse_allow_origins(
326326
)
327327

328328

329-
def _get_expose_headers(app: Sanic) -> FrozenSet[str]:
329+
def _get_expose_headers(app: Sanic) -> frozenset[str]:
330330
expose_headers = (
331331
(
332332
app.config.CORS_EXPOSE_HEADERS
@@ -341,11 +341,11 @@ def _get_expose_headers(app: Sanic) -> FrozenSet[str]:
341341
return frozenset(header.lower() for header in expose_headers)
342342

343343

344-
def _get_allow_headers(app: Sanic) -> FrozenSet[str]:
344+
def _get_allow_headers(app: Sanic) -> frozenset[str]:
345345
return _parse_allow_headers(app.config.CORS_ALLOW_HEADERS)
346346

347347

348-
def _parse_allow_headers(value: str) -> FrozenSet[str]:
348+
def _parse_allow_headers(value: str) -> frozenset[str]:
349349
allow_headers = (
350350
(
351351
value
@@ -372,11 +372,11 @@ def _parse_max_age(value) -> str:
372372
return str(max_age)
373373

374374

375-
def _get_allow_methods(app: Sanic) -> FrozenSet[str]:
375+
def _get_allow_methods(app: Sanic) -> frozenset[str]:
376376
return _parse_allow_methods(app.config.CORS_METHODS)
377377

378378

379-
def _parse_allow_methods(value) -> FrozenSet[str]:
379+
def _parse_allow_methods(value) -> frozenset[str]:
380380
allow_methods = (
381381
(
382382
value

sanic_ext/extensions/http/methods.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from functools import partial
22
from inspect import isawaitable
33
from operator import itemgetter
4-
from typing import Sequence, Union
4+
from typing import Union
5+
from collections.abc import Sequence
56

67
from sanic import Sanic
78
from sanic.constants import HTTPMethod

sanic_ext/extensions/injection/constructor.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def __init__(
3939
self, func: Callable[..., Any], request_arg: Optional[str] = None
4040
):
4141
self.func = func
42-
self.injections: Dict[str, Tuple[Type, Constructor]] = {}
43-
self.constants: Dict[str, Any] = {}
42+
self.injections: dict[str, tuple[type, Constructor]] = {}
43+
self.constants: dict[str, Any] = {}
4444
self.pass_kwargs: bool = False
4545
self.request_arg = request_arg
4646

@@ -77,7 +77,7 @@ def prepare(
7777
app: Sanic,
7878
injection_registry: InjectionRegistry,
7979
constant_registry: ConstantRegistry,
80-
allowed_types: Set[Type[object]],
80+
allowed_types: set[type[object]],
8181
) -> None:
8282
hints = self._get_hints()
8383
hints.pop("return", None)
@@ -118,25 +118,25 @@ def prepare(
118118
"html#injecting-services for more details."
119119
)
120120

121-
checked: Set[Type[object]] = set()
122-
current: Set[Type[object]] = set()
121+
checked: set[type[object]] = set()
122+
current: set[type[object]] = set()
123123
self.check_circular(checked, current)
124124

125125
def check_circular(
126126
self,
127-
checked: Set[Type[object]],
128-
current: Set[Type[object]],
127+
checked: set[type[object]],
128+
current: set[type[object]],
129129
) -> None:
130130
dependencies = set(self.injections.values())
131131
for dependency, constructor in dependencies:
132132
self._visit(dependency, constructor, checked, current)
133133

134134
def _visit(
135135
self,
136-
dependency: Type[object],
136+
dependency: type[object],
137137
constructor: Constructor,
138-
checked: Set[Type[object]],
139-
current: Set[Type[object]],
138+
checked: set[type[object]],
139+
current: set[type[object]],
140140
):
141141
if dependency in checked:
142142
return
@@ -167,7 +167,7 @@ def _get_hints(self):
167167
raise InitError(f"Cannot get type hints for {self.func}")
168168

169169

170-
async def gather_args(injections, request, **kwargs) -> Dict[str, Any]:
170+
async def gather_args(injections, request, **kwargs) -> dict[str, Any]:
171171
return {
172172
name: await do_cast(_type, constructor, request, **kwargs)
173173
for name, (_type, constructor) in injections.items()

sanic_ext/extensions/injection/injector.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ def add_injection(
2424

2525
@app.listener("before_server_start", priority=PRIORITY)
2626
async def finalize_injections(app: Sanic, _):
27-
router_converters = set(
27+
router_converters = {
2828
allowed[0] for allowed in app.router.regex_types.values()
29-
)
29+
}
3030
router_types = set()
3131
for converter in router_converters:
3232
if isclass(converter):
@@ -105,10 +105,10 @@ async def setup_signatures(app, _):
105105
except TypeError:
106106
continue
107107

108-
dependencies: Dict[
109-
str, Tuple[Type, Optional[Callable[..., Any]]]
108+
dependencies: dict[
109+
str, tuple[type, Optional[Callable[..., Any]]]
110110
] = {}
111-
constants: Dict[str, Any] = {}
111+
constants: dict[str, Any] = {}
112112
for param, annotation in hints.items():
113113
if annotation in injection_registry:
114114
dependencies[param] = (

sanic_ext/extensions/injection/registry.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
class InjectionRegistry:
1212
def __init__(self):
13-
self._registry: Dict[Type, Optional[Callable[..., Any]]] = {}
13+
self._registry: dict[type, Optional[Callable[..., Any]]] = {}
1414

1515
def __getitem__(self, key):
1616
return self._registry[key]
@@ -26,7 +26,7 @@ def get(self, key, default=None):
2626

2727
def register(
2828
self,
29-
_type: Type,
29+
_type: type,
3030
constructor: Optional[Callable[..., Any]],
3131
request_arg: Optional[str] = None,
3232
) -> None:
@@ -50,11 +50,11 @@ def length(self):
5050

5151
class SignatureRegistry:
5252
def __init__(self):
53-
self._registry: Dict[
53+
self._registry: dict[
5454
str,
55-
Tuple[
56-
Dict[str, Tuple[Type, Optional[Callable[..., Any]]]],
57-
Dict[str, Any],
55+
tuple[
56+
dict[str, tuple[type, Optional[Callable[..., Any]]]],
57+
dict[str, Any],
5858
],
5959
] = {}
6060

@@ -70,16 +70,16 @@ def get(self, key, default=None):
7070
def register(
7171
self,
7272
route_name: str,
73-
dependencies: Dict[str, Tuple[Type, Optional[Callable[..., Any]]]],
74-
constants: Optional[Dict[str, Any]] = None,
73+
dependencies: dict[str, tuple[type, Optional[Callable[..., Any]]]],
74+
constants: Optional[dict[str, Any]] = None,
7575
) -> None:
7676
self._registry[route_name] = (dependencies, constants or {})
7777

7878

7979
class ConstantRegistry:
8080
def __init__(self, config: Config):
8181
self._config = config
82-
self._registry: Set[str] = set()
82+
self._registry: set[str] = set()
8383

8484
def __str__(self) -> str:
8585
return str(self._registry)

0 commit comments

Comments
 (0)