Skip to content

Commit

Permalink
fix circular import
Browse files Browse the repository at this point in the history
  • Loading branch information
jonapich committed Feb 2, 2024
1 parent fd1756b commit 16d21a7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 52 deletions.
64 changes: 33 additions & 31 deletions github/Auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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__()

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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__()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand All @@ -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:
Expand All @@ -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)
Expand Down
9 changes: 1 addition & 8 deletions github/NamedUser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions github/OrganizationDependabotAlert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
14 changes: 7 additions & 7 deletions github/SecretScanningAlert.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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"])
Expand All @@ -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
Expand Down

0 comments on commit 16d21a7

Please sign in to comment.