Skip to content

refactor: use more python3.9 syntax #3010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# python-gitlab documentation build configuration file, created by
# sphinx-quickstart on Mon Dec 8 15:17:39 2014.
Expand Down
1 change: 0 additions & 1 deletion gitlab/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
#
# Copyright (C) 2013-2019 Gauvain Pocentek, 2019-2023 python-gitlab team
#
Expand Down
22 changes: 9 additions & 13 deletions gitlab/_backends/protocol.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from __future__ import annotations

import abc
import sys
from typing import Any, Dict, Optional, Union
from typing import Any, Protocol

import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder # type: ignore

if sys.version_info >= (3, 8):
from typing import Protocol
else:
from typing_extensions import Protocol


class BackendResponse(Protocol):
@abc.abstractmethod
Expand All @@ -22,11 +18,11 @@ def http_request(
self,
method: str,
url: str,
json: Optional[Union[Dict[str, Any], bytes]],
data: Optional[Union[Dict[str, Any], MultipartEncoder]],
params: Optional[Any],
timeout: Optional[float],
verify: Optional[Union[bool, str]],
stream: Optional[bool],
json: dict[str, Any] | bytes | None,
data: dict[str, Any] | MultipartEncoder | None,
params: Any | None,
timeout: float | None,
verify: bool | str | None,
stream: bool | None,
**kwargs: Any,
) -> BackendResponse: ...
24 changes: 12 additions & 12 deletions gitlab/_backends/requests_backend.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import dataclasses
from typing import Any, BinaryIO, Dict, Optional, TYPE_CHECKING, Union
from typing import Any, BinaryIO, TYPE_CHECKING

import requests
from requests import PreparedRequest
Expand Down Expand Up @@ -44,8 +44,8 @@ def __call__(self, r: PreparedRequest) -> PreparedRequest:
@dataclasses.dataclass
class SendData:
content_type: str
data: Optional[Union[Dict[str, Any], MultipartEncoder]] = None
json: Optional[Union[Dict[str, Any], bytes]] = None
data: dict[str, Any] | MultipartEncoder | None = None
json: dict[str, Any] | bytes | None = None

def __post_init__(self) -> None:
if self.json is not None and self.data is not None:
Expand Down Expand Up @@ -84,7 +84,7 @@ def json(self) -> Any:


class RequestsBackend(protocol.Backend):
def __init__(self, session: Optional[requests.Session] = None) -> None:
def __init__(self, session: requests.Session | None = None) -> None:
self._client: requests.Session = session or requests.Session()

@property
Expand All @@ -93,8 +93,8 @@ def client(self) -> requests.Session:

@staticmethod
def prepare_send_data(
files: Optional[Dict[str, Any]] = None,
post_data: Optional[Union[Dict[str, Any], bytes, BinaryIO]] = None,
files: dict[str, Any] | None = None,
post_data: dict[str, Any] | bytes | BinaryIO | None = None,
raw: bool = False,
) -> SendData:
if files:
Expand Down Expand Up @@ -130,12 +130,12 @@ def http_request(
self,
method: str,
url: str,
json: Optional[Union[Dict[str, Any], bytes]] = None,
data: Optional[Union[Dict[str, Any], MultipartEncoder]] = None,
params: Optional[Any] = None,
timeout: Optional[float] = None,
verify: Optional[Union[bool, str]] = True,
stream: Optional[bool] = False,
json: dict[str, Any] | bytes | None = None,
data: dict[str, Any] | MultipartEncoder | None = None,
params: Any | None = None,
timeout: float | None = None,
verify: bool | str | None = True,
stream: bool | None = False,
**kwargs: Any,
) -> RequestsResponse:
"""Make HTTP request
Expand Down
70 changes: 34 additions & 36 deletions gitlab/base.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
from __future__ import annotations

import copy
import importlib
import json
import pprint
import textwrap
from collections.abc import Iterable
from types import ModuleType
from typing import (
Any,
ClassVar,
Dict,
Generic,
Iterable,
Optional,
Type,
TYPE_CHECKING,
TypeVar,
Union,
)

import gitlab
Expand Down Expand Up @@ -51,20 +49,20 @@ class RESTObject:
object's ``__repr__()`` method.
"""

_id_attr: Optional[str] = "id"
_attrs: Dict[str, Any]
_id_attr: str | None = "id"
_attrs: dict[str, Any]
_created_from_list: bool # Indicates if object was created from a list() action
_module: ModuleType
_parent_attrs: Dict[str, Any]
_repr_attr: Optional[str] = None
_updated_attrs: Dict[str, Any]
_parent_attrs: dict[str, Any]
_repr_attr: str | None = None
_updated_attrs: dict[str, Any]
_lazy: bool
manager: "RESTManager[Any]"
manager: RESTManager[Any]

def __init__(
self,
manager: "RESTManager[Any]",
attrs: Dict[str, Any],
manager: RESTManager[Any],
attrs: dict[str, Any],
*,
created_from_list: bool = False,
lazy: bool = False,
Expand All @@ -88,13 +86,13 @@ def __init__(
self.__dict__["_parent_attrs"] = self.manager.parent_attrs
self._create_managers()

def __getstate__(self) -> Dict[str, Any]:
def __getstate__(self) -> dict[str, Any]:
state = self.__dict__.copy()
module = state.pop("_module")
state["_module_name"] = module.__name__
return state

def __setstate__(self, state: Dict[str, Any]) -> None:
def __setstate__(self, state: dict[str, Any]) -> None:
module_name = state.pop("_module_name")
self.__dict__.update(state)
self.__dict__["_module"] = importlib.import_module(module_name)
Expand Down Expand Up @@ -147,7 +145,7 @@ def __getattr__(self, name: str) -> Any:
def __setattr__(self, name: str, value: Any) -> None:
self.__dict__["_updated_attrs"][name] = value

def asdict(self, *, with_parent_attrs: bool = False) -> Dict[str, Any]:
def asdict(self, *, with_parent_attrs: bool = False) -> dict[str, Any]:
data = {}
if with_parent_attrs:
data.update(copy.deepcopy(self._parent_attrs))
Expand All @@ -156,7 +154,7 @@ def asdict(self, *, with_parent_attrs: bool = False) -> Dict[str, Any]:
return data

@property
def attributes(self) -> Dict[str, Any]:
def attributes(self) -> dict[str, Any]:
return self.asdict(with_parent_attrs=True)

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

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

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

@property
def _repr_value(self) -> Optional[str]:
def _repr_value(self) -> str | None:
"""Safely returns the human-readable resource name if present."""
if self._repr_attr is None or not hasattr(self, self._repr_attr):
return None
Expand All @@ -255,7 +253,7 @@ def _repr_value(self) -> Optional[str]:
return repr_val

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

def __init__(
self, manager: "RESTManager[Any]", obj_cls: Type[RESTObject], _list: GitlabList
self, manager: RESTManager[Any], obj_cls: type[RESTObject], _list: GitlabList
) -> None:
"""Creates an objects list from a GitlabList.

Expand All @@ -296,7 +294,7 @@ def __init__(
self._obj_cls = obj_cls
self._list = _list

def __iter__(self) -> "RESTObjectList":
def __iter__(self) -> RESTObjectList:
return self

def __len__(self) -> int:
Expand All @@ -315,33 +313,33 @@ def current_page(self) -> int:
return self._list.current_page

@property
def prev_page(self) -> Optional[int]:
def prev_page(self) -> int | None:
"""The previous page number.

If None, the current page is the first.
"""
return self._list.prev_page

@property
def next_page(self) -> Optional[int]:
def next_page(self) -> int | None:
"""The next page number.

If None, the current page is the last.
"""
return self._list.next_page

@property
def per_page(self) -> Optional[int]:
def per_page(self) -> int | None:
"""The number of items per page."""
return self._list.per_page

@property
def total_pages(self) -> Optional[int]:
def total_pages(self) -> int | None:
"""The total number of pages."""
return self._list.total_pages

@property
def total(self) -> Optional[int]:
def total(self) -> int | None:
"""The total number of items."""
return self._list.total

Expand All @@ -362,15 +360,15 @@ class RESTManager(Generic[TObjCls]):
_update_attrs: g_types.RequiredOptional = g_types.RequiredOptional()
_path: ClassVar[str]
_obj_cls: type[TObjCls]
_from_parent_attrs: Dict[str, Any] = {}
_types: Dict[str, Type[g_types.GitlabAttribute]] = {}
_from_parent_attrs: dict[str, Any] = {}
_types: dict[str, type[g_types.GitlabAttribute]] = {}

_computed_path: str
_parent: Optional[RESTObject]
_parent_attrs: Dict[str, Any]
_parent: RESTObject | None
_parent_attrs: dict[str, Any]
gitlab: Gitlab

def __init__(self, gl: Gitlab, parent: Optional[RESTObject] = None) -> None:
def __init__(self, gl: Gitlab, parent: RESTObject | None = None) -> None:
"""REST manager constructor.

Args:
Expand All @@ -382,17 +380,17 @@ def __init__(self, gl: Gitlab, parent: Optional[RESTObject] = None) -> None:
self._computed_path = self._compute_path()

@property
def parent_attrs(self) -> Optional[Dict[str, Any]]:
def parent_attrs(self) -> dict[str, Any] | None:
return self._parent_attrs

def _compute_path(self, path: Optional[str] = None) -> str:
def _compute_path(self, path: str | None = None) -> str:
self._parent_attrs = {}
if path is None:
path = self._path
if self._parent is None or not self._from_parent_attrs:
return path

data: Dict[str, Optional[gitlab.utils.EncodedId]] = {}
data: dict[str, gitlab.utils.EncodedId | None] = {}
for self_attr, parent_attr in self._from_parent_attrs.items():
if not hasattr(self._parent, parent_attr):
data[self_attr] = None
Expand Down
Loading