refactor: use more python3.9 syntax · python-gitlab/python-gitlab@4e90c11
1+from __future__ import annotations
2+13import copy
24import importlib
35import json
46import pprint
57import textwrap
8+from collections.abc import Iterable
69from types import ModuleType
710from typing import (
811Any,
912ClassVar,
10-Dict,
1113Generic,
12-Iterable,
13-Optional,
14-Type,
1514TYPE_CHECKING,
1615TypeVar,
17-Union,
1816)
19172018import 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]
63616462def __init__(
6563self,
66-manager: "RESTManager[Any]",
67-attrs: Dict[str, Any],
64+manager: RESTManager[Any],
65+attrs: dict[str, Any],
6866*,
6967created_from_list: bool = False,
7068lazy: bool = False,
@@ -88,13 +86,13 @@ def __init__(
8886self.__dict__["_parent_attrs"] = self.manager.parent_attrs
8987self._create_managers()
908891-def __getstate__(self) -> Dict[str, Any]:
89+def __getstate__(self) -> dict[str, Any]:
9290state = self.__dict__.copy()
9391module = state.pop("_module")
9492state["_module_name"] = module.__name__
9593return state
969497-def __setstate__(self, state: Dict[str, Any]) -> None:
95+def __setstate__(self, state: dict[str, Any]) -> None:
9896module_name = state.pop("_module_name")
9997self.__dict__.update(state)
10098self.__dict__["_module"] = importlib.import_module(module_name)
@@ -147,7 +145,7 @@ def __getattr__(self, name: str) -> Any:
147145def __setattr__(self, name: str, value: Any) -> None:
148146self.__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]:
151149data = {}
152150if with_parent_attrs:
153151data.update(copy.deepcopy(self._parent_attrs))
@@ -156,7 +154,7 @@ def asdict(self, *, with_parent_attrs: bool = False) -> Dict[str, Any]:
156154return data
157155158156@property
159-def attributes(self) -> Dict[str, Any]:
157+def attributes(self) -> dict[str, Any]:
160158return self.asdict(with_parent_attrs=True)
161159162160def 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()
232230self.__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:
235233self.__dict__["_updated_attrs"] = {}
236234self.__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."""
240238if self._id_attr is None or not hasattr(self, self._id_attr):
241239return None
@@ -245,7 +243,7 @@ def get_id(self) -> Optional[Union[int, str]]:
245243return 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."""
250248if self._repr_attr is None or not hasattr(self, self._repr_attr):
251249return None
@@ -255,7 +253,7 @@ def _repr_value(self) -> Optional[str]:
255253return 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"""
261259obj_id = self.get_id()
@@ -280,7 +278,7 @@ class RESTObjectList:
280278 """
281279282280def __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__(
296294self._obj_cls = obj_cls
297295self._list = _list
298296299-def __iter__(self) -> "RESTObjectList":
297+def __iter__(self) -> RESTObjectList:
300298return self
301299302300def __len__(self) -> int:
@@ -315,33 +313,33 @@ def current_page(self) -> int:
315313return 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 """
323321return 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 """
331329return 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."""
336334return 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."""
341339return self._list.total_pages
342340343341@property
344-def total(self) -> Optional[int]:
342+def total(self) -> int | None:
345343"""The total number of items."""
346344return 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]
371369gitlab: 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:
382380self._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:
386384return self._parent_attrs
387385388-def _compute_path(self, path: Optional[str] = None) -> str:
386+def _compute_path(self, path: str | None = None) -> str:
389387self._parent_attrs = {}
390388if path is None:
391389path = self._path
392390if self._parent is None or not self._from_parent_attrs:
393391return path
394392395-data: Dict[str, Optional[gitlab.utils.EncodedId]] = {}
393+data: dict[str, gitlab.utils.EncodedId | None] = {}
396394for self_attr, parent_attr in self._from_parent_attrs.items():
397395if not hasattr(self._parent, parent_attr):
398396data[self_attr] = None