1
+ from __future__ import annotations
2
+
1
3
import copy
2
4
import importlib
3
5
import json
4
6
import pprint
5
7
import textwrap
8
+ from collections .abc import Iterable
6
9
from types import ModuleType
7
- from typing import Any , Dict , Iterable , Optional , Type , TYPE_CHECKING , Union
10
+ from typing import Any , TYPE_CHECKING
8
11
9
12
import gitlab
10
13
from gitlab import types as g_types
@@ -40,20 +43,20 @@ class RESTObject:
40
43
object's ``__repr__()`` method.
41
44
"""
42
45
43
- _id_attr : Optional [ str ] = "id"
44
- _attrs : Dict [str , Any ]
46
+ _id_attr : str | None = "id"
47
+ _attrs : dict [str , Any ]
45
48
_created_from_list : bool # Indicates if object was created from a list() action
46
49
_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 ]
50
53
_lazy : bool
51
- manager : " RESTManager"
54
+ manager : RESTManager
52
55
53
56
def __init__ (
54
57
self ,
55
- manager : " RESTManager" ,
56
- attrs : Dict [str , Any ],
58
+ manager : RESTManager ,
59
+ attrs : dict [str , Any ],
57
60
* ,
58
61
created_from_list : bool = False ,
59
62
lazy : bool = False ,
@@ -77,13 +80,13 @@ def __init__(
77
80
self .__dict__ ["_parent_attrs" ] = self .manager .parent_attrs
78
81
self ._create_managers ()
79
82
80
- def __getstate__ (self ) -> Dict [str , Any ]:
83
+ def __getstate__ (self ) -> dict [str , Any ]:
81
84
state = self .__dict__ .copy ()
82
85
module = state .pop ("_module" )
83
86
state ["_module_name" ] = module .__name__
84
87
return state
85
88
86
- def __setstate__ (self , state : Dict [str , Any ]) -> None :
89
+ def __setstate__ (self , state : dict [str , Any ]) -> None :
87
90
module_name = state .pop ("_module_name" )
88
91
self .__dict__ .update (state )
89
92
self .__dict__ ["_module" ] = importlib .import_module (module_name )
@@ -136,7 +139,7 @@ def __getattr__(self, name: str) -> Any:
136
139
def __setattr__ (self , name : str , value : Any ) -> None :
137
140
self .__dict__ ["_updated_attrs" ][name ] = value
138
141
139
- def asdict (self , * , with_parent_attrs : bool = False ) -> Dict [str , Any ]:
142
+ def asdict (self , * , with_parent_attrs : bool = False ) -> dict [str , Any ]:
140
143
data = {}
141
144
if with_parent_attrs :
142
145
data .update (copy .deepcopy (self ._parent_attrs ))
@@ -145,7 +148,7 @@ def asdict(self, *, with_parent_attrs: bool = False) -> Dict[str, Any]:
145
148
return data
146
149
147
150
@property
148
- def attributes (self ) -> Dict [str , Any ]:
151
+ def attributes (self ) -> dict [str , Any ]:
149
152
return self .asdict (with_parent_attrs = True )
150
153
151
154
def to_json (self , * , with_parent_attrs : bool = False , ** kwargs : Any ) -> str :
@@ -220,11 +223,11 @@ def _create_managers(self) -> None:
220
223
# Since we have our own __setattr__ method, we can't use setattr()
221
224
self .__dict__ [attr ] = manager
222
225
223
- def _update_attrs (self , new_attrs : Dict [str , Any ]) -> None :
226
+ def _update_attrs (self , new_attrs : dict [str , Any ]) -> None :
224
227
self .__dict__ ["_updated_attrs" ] = {}
225
228
self .__dict__ ["_attrs" ] = new_attrs
226
229
227
- def get_id (self ) -> Optional [ Union [ int , str ]] :
230
+ def get_id (self ) -> int | str | None :
228
231
"""Returns the id of the resource."""
229
232
if self ._id_attr is None or not hasattr (self , self ._id_attr ):
230
233
return None
@@ -234,7 +237,7 @@ def get_id(self) -> Optional[Union[int, str]]:
234
237
return id_val
235
238
236
239
@property
237
- def _repr_value (self ) -> Optional [ str ] :
240
+ def _repr_value (self ) -> str | None :
238
241
"""Safely returns the human-readable resource name if present."""
239
242
if self ._repr_attr is None or not hasattr (self , self ._repr_attr ):
240
243
return None
@@ -244,7 +247,7 @@ def _repr_value(self) -> Optional[str]:
244
247
return repr_val
245
248
246
249
@property
247
- def encoded_id (self ) -> Optional [ Union [ int , str ]] :
250
+ def encoded_id (self ) -> int | str | None :
248
251
"""Ensure that the ID is url-encoded so that it can be safely used in a URL
249
252
path"""
250
253
obj_id = self .get_id ()
@@ -269,7 +272,7 @@ class RESTObjectList:
269
272
"""
270
273
271
274
def __init__ (
272
- self , manager : " RESTManager" , obj_cls : Type [RESTObject ], _list : GitlabList
275
+ self , manager : RESTManager , obj_cls : type [RESTObject ], _list : GitlabList
273
276
) -> None :
274
277
"""Creates an objects list from a GitlabList.
275
278
@@ -285,7 +288,7 @@ def __init__(
285
288
self ._obj_cls = obj_cls
286
289
self ._list = _list
287
290
288
- def __iter__ (self ) -> " RESTObjectList" :
291
+ def __iter__ (self ) -> RESTObjectList :
289
292
return self
290
293
291
294
def __len__ (self ) -> int :
@@ -304,33 +307,33 @@ def current_page(self) -> int:
304
307
return self ._list .current_page
305
308
306
309
@property
307
- def prev_page (self ) -> Optional [ int ] :
310
+ def prev_page (self ) -> int | None :
308
311
"""The previous page number.
309
312
310
313
If None, the current page is the first.
311
314
"""
312
315
return self ._list .prev_page
313
316
314
317
@property
315
- def next_page (self ) -> Optional [ int ] :
318
+ def next_page (self ) -> int | None :
316
319
"""The next page number.
317
320
318
321
If None, the current page is the last.
319
322
"""
320
323
return self ._list .next_page
321
324
322
325
@property
323
- def per_page (self ) -> Optional [ int ] :
326
+ def per_page (self ) -> int | None :
324
327
"""The number of items per page."""
325
328
return self ._list .per_page
326
329
327
330
@property
328
- def total_pages (self ) -> Optional [ int ] :
331
+ def total_pages (self ) -> int | None :
329
332
"""The total number of pages."""
330
333
return self ._list .total_pages
331
334
332
335
@property
333
- def total (self ) -> Optional [ int ] :
336
+ def total (self ) -> int | None :
334
337
"""The total number of items."""
335
338
return self ._list .total
336
339
@@ -346,17 +349,17 @@ class RESTManager:
346
349
347
350
_create_attrs : g_types .RequiredOptional = g_types .RequiredOptional ()
348
351
_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 ]
357
360
gitlab : Gitlab
358
361
359
- def __init__ (self , gl : Gitlab , parent : Optional [ RESTObject ] = None ) -> None :
362
+ def __init__ (self , gl : Gitlab , parent : RESTObject | None = None ) -> None :
360
363
"""REST manager constructor.
361
364
362
365
Args:
@@ -368,10 +371,10 @@ def __init__(self, gl: Gitlab, parent: Optional[RESTObject] = None) -> None:
368
371
self ._computed_path = self ._compute_path ()
369
372
370
373
@property
371
- def parent_attrs (self ) -> Optional [ Dict [ str , Any ]] :
374
+ def parent_attrs (self ) -> dict [ str , Any ] | None :
372
375
return self ._parent_attrs
373
376
374
- def _compute_path (self , path : Optional [ str ] = None ) -> Optional [ str ] :
377
+ def _compute_path (self , path : str | None = None ) -> str | None :
375
378
self ._parent_attrs = {}
376
379
if path is None :
377
380
path = self ._path
@@ -380,7 +383,7 @@ def _compute_path(self, path: Optional[str] = None) -> Optional[str]:
380
383
if self ._parent is None or not self ._from_parent_attrs :
381
384
return path
382
385
383
- data : Dict [str , Optional [ gitlab .utils .EncodedId ] ] = {}
386
+ data : dict [str , gitlab .utils .EncodedId | None ] = {}
384
387
for self_attr , parent_attr in self ._from_parent_attrs .items ():
385
388
if not hasattr (self ._parent , parent_attr ):
386
389
data [self_attr ] = None
@@ -390,5 +393,5 @@ def _compute_path(self, path: Optional[str] = None) -> Optional[str]:
390
393
return path .format (** data )
391
394
392
395
@property
393
- def path (self ) -> Optional [ str ] :
396
+ def path (self ) -> str | None :
394
397
return self ._computed_path
0 commit comments