Skip to content

Commit 4afeaff

Browse files
author
Liora Milbaum
authored
feat(backends): use PEP544 protocols for structural subtyping (#2442)
The purpose of this change is to track API changes described in https://github.com/python-gitlab/python-gitlab/blob/main/docs/api-levels.rst, for example, for package versioning and breaking change announcements in case of protocol changes. This is MVP implementation to be used by #2435.
1 parent 3d7ca1c commit 4afeaff

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

gitlab/_backends/protocol.py

+34
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

+4-2
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)