Skip to content

Commit 32352c3

Browse files
committed
refactor: use more python3.9 syntax
1 parent 9734ad4 commit 32352c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+773
-820
lines changed

gitlab/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# Copyright (C) 2013-2019 Gauvain Pocentek, 2019-2023 python-gitlab team
43
#

gitlab/_backends/protocol.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1+
from __future__ import annotations
2+
13
import abc
2-
import sys
3-
from typing import Any, Dict, Optional, Union
4+
from typing import Any, Protocol
45

56
import requests
67
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore
78

8-
if sys.version_info >= (3, 8):
9-
from typing import Protocol
10-
else:
11-
from typing_extensions import Protocol
12-
139

1410
class BackendResponse(Protocol):
1511
@abc.abstractmethod
@@ -22,11 +18,11 @@ def http_request(
2218
self,
2319
method: str,
2420
url: str,
25-
json: Optional[Union[Dict[str, Any], bytes]],
26-
data: Optional[Union[Dict[str, Any], MultipartEncoder]],
27-
params: Optional[Any],
28-
timeout: Optional[float],
29-
verify: Optional[Union[bool, str]],
30-
stream: Optional[bool],
21+
json: dict[str, Any] | bytes | None,
22+
data: dict[str, Any] | MultipartEncoder | None,
23+
params: Any | None,
24+
timeout: float | None,
25+
verify: bool | str | None,
26+
stream: bool | None,
3127
**kwargs: Any,
3228
) -> BackendResponse: ...

gitlab/_backends/requests_backend.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import dataclasses
4-
from typing import Any, BinaryIO, Dict, Optional, TYPE_CHECKING, Union
4+
from typing import Any, BinaryIO, TYPE_CHECKING
55

66
import requests
77
from requests import PreparedRequest
@@ -44,8 +44,8 @@ def __call__(self, r: PreparedRequest) -> PreparedRequest:
4444
@dataclasses.dataclass
4545
class SendData:
4646
content_type: str
47-
data: Optional[Union[Dict[str, Any], MultipartEncoder]] = None
48-
json: Optional[Union[Dict[str, Any], bytes]] = None
47+
data: dict[str, Any] | MultipartEncoder | None = None
48+
json: dict[str, Any] | bytes | None = None
4949

5050
def __post_init__(self) -> None:
5151
if self.json is not None and self.data is not None:
@@ -84,7 +84,7 @@ def json(self) -> Any:
8484

8585

8686
class RequestsBackend(protocol.Backend):
87-
def __init__(self, session: Optional[requests.Session] = None) -> None:
87+
def __init__(self, session: requests.Session | None = None) -> None:
8888
self._client: requests.Session = session or requests.Session()
8989

9090
@property
@@ -93,8 +93,8 @@ def client(self) -> requests.Session:
9393

9494
@staticmethod
9595
def prepare_send_data(
96-
files: Optional[Dict[str, Any]] = None,
97-
post_data: Optional[Union[Dict[str, Any], bytes, BinaryIO]] = None,
96+
files: dict[str, Any] | None = None,
97+
post_data: dict[str, Any] | bytes | BinaryIO | None = None,
9898
raw: bool = False,
9999
) -> SendData:
100100
if files:
@@ -130,12 +130,12 @@ def http_request(
130130
self,
131131
method: str,
132132
url: str,
133-
json: Optional[Union[Dict[str, Any], bytes]] = None,
134-
data: Optional[Union[Dict[str, Any], MultipartEncoder]] = None,
135-
params: Optional[Any] = None,
136-
timeout: Optional[float] = None,
137-
verify: Optional[Union[bool, str]] = True,
138-
stream: Optional[bool] = False,
133+
json: dict[str, Any] | bytes | None = None,
134+
data: dict[str, Any] | MultipartEncoder | None = None,
135+
params: Any | None = None,
136+
timeout: float | None = None,
137+
verify: bool | str | None = True,
138+
stream: bool | None = False,
139139
**kwargs: Any,
140140
) -> RequestsResponse:
141141
"""Make HTTP request

gitlab/base.py

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
from __future__ import annotations
2+
13
import copy
24
import importlib
35
import json
46
import pprint
57
import textwrap
8+
from collections.abc import Iterable
69
from types import ModuleType
7-
from typing import Any, Dict, Iterable, Optional, Type, TYPE_CHECKING, Union
10+
from typing import Any, TYPE_CHECKING
811

912
import gitlab
1013
from gitlab import types as g_types
@@ -40,20 +43,20 @@ class RESTObject:
4043
object's ``__repr__()`` method.
4144
"""
4245

43-
_id_attr: Optional[str] = "id"
44-
_attrs: Dict[str, Any]
46+
_id_attr: str | None = "id"
47+
_attrs: dict[str, Any]
4548
_created_from_list: bool # Indicates if object was created from a list() action
4649
_module: ModuleType
47-
_parent_attrs: Dict[str, Any]
48-
_repr_attr: Optional[str] = None
49-
_updated_attrs: Dict[str, Any]
50+
_parent_attrs: dict[str, Any]
51+
_repr_attr: str | None = None
52+
_updated_attrs: dict[str, Any]
5053
_lazy: bool
51-
manager: "RESTManager"
54+
manager: RESTManager
5255

5356
def __init__(
5457
self,
55-
manager: "RESTManager",
56-
attrs: Dict[str, Any],
58+
manager: RESTManager,
59+
attrs: dict[str, Any],
5760
*,
5861
created_from_list: bool = False,
5962
lazy: bool = False,
@@ -77,13 +80,13 @@ def __init__(
7780
self.__dict__["_parent_attrs"] = self.manager.parent_attrs
7881
self._create_managers()
7982

80-
def __getstate__(self) -> Dict[str, Any]:
83+
def __getstate__(self) -> dict[str, Any]:
8184
state = self.__dict__.copy()
8285
module = state.pop("_module")
8386
state["_module_name"] = module.__name__
8487
return state
8588

86-
def __setstate__(self, state: Dict[str, Any]) -> None:
89+
def __setstate__(self, state: dict[str, Any]) -> None:
8790
module_name = state.pop("_module_name")
8891
self.__dict__.update(state)
8992
self.__dict__["_module"] = importlib.import_module(module_name)
@@ -136,7 +139,7 @@ def __getattr__(self, name: str) -> Any:
136139
def __setattr__(self, name: str, value: Any) -> None:
137140
self.__dict__["_updated_attrs"][name] = value
138141

139-
def asdict(self, *, with_parent_attrs: bool = False) -> Dict[str, Any]:
142+
def asdict(self, *, with_parent_attrs: bool = False) -> dict[str, Any]:
140143
data = {}
141144
if with_parent_attrs:
142145
data.update(copy.deepcopy(self._parent_attrs))
@@ -145,7 +148,7 @@ def asdict(self, *, with_parent_attrs: bool = False) -> Dict[str, Any]:
145148
return data
146149

147150
@property
148-
def attributes(self) -> Dict[str, Any]:
151+
def attributes(self) -> dict[str, Any]:
149152
return self.asdict(with_parent_attrs=True)
150153

151154
def to_json(self, *, with_parent_attrs: bool = False, **kwargs: Any) -> str:
@@ -220,11 +223,11 @@ def _create_managers(self) -> None:
220223
# Since we have our own __setattr__ method, we can't use setattr()
221224
self.__dict__[attr] = manager
222225

223-
def _update_attrs(self, new_attrs: Dict[str, Any]) -> None:
226+
def _update_attrs(self, new_attrs: dict[str, Any]) -> None:
224227
self.__dict__["_updated_attrs"] = {}
225228
self.__dict__["_attrs"] = new_attrs
226229

227-
def get_id(self) -> Optional[Union[int, str]]:
230+
def get_id(self) -> int | str | None:
228231
"""Returns the id of the resource."""
229232
if self._id_attr is None or not hasattr(self, self._id_attr):
230233
return None
@@ -234,7 +237,7 @@ def get_id(self) -> Optional[Union[int, str]]:
234237
return id_val
235238

236239
@property
237-
def _repr_value(self) -> Optional[str]:
240+
def _repr_value(self) -> str | None:
238241
"""Safely returns the human-readable resource name if present."""
239242
if self._repr_attr is None or not hasattr(self, self._repr_attr):
240243
return None
@@ -244,7 +247,7 @@ def _repr_value(self) -> Optional[str]:
244247
return repr_val
245248

246249
@property
247-
def encoded_id(self) -> Optional[Union[int, str]]:
250+
def encoded_id(self) -> int | str | None:
248251
"""Ensure that the ID is url-encoded so that it can be safely used in a URL
249252
path"""
250253
obj_id = self.get_id()
@@ -269,7 +272,7 @@ class RESTObjectList:
269272
"""
270273

271274
def __init__(
272-
self, manager: "RESTManager", obj_cls: Type[RESTObject], _list: GitlabList
275+
self, manager: RESTManager, obj_cls: type[RESTObject], _list: GitlabList
273276
) -> None:
274277
"""Creates an objects list from a GitlabList.
275278
@@ -285,7 +288,7 @@ def __init__(
285288
self._obj_cls = obj_cls
286289
self._list = _list
287290

288-
def __iter__(self) -> "RESTObjectList":
291+
def __iter__(self) -> RESTObjectList:
289292
return self
290293

291294
def __len__(self) -> int:
@@ -304,33 +307,33 @@ def current_page(self) -> int:
304307
return self._list.current_page
305308

306309
@property
307-
def prev_page(self) -> Optional[int]:
310+
def prev_page(self) -> int | None:
308311
"""The previous page number.
309312
310313
If None, the current page is the first.
311314
"""
312315
return self._list.prev_page
313316

314317
@property
315-
def next_page(self) -> Optional[int]:
318+
def next_page(self) -> int | None:
316319
"""The next page number.
317320
318321
If None, the current page is the last.
319322
"""
320323
return self._list.next_page
321324

322325
@property
323-
def per_page(self) -> Optional[int]:
326+
def per_page(self) -> int | None:
324327
"""The number of items per page."""
325328
return self._list.per_page
326329

327330
@property
328-
def total_pages(self) -> Optional[int]:
331+
def total_pages(self) -> int | None:
329332
"""The total number of pages."""
330333
return self._list.total_pages
331334

332335
@property
333-
def total(self) -> Optional[int]:
336+
def total(self) -> int | None:
334337
"""The total number of items."""
335338
return self._list.total
336339

@@ -346,17 +349,17 @@ class RESTManager:
346349

347350
_create_attrs: g_types.RequiredOptional = g_types.RequiredOptional()
348351
_update_attrs: g_types.RequiredOptional = g_types.RequiredOptional()
349-
_path: Optional[str] = None
350-
_obj_cls: Optional[Type[RESTObject]] = None
351-
_from_parent_attrs: Dict[str, Any] = {}
352-
_types: Dict[str, Type[g_types.GitlabAttribute]] = {}
353-
354-
_computed_path: Optional[str]
355-
_parent: Optional[RESTObject]
356-
_parent_attrs: Dict[str, Any]
352+
_path: str | None = None
353+
_obj_cls: type[RESTObject] | None = None
354+
_from_parent_attrs: dict[str, Any] = {}
355+
_types: dict[str, type[g_types.GitlabAttribute]] = {}
356+
357+
_computed_path: str | None
358+
_parent: RESTObject | None
359+
_parent_attrs: dict[str, Any]
357360
gitlab: Gitlab
358361

359-
def __init__(self, gl: Gitlab, parent: Optional[RESTObject] = None) -> None:
362+
def __init__(self, gl: Gitlab, parent: RESTObject | None = None) -> None:
360363
"""REST manager constructor.
361364
362365
Args:
@@ -368,10 +371,10 @@ def __init__(self, gl: Gitlab, parent: Optional[RESTObject] = None) -> None:
368371
self._computed_path = self._compute_path()
369372

370373
@property
371-
def parent_attrs(self) -> Optional[Dict[str, Any]]:
374+
def parent_attrs(self) -> dict[str, Any] | None:
372375
return self._parent_attrs
373376

374-
def _compute_path(self, path: Optional[str] = None) -> Optional[str]:
377+
def _compute_path(self, path: str | None = None) -> str | None:
375378
self._parent_attrs = {}
376379
if path is None:
377380
path = self._path
@@ -380,7 +383,7 @@ def _compute_path(self, path: Optional[str] = None) -> Optional[str]:
380383
if self._parent is None or not self._from_parent_attrs:
381384
return path
382385

383-
data: Dict[str, Optional[gitlab.utils.EncodedId]] = {}
386+
data: dict[str, gitlab.utils.EncodedId | None] = {}
384387
for self_attr, parent_attr in self._from_parent_attrs.items():
385388
if not hasattr(self._parent, parent_attr):
386389
data[self_attr] = None
@@ -390,5 +393,5 @@ def _compute_path(self, path: Optional[str] = None) -> Optional[str]:
390393
return path.format(**data)
391394

392395
@property
393-
def path(self) -> Optional[str]:
396+
def path(self) -> str | None:
394397
return self._computed_path

0 commit comments

Comments
 (0)