refactor: use more python3.9 syntax · python-gitlab/python-gitlab@4e90c11

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

)

19172018

import gitlab

@@ -51,20 +49,20 @@ class RESTObject:

5149

object's ``__repr__()`` method.

5250

"""

535154-

_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]

63616462

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()

908891-

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

969497-

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

149147150-

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

157155158156

@property

159-

def attributes(self) -> Dict[str, Any]:

157+

def attributes(self) -> dict[str, Any]:

160158

return self.asdict(with_parent_attrs=True)

161159162160

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

233231234-

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

237235238-

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

246244247245

@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

256254257255

@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

"""

281279282280

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

298296299-

def __iter__(self) -> "RESTObjectList":

297+

def __iter__(self) -> RESTObjectList:

300298

return self

301299302300

def __len__(self) -> int:

@@ -315,33 +313,33 @@ def current_page(self) -> int:

315313

return self._list.current_page

316314317315

@property

318-

def prev_page(self) -> Optional[int]:

316+

def prev_page(self) -> int | None:

319317

"""The previous page number.

320318321319

If None, the current page is the first.

322320

"""

323321

return self._list.prev_page

324322325323

@property

326-

def next_page(self) -> Optional[int]:

324+

def next_page(self) -> int | None:

327325

"""The next page number.

328326329327

If None, the current page is the last.

330328

"""

331329

return self._list.next_page

332330333331

@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

337335338336

@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

342340343341

@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]] = {}

367365368366

_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

372370373-

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.

375373376374

Args:

@@ -382,17 +380,17 @@ def __init__(self, gl: Gitlab, parent: Optional[RESTObject] = None) -> None:

382380

self._computed_path = self._compute_path()

383381384382

@property

385-

def parent_attrs(self) -> Optional[Dict[str, Any]]:

383+

def parent_attrs(self) -> dict[str, Any] | None:

386384

return self._parent_attrs

387385388-

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

394392395-

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