diff --git a/github/Auth.py b/github/Auth.py index 5fc1e3fa78..70e829f7c4 100644 --- a/github/Auth.py +++ b/github/Auth.py @@ -23,22 +23,24 @@ # # ################################################################################ +from __future__ import annotations + import abc import base64 import time from abc import ABC from datetime import datetime, timedelta, timezone -from typing import TYPE_CHECKING, Dict, Optional, Union +from typing import TYPE_CHECKING import jwt from requests import utils from github import Consts -from github.InstallationAuthorization import InstallationAuthorization from github.Requester import Requester, WithRequester if TYPE_CHECKING: from github.GithubIntegration import GithubIntegration + from github.InstallationAuthorization import InstallationAuthorization # For App authentication, time remaining before token expiration to request a new one ACCESS_TOKEN_REFRESH_THRESHOLD_SECONDS = 20 @@ -153,7 +155,7 @@ class AppAuth(JWT): def __init__( self, - app_id: Union[int, str], + app_id: int | str, private_key: str, jwt_expiry: int = Consts.DEFAULT_JWT_EXPIRY, jwt_issued_at: int = Consts.DEFAULT_JWT_ISSUED_AT, @@ -174,7 +176,7 @@ def __init__( self._jwt_algorithm = jwt_algorithm @property - def app_id(self) -> Union[int, str]: + def app_id(self) -> int | str: return self._app_id @property @@ -188,9 +190,9 @@ def token(self) -> str: def get_installation_auth( self, installation_id: int, - token_permissions: Optional[Dict[str, str]] = None, - requester: Optional[Requester] = None, - ) -> "AppInstallationAuth": + token_permissions: dict[str, str] | None = None, + requester: Requester | None = None, + ) -> AppInstallationAuth: """ Creates a github.Auth.AppInstallationAuth instance for an installation. :param installation_id: installation id @@ -200,7 +202,7 @@ def get_installation_auth( """ return AppInstallationAuth(self, installation_id, token_permissions, requester) - def create_jwt(self, expiration: Optional[int] = None) -> str: + def create_jwt(self, expiration: int | None = None) -> str: """ Create a signed JWT https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-a-github-app @@ -247,15 +249,15 @@ class AppInstallationAuth(Auth, WithRequester["AppInstallationAuth"]): """ # used to fetch live access token when calling self.token - __integration: Optional["GithubIntegration"] = None - __installation_authorization: Optional[InstallationAuthorization] = None + __integration: GithubIntegration | None = None + __installation_authorization: InstallationAuthorization | None = None def __init__( self, app_auth: AppAuth, installation_id: int, - token_permissions: Optional[Dict[str, str]] = None, - requester: Optional[Requester] = None, + token_permissions: dict[str, str] | None = None, + requester: Requester | None = None, ): super().__init__() @@ -270,7 +272,7 @@ def __init__( if requester is not None: self.withRequester(requester) - def withRequester(self, requester: Requester) -> "AppInstallationAuth": + def withRequester(self, requester: Requester) -> AppInstallationAuth: super().withRequester(requester.withAuth(self._app_auth)) # imported here to avoid circular import @@ -281,7 +283,7 @@ def withRequester(self, requester: Requester) -> "AppInstallationAuth": return self @property - def app_id(self) -> Union[int, str]: + def app_id(self) -> int | str: return self._app_auth.app_id @property @@ -293,7 +295,7 @@ def installation_id(self) -> int: return self._installation_id @property - def token_permissions(self) -> Optional[Dict[str, str]]: + def token_permissions(self) -> dict[str, str] | None: return self._token_permissions @property @@ -330,10 +332,10 @@ class AppUserAuth(Auth, WithRequester["AppUserAuth"]): _client_secret: str _token: str _type: str - _scope: Optional[str] - _expires_at: Optional[datetime] - _refresh_token: Optional[str] - _refresh_expires_at: Optional[datetime] + _scope: str | None + _expires_at: datetime | None + _refresh_token: str | None + _refresh_expires_at: datetime | None # imported here to avoid circular import from github.ApplicationOAuth import ApplicationOAuth @@ -345,11 +347,11 @@ def __init__( client_id: str, client_secret: str, token: str, - token_type: Optional[str] = None, - expires_at: Optional[datetime] = None, - refresh_token: Optional[str] = None, - refresh_expires_at: Optional[datetime] = None, - requester: Optional[Requester] = None, + token_type: str | None = None, + expires_at: datetime | None = None, + refresh_token: str | None = None, + refresh_expires_at: datetime | None = None, + requester: Requester | None = None, ) -> None: super().__init__() @@ -395,7 +397,7 @@ def token(self) -> str: self._refresh() return self._token - def withRequester(self, requester: Requester) -> "AppUserAuth": + def withRequester(self, requester: Requester) -> AppUserAuth: super().withRequester(requester.withAuth(None)) # imported here to avoid circular import @@ -436,15 +438,15 @@ def _refresh(self) -> None: self._refresh_expires_at = token.refresh_expires_at @property - def expires_at(self) -> Optional[datetime]: + def expires_at(self) -> datetime | None: return self._expires_at @property - def refresh_token(self) -> Optional[str]: + def refresh_token(self) -> str | None: return self._refresh_token @property - def refresh_expires_at(self) -> Optional[datetime]: + def refresh_expires_at(self) -> datetime | None: return self._refresh_expires_at @@ -456,8 +458,8 @@ class NetrcAuth(HTTPBasicAuth, WithRequester["NetrcAuth"]): def __init__(self) -> None: super().__init__() - self._login: Optional[str] = None - self._password: Optional[str] = None + self._login: str | None = None + self._password: str | None = None @property def username(self) -> str: @@ -473,7 +475,7 @@ def password(self) -> str: assert self._password is not None, "Method withRequester(Requester) must be called first" return self._password - def withRequester(self, requester: Requester) -> "NetrcAuth": + def withRequester(self, requester: Requester) -> NetrcAuth: super().withRequester(requester) auth = utils.get_netrc_auth(requester.base_url, raise_errors=True) diff --git a/github/NamedUser.py b/github/NamedUser.py index 8de6a26cd1..fd5ef031af 100644 --- a/github/NamedUser.py +++ b/github/NamedUser.py @@ -56,14 +56,7 @@ from datetime import datetime from typing import TYPE_CHECKING, Any -import github.Event -import github.Gist -import github.GithubObject -import github.Organization -import github.PaginatedList -import github.Permissions -import github.Plan -import github.Repository +import github # not using this form causes a circular import cycle from github import Consts from github.GithubObject import Attribute, NotSet, Opt, is_defined from github.PaginatedList import PaginatedList diff --git a/github/OrganizationDependabotAlert.py b/github/OrganizationDependabotAlert.py index 48e5bb57af..8b5a0176c8 100644 --- a/github/OrganizationDependabotAlert.py +++ b/github/OrganizationDependabotAlert.py @@ -24,25 +24,25 @@ from typing import Any -from github.DependabotAlert import DependabotAlert +import github.DependabotAlert +import github.Repository from github.GithubObject import Attribute, NotSet -from github.Repository import Repository -class OrganizationDependabotAlert(DependabotAlert): +class OrganizationDependabotAlert(github.DependabotAlert.DependabotAlert): """ This class represents a Dependabot alert on an organization. The reference can be found here https://docs.github.com/en/rest/dependabot/alerts#list-dependabot-alerts-for-an-organization """ def _initAttributes(self) -> None: super()._initAttributes() - self._repository: Attribute[Repository] = NotSet + self._repository: Attribute[github.Repository.Repository] = NotSet @property - def repository(self) -> Repository: + def repository(self) -> github.Repository.Repository: return self._repository.value def _useAttributes(self, attributes: dict[str, Any]) -> None: super()._useAttributes(attributes) if "repository" in attributes: - self._repository = self._makeClassAttribute(Repository, attributes["repository"]) + self._repository = self._makeClassAttribute(github.Repository.Repository, attributes["repository"]) diff --git a/github/SecretScanningAlert.py b/github/SecretScanningAlert.py index 6d34f8e723..0ad4aaed1b 100644 --- a/github/SecretScanningAlert.py +++ b/github/SecretScanningAlert.py @@ -1,7 +1,7 @@ from typing import Any, Dict +import github.NamedUser from github.GithubObject import Attribute, NonCompletableGithubObject, NotSet -from github.NamedUser import NamedUser class SecretScanningAlert(NonCompletableGithubObject): @@ -57,7 +57,7 @@ def push_protection_bypassed_at(self) -> str: return self._push_protection_bypassed_at.value @property - def push_protection_bypassed_by(self) -> NamedUser: + def push_protection_bypassed_by(self) -> github.NamedUser.NamedUser: return self._push_protection_bypassed_by.value @property @@ -73,7 +73,7 @@ def resolved_at(self) -> str: return self._resolved_at.value @property - def resolved_by(self) -> NamedUser: + def resolved_by(self) -> github.NamedUser.NamedUser: return self._resolved_by.value @property @@ -107,11 +107,11 @@ def _initAttributes(self) -> None: self._number: Attribute[int] = NotSet self._push_protection_bypassed: Attribute[bool] = NotSet self._push_protection_bypassed_at: Attribute[str] = NotSet - self._push_protection_bypassed_by: Attribute[NamedUser] = NotSet + self._push_protection_bypassed_by: Attribute[github.NamedUser.NamedUser] = NotSet self._resolution: Attribute[str] = NotSet self._resolution_comment: Attribute[str] = NotSet self._resolved_at: Attribute[str] = NotSet - self._resolved_by: Attribute[NamedUser] = NotSet + self._resolved_by: Attribute[github.NamedUser.NamedUser] = NotSet self._secret: Attribute[str] = NotSet self._secret_type: Attribute[str] = NotSet self._secret_type_display_name: Attribute[str] = NotSet @@ -134,7 +134,7 @@ def _useAttributes(self, attributes: Dict[str, Any]) -> None: self._push_protection_bypassed_at = self._makeStringAttribute(attributes["push_protection_bypassed_at"]) if "push_protection_bypassed_by" in attributes: # pragma no branch self._push_protection_bypassed_by = self._makeClassAttribute( - NamedUser, attributes["push_protection_bypassed_by"] + github.NamedUser.NamedUser, attributes["push_protection_bypassed_by"] ) if "resolution" in attributes: # pragma no branch self._resolution = self._makeStringAttribute(attributes["resolution"]) @@ -143,7 +143,7 @@ def _useAttributes(self, attributes: Dict[str, Any]) -> None: if "resolved_at" in attributes: # pragma no branch self._resolved_at = self._makeStringAttribute(attributes["resolved_at"]) if "resolved_by" in attributes: # pragma no branch - self._resolved_by = self._makeClassAttribute(NamedUser, attributes["resolved_by"]) + self._resolved_by = self._makeClassAttribute(github.NamedUser.NamedUser, attributes["resolved_by"]) if "secret" in attributes: # pragma no branch self._secret = self._makeStringAttribute(attributes["secret"]) if "secret_type" in attributes: # pragma no branch