← 返回首页
refactor: use more python3.9 syntax · python-gitlab/python-gitlab@4e90c11 · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

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

Provide feedback

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

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus

Commit 4e90c11

Browse files
authored andcommitted
refactor: use more python3.9 syntax
1 parent 91c4f18 commit 4e90c11

59 files changed

Lines changed: 698 additions & 719 deletions

Some content is hidden

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

‎docs/conf.py‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32
#
43
# python-gitlab documentation build configuration file, created by
54
# sphinx-quickstart on Mon Dec 8 15:17:39 2014.

‎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: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
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
710
from typing import (
811
Any,
912
ClassVar,
10-
Dict,
1113
Generic,
12-
Iterable,
13-
Optional,
14-
Type,
1514
TYPE_CHECKING,
1615
TypeVar,
17-
Union,
1816
)
1917

2018
import gitlab
@@ -51,20 +49,20 @@ class RESTObject:
5149
object's ``__repr__()`` method.
5250
"""
5351

54-
_id_attr: Optional[str] = "id"
55-
_attrs: Dict[str, Any]
52+
_id_attr: str | None = "id"
53+
_attrs: dict[str, Any]
5654
_created_from_list: bool # Indicates if object was created from a list() action
5755
_module: ModuleType
58-
_parent_attrs: Dict[str, Any]
59-
_repr_attr: Optional[str] = None
60-
_updated_attrs: Dict[str, Any]
56+
_parent_attrs: dict[str, Any]
57+
_repr_attr: str | None = None
58+
_updated_attrs: dict[str, Any]
6159
_lazy: bool
62-
manager: "RESTManager[Any]"
60+
manager: RESTManager[Any]
6361

6462
def __init__(
6563
self,
66-
manager: "RESTManager[Any]",
67-
attrs: Dict[str, Any],
64+
manager: RESTManager[Any],
65+
attrs: dict[str, Any],
6866
*,
6967
created_from_list: bool = False,
7068
lazy: bool = False,
@@ -88,13 +86,13 @@ def __init__(
8886
self.__dict__["_parent_attrs"] = self.manager.parent_attrs
8987
self._create_managers()
9088

91-
def __getstate__(self) -> Dict[str, Any]:
89+
def __getstate__(self) -> dict[str, Any]:
9290
state = self.__dict__.copy()
9391
module = state.pop("_module")
9492
state["_module_name"] = module.__name__
9593
return state
9694

97-
def __setstate__(self, state: Dict[str, Any]) -> None:
95+
def __setstate__(self, state: dict[str, Any]) -> None:
9896
module_name = state.pop("_module_name")
9997
self.__dict__.update(state)
10098
self.__dict__["_module"] = importlib.import_module(module_name)
@@ -147,7 +145,7 @@ def __getattr__(self, name: str) -> Any:
147145
def __setattr__(self, name: str, value: Any) -> None:
148146
self.__dict__["_updated_attrs"][name] = value
149147

150-
def asdict(self, *, with_parent_attrs: bool = False) -> Dict[str, Any]:
148+
def asdict(self, *, with_parent_attrs: bool = False) -> dict[str, Any]:
151149
data = {}
152150
if with_parent_attrs:
153151
data.update(copy.deepcopy(self._parent_attrs))
@@ -156,7 +154,7 @@ def asdict(self, *, with_parent_attrs: bool = False) -> Dict[str, Any]:
156154
return data
157155

158156
@property
159-
def attributes(self) -> Dict[str, Any]:
157+
def attributes(self) -> dict[str, Any]:
160158
return self.asdict(with_parent_attrs=True)
161159

162160
def to_json(self, *, with_parent_attrs: bool = False, **kwargs: Any) -> str:
@@ -231,11 +229,11 @@ def _create_managers(self) -> None:
231229
# Since we have our own __setattr__ method, we can't use setattr()
232230
self.__dict__[attr] = manager
233231

234-
def _update_attrs(self, new_attrs: Dict[str, Any]) -> None:
232+
def _update_attrs(self, new_attrs: dict[str, Any]) -> None:
235233
self.__dict__["_updated_attrs"] = {}
236234
self.__dict__["_attrs"] = new_attrs
237235

238-
def get_id(self) -> Optional[Union[int, str]]:
236+
def get_id(self) -> int | str | None:
239237
"""Returns the id of the resource."""
240238
if self._id_attr is None or not hasattr(self, self._id_attr):
241239
return None
@@ -245,7 +243,7 @@ def get_id(self) -> Optional[Union[int, str]]:
245243
return id_val
246244

247245
@property
248-
def _repr_value(self) -> Optional[str]:
246+
def _repr_value(self) -> str | None:
249247
"""Safely returns the human-readable resource name if present."""
250248
if self._repr_attr is None or not hasattr(self, self._repr_attr):
251249
return None
@@ -255,7 +253,7 @@ def _repr_value(self) -> Optional[str]:
255253
return repr_val
256254

257255
@property
258-
def encoded_id(self) -> Optional[Union[int, str]]:
256+
def encoded_id(self) -> int | str | None:
259257
"""Ensure that the ID is url-encoded so that it can be safely used in a URL
260258
path"""
261259
obj_id = self.get_id()
@@ -280,7 +278,7 @@ class RESTObjectList:
280278
"""
281279

282280
def __init__(
283-
self, manager: "RESTManager[Any]", obj_cls: Type[RESTObject], _list: GitlabList
281+
self, manager: RESTManager[Any], obj_cls: type[RESTObject], _list: GitlabList
284282
) -> None:
285283
"""Creates an objects list from a GitlabList.
286284
@@ -296,7 +294,7 @@ def __init__(
296294
self._obj_cls = obj_cls
297295
self._list = _list
298296

299-
def __iter__(self) -> "RESTObjectList":
297+
def __iter__(self) -> RESTObjectList:
300298
return self
301299

302300
def __len__(self) -> int:
@@ -315,33 +313,33 @@ def current_page(self) -> int:
315313
return self._list.current_page
316314

317315
@property
318-
def prev_page(self) -> Optional[int]:
316+
def prev_page(self) -> int | None:
319317
"""The previous page number.
320318
321319
If None, the current page is the first.
322320
"""
323321
return self._list.prev_page
324322

325323
@property
326-
def next_page(self) -> Optional[int]:
324+
def next_page(self) -> int | None:
327325
"""The next page number.
328326
329327
If None, the current page is the last.
330328
"""
331329
return self._list.next_page
332330

333331
@property
334-
def per_page(self) -> Optional[int]:
332+
def per_page(self) -> int | None:
335333
"""The number of items per page."""
336334
return self._list.per_page
337335

338336
@property
339-
def total_pages(self) -> Optional[int]:
337+
def total_pages(self) -> int | None:
340338
"""The total number of pages."""
341339
return self._list.total_pages
342340

343341
@property
344-
def total(self) -> Optional[int]:
342+
def total(self) -> int | None:
345343
"""The total number of items."""
346344
return self._list.total
347345

@@ -362,15 +360,15 @@ class RESTManager(Generic[TObjCls]):
362360
_update_attrs: g_types.RequiredOptional = g_types.RequiredOptional()
363361
_path: ClassVar[str]
364362
_obj_cls: type[TObjCls]
365-
_from_parent_attrs: Dict[str, Any] = {}
366-
_types: Dict[str, Type[g_types.GitlabAttribute]] = {}
363+
_from_parent_attrs: dict[str, Any] = {}
364+
_types: dict[str, type[g_types.GitlabAttribute]] = {}
367365

368366
_computed_path: str
369-
_parent: Optional[RESTObject]
370-
_parent_attrs: Dict[str, Any]
367+
_parent: RESTObject | None
368+
_parent_attrs: dict[str, Any]
371369
gitlab: Gitlab
372370

373-
def __init__(self, gl: Gitlab, parent: Optional[RESTObject] = None) -> None:
371+
def __init__(self, gl: Gitlab, parent: RESTObject | None = None) -> None:
374372
"""REST manager constructor.
375373
376374
Args:
@@ -382,17 +380,17 @@ def __init__(self, gl: Gitlab, parent: Optional[RESTObject] = None) -> None:
382380
self._computed_path = self._compute_path()
383381

384382
@property
385-
def parent_attrs(self) -> Optional[Dict[str, Any]]:
383+
def parent_attrs(self) -> dict[str, Any] | None:
386384
return self._parent_attrs
387385

388-
def _compute_path(self, path: Optional[str] = None) -> str:
386+
def _compute_path(self, path: str | None = None) -> str:
389387
self._parent_attrs = {}
390388
if path is None:
391389
path = self._path
392390
if self._parent is None or not self._from_parent_attrs:
393391
return path
394392

395-
data: Dict[str, Optional[gitlab.utils.EncodedId]] = {}
393+
data: dict[str, gitlab.utils.EncodedId | None] = {}
396394
for self_attr, parent_attr in self._from_parent_attrs.items():
397395
if not hasattr(self._parent, parent_attr):
398396
data[self_attr] = None

0 commit comments

Comments
 (0)

Footer

© 2026 GitHub, Inc.