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
7- from typing import Any , Dict , Iterable , Optional , Type , TYPE_CHECKING , Union
10+ from typing import Any , TYPE_CHECKING
811
912import gitlab
1013from 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