Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitacf2b49

Browse files
author
Liora Milbaum
committed
feat: PEP 544 – Protocols: Structural subtyping (static duck typing)
1 parenta208276 commitacf2b49

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

‎gitlab/_backends/protocol.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import abc
2+
import sys
3+
from typing import Any, Dict, Optional, Union
4+
5+
import requests
6+
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore
7+
8+
if sys.version_info >= (3, 8):
9+
from typing import Protocol
10+
else:
11+
from typing_extensions import Protocol
12+
13+
14+
class BackendResponse(Protocol):
15+
@abc.abstractmethod
16+
def __init__(self, response: requests.Response) -> None:
17+
...
18+
19+
20+
class Backend(Protocol):
21+
@abc.abstractmethod
22+
def http_request(
23+
self,
24+
method: str,
25+
url: str,
26+
json: Optional[Union[Dict[str, Any], bytes]],
27+
data: Optional[Union[Dict[str, Any], MultipartEncoder]],
28+
params: Optional[Any],
29+
timeout: Optional[float],
30+
verify: Optional[Union[bool, str]],
31+
stream: Optional[bool],
32+
**kwargs: Any,
33+
) -> BackendResponse:
34+
...

‎gitlab/_backends/requests_backend.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from requests.structures import CaseInsensitiveDict
77
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore
88

9+
from . import protocol
910

10-
class RequestsResponse:
11+
12+
class RequestsResponse(protocol.BackendResponse):
1113
def __init__(self, response: requests.Response) -> None:
1214
self._response: requests.Response = response
1315

@@ -35,7 +37,7 @@ def json(self) -> Any:
3537
return self._response.json()
3638

3739

38-
class RequestsBackend:
40+
class RequestsBackend(protocol.Backend):
3941
def __init__(self, session: Optional[requests.Session] = None) -> None:
4042
self._client: requests.Session = session or requests.Session()
4143

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp