Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit e24a600

Browse files
Revert "chore: some type updates (#473)"
This reverts commit 98a9a16.
1 parent d5d7c20 commit e24a600

File tree

5 files changed

+50
-51
lines changed

5 files changed

+50
-51
lines changed

shared/helpers/cache.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def args_indexes_to_log(self) -> List[int]:
142142
return self.get("args_indexes_to_log", [])
143143

144144
@property
145-
def kwargs_keys_to_log(self) -> List[str]:
145+
def kwargs_keys_to_log(self) -> List[Any]:
146146
"""List of args from the function to be logged (if present)"""
147147
return self.get("kwargs_keys_to_log", [])
148148

shared/torngit/base.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
from enum import Enum
3+
from typing import Dict, List, Optional, Tuple
34

45
import httpx
56

@@ -12,6 +13,7 @@
1213
OnRefreshCallback,
1314
Token,
1415
)
16+
from shared.typings.torngit import TorngitInstanceData
1517

1618
get_start_of_line = re.compile(r"@@ \-(\d+),?(\d*) \+(\d+),?(\d*).*").match
1719

@@ -26,67 +28,66 @@ class TokenType(Enum):
2628
pull = "pull"
2729

2830

29-
TokenTypeMapping = dict[TokenType, Token]
31+
TokenTypeMapping = Dict[TokenType, Token]
3032

3133

3234
class TorngitBaseAdapter(object):
3335
_repo_url: str | None = None
3436
_aws_key = None
35-
_oauth: OauthConsumerToken | None
36-
_on_token_refresh: OnRefreshCallback | None
37+
_oauth: OauthConsumerToken | None = None
38+
_on_token_refresh: OnRefreshCallback = None
3739
_token: Token | None = None
38-
verify_ssl: str | bool
39-
_timeout: httpx.Timeout
40-
valid_languages = set(language[0] for language in Repository.Languages.choices)
40+
verify_ssl = None
4141

42-
# These are set by the subclasses
43-
service_url: str | None = None
44-
service: str | None = None
45-
urls: dict[str, str] | None = None
42+
valid_languages = set(language.value for language in Repository.Languages)
4643

4744
def __init__(
4845
self,
4946
oauth_consumer_token: OauthConsumerToken | None = None,
50-
timeout: httpx.Timeout | tuple[int, int] | None = None,
47+
timeouts=None,
5148
token: Token | None = None,
5249
token_type_mapping: TokenTypeMapping | None = None,
53-
on_token_refresh: OnRefreshCallback | None = None,
54-
verify_ssl: str | bool = True,
50+
on_token_refresh: OnRefreshCallback = None,
51+
verify_ssl=None,
5552
**kwargs,
56-
) -> None:
57-
if isinstance(timeout, tuple):
58-
self._timeout = httpx.Timeout(timeout[1], connect=timeout[0])
59-
else:
60-
self._timeout = timeout or httpx.Timeout(30, connect=10)
53+
):
54+
self._timeouts = timeouts or [10, 30]
6155
self._token = token
6256
self._on_token_refresh = on_token_refresh
6357
self._token_type_mapping = token_type_mapping or {}
6458
self._oauth = oauth_consumer_token
65-
self.verify_ssl = verify_ssl
66-
self.data = {
67-
"owner": kwargs.get("owner", {}),
68-
"repo": kwargs.get("repo", {}),
69-
"fallback_installations": kwargs.get("fallback_installations", None),
70-
"installation": kwargs.get("installation", None),
71-
"additional_data": kwargs.get("additional_data", {}),
59+
self.data: TorngitInstanceData = {
60+
"owner": {},
61+
"repo": {},
62+
"fallback_installations": None,
63+
"installation": None,
64+
"additional_data": {},
7265
}
66+
self.verify_ssl = verify_ssl
67+
self.data.update(kwargs)
7368
# This has the side effect of initializing the torngit_cache
7469
# (if not yet initialized)
7570
torngit_cache.initialize()
7671

77-
def __repr__(self) -> str:
72+
def __repr__(self):
7873
return "<%s slug=%s ownerid=%s repoid=%s>" % (
7974
self.service,
8075
self.slug,
8176
self.data["owner"].get("ownerid"),
8277
self.data["repo"].get("repoid"),
8378
)
8479

85-
def get_client(self, timeout: httpx.Timeout | None = None) -> httpx.AsyncClient:
86-
if timeout is None:
87-
timeout = self._timeout
80+
def get_client(self, timeouts: List[int] = []) -> httpx.AsyncClient:
81+
if timeouts:
82+
timeout = httpx.Timeout(timeouts[1], connect=timeouts[0])
83+
else:
84+
timeout = httpx.Timeout(self._timeouts[1], connect=self._timeouts[0])
8885
return httpx.AsyncClient(
89-
verify=self.verify_ssl,
86+
verify=(
87+
self.verify_ssl
88+
if not isinstance(self.verify_ssl, bool)
89+
else self.verify_ssl
90+
),
9091
timeout=timeout,
9192
)
9293

@@ -95,7 +96,7 @@ def get_token_by_type(self, token_type: TokenType):
9596
return self._token_type_mapping.get(token_type)
9697
return self.token
9798

98-
def get_token_by_type_if_none(self, token: str | None, token_type: TokenType):
99+
def get_token_by_type_if_none(self, token: Optional[str], token_type: TokenType):
99100
if token is not None:
100101
return token
101102
return self.get_token_by_type(token_type)
@@ -105,15 +106,14 @@ def _oauth_consumer_token(self) -> OauthConsumerToken:
105106
raise Exception("Oauth consumer token not present")
106107
return self._oauth
107108

108-
def _validate_language(self, language: str | None) -> str | None:
109+
def _validate_language(self, language: str) -> str | None:
109110
if language:
110111
language = language.lower()
111-
112112
if language in self.valid_languages:
113113
return language
114114
return None
115115

116-
def set_token(self, token: Token) -> None:
116+
def set_token(self, token: OauthConsumerToken) -> None:
117117
self._token = token
118118

119119
@property
@@ -329,7 +329,7 @@ async def edit_webhook(
329329

330330
# OTHERS
331331

332-
async def get_authenticated(self, token=None) -> tuple[bool, bool]:
332+
async def get_authenticated(self, token=None) -> Tuple[bool, bool]:
333333
"""Finds the user permissions about about whether the user on
334334
`self.data["user"]` can access the repo from `self.data["repo"]`
335335
Returns a `can_view` and a `can_edit` permission tuple
@@ -357,7 +357,7 @@ async def get_authenticated_user(self, **kwargs):
357357
async def get_branches(self, token=None):
358358
raise NotImplementedError()
359359

360-
async def get_branch(self, branch_name: str, token=None):
360+
async def get_branch(self, token=None):
361361
raise NotImplementedError()
362362

363363
async def get_compare(
@@ -426,7 +426,7 @@ async def list_top_level_files(self, ref, token=None):
426426
async def get_workflow_run(self, run_id, token=None):
427427
raise NotImplementedError()
428428

429-
async def get_best_effort_branches(self, commit_sha: str, token=None) -> list[str]:
429+
async def get_best_effort_branches(self, commit_sha: str, token=None) -> List[str]:
430430
"""
431431
Gets a 'best effort' list of branches this commit is in.
432432
If a branch is returned, this means this commit is in that branch. If not, it could still be

shared/torngit/cache/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def is_initialized(self) -> bool:
4747
def is_enabled(self) -> bool:
4848
return self._enabled
4949

50-
def get_ttl(self, endpoint: CachedEndpoint) -> int:
50+
def get_ttl(self, endpoint: CachedEndpoint) -> dict:
5151
return self.ttls.get(endpoint, 120)
5252

5353

shared/typings/oauth_token_types.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import Awaitable, Callable, NotRequired, TypedDict
1+
from typing import Awaitable, Callable, Optional, TypedDict
22

33

44
class Token(TypedDict):
55
key: str
6-
refresh_token: NotRequired[str]
76
# This information is used to identify the token owner in the logs, if present
87
username: str | None
98
# This represents the entity it belongs to. Entities can have the form of
@@ -14,9 +13,9 @@ class Token(TypedDict):
1413
entity_name: str | None
1514

1615

17-
class OauthConsumerToken(TypedDict):
18-
key: str # client_id
19-
secret: str
16+
class OauthConsumerToken(Token):
17+
secret: Optional[str]
18+
refresh_token: Optional[str]
2019

2120

22-
OnRefreshCallback = Callable[[Token], Awaitable[None]]
21+
OnRefreshCallback = Optional[Callable[[OauthConsumerToken], Awaitable[None]]]

shared/typings/torngit.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, List, NotRequired, Optional, TypedDict
1+
from typing import Dict, List, NotRequired, Optional, TypedDict, Union
22

33
from shared.reports.types import UploadType
44

@@ -38,8 +38,8 @@ class AdditionalData(TypedDict):
3838

3939

4040
class TorngitInstanceData(TypedDict):
41-
owner: OwnerInfo | Dict
42-
repo: RepoInfo | Dict
43-
fallback_installations: List[GithubInstallationInfo | None] | None
44-
installation: GithubInstallationInfo | None
45-
additional_data: AdditionalData | None
41+
owner: Union[OwnerInfo, Dict]
42+
repo: Union[RepoInfo, Dict]
43+
fallback_installations: List[Optional[GithubInstallationInfo]] | None
44+
installation: Optional[GithubInstallationInfo]
45+
additional_data: Optional[AdditionalData]

0 commit comments

Comments
 (0)