Skip to content

Commit f4a3931

Browse files
committed
fix lint and some types
1 parent ece43fc commit f4a3931

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

firebase_admin/_http_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from __future__ import annotations
2121
import logging
22-
from typing import Any, Dict, Optional, Tuple, Union
22+
from typing import Any, Dict, Generator, Optional, Tuple, Union
2323
import httpx
2424
import requests.adapters
2525
from requests.packages.urllib3.util import retry # pylint: disable=import-error
@@ -175,7 +175,10 @@ def __init__(self, credential: credentials.Credentials):
175175
self._refresh_status_codes = (401,)
176176

177177
def apply_auth_headers(
178-
self, request: httpx.Request, auth_request: google_auth_requests.Request):
178+
self,
179+
request: httpx.Request,
180+
auth_request: google_auth_requests.Request
181+
) -> None:
179182
"""A helper function to refreshes credentials if needed and mutates the request headers to
180183
contain access token and any other google auth headers."""
181184

@@ -188,7 +191,7 @@ def apply_auth_headers(
188191
)
189192
logger.debug('Auth headers applied. Credential validity after: %s', self._credential.valid)
190193

191-
def auth_flow(self, request: httpx.Request):
194+
def auth_flow(self, request: httpx.Request) -> Generator[httpx.Request, httpx.Response, None]:
192195
_original_headers = request.headers.copy()
193196
_credential_refresh_attempt = 0
194197

firebase_admin/_retry.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@
2323
import random
2424
import re
2525
import time
26-
from types import CoroutineType
27-
from typing import Any, Callable, List, Optional, Tuple
26+
from typing import Any, Callable, List, Optional, Tuple, Coroutine
2827
import logging
2928
import asyncio
3029
import httpx
@@ -163,7 +162,7 @@ class HttpxRetryTransport(httpx.AsyncBaseTransport):
163162

164163
DEFAULT_RETRY = HttpxRetry(max_retries=4, status_forcelist=[500, 503], backoff_factor=0.5)
165164

166-
def __init__(self, retry: HttpxRetry = DEFAULT_RETRY, **kwargs) -> None:
165+
def __init__(self, retry: HttpxRetry = DEFAULT_RETRY, **kwargs: Any) -> None:
167166
self._retry = retry
168167

169168
transport_kwargs = kwargs.copy()
@@ -180,7 +179,7 @@ async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
180179
async def _dispatch_with_retry(
181180
self,
182181
request: httpx.Request,
183-
dispatch_method: Callable[[httpx.Request], CoroutineType[Any, Any, httpx.Response]]
182+
dispatch_method: Callable[[httpx.Request], Coroutine[Any, Any, httpx.Response]]
184183
) -> httpx.Response:
185184
"""Sends a request with retry logic using a provided dispatch method."""
186185
# This request config is used across all requests that use this transport and therefore

firebase_admin/messaging.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"""Firebase Cloud Messaging module."""
1616

1717
from __future__ import annotations
18-
from typing import Callable, List, Optional
18+
from typing import Any, Callable, Dict, List, Optional, cast
1919
import concurrent.futures
2020
import json
2121
import warnings
@@ -37,7 +37,6 @@
3737
exceptions,
3838
App
3939
)
40-
from firebase_admin._retry import HttpxRetryTransport
4140

4241
logger = logging.getLogger(__name__)
4342

@@ -111,7 +110,7 @@
111110
def _get_messaging_service(app: Optional[App]) -> _MessagingService:
112111
return _utils.get_app_service(app, _MESSAGING_ATTRIBUTE, _MessagingService)
113112

114-
def send(message, dry_run=False, app: Optional[App] = None):
113+
def send(message: Message, dry_run: bool = False, app: Optional[App] = None) -> str:
115114
"""Sends the given message via Firebase Cloud Messaging (FCM).
116115
117116
If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
@@ -131,7 +130,11 @@ def send(message, dry_run=False, app: Optional[App] = None):
131130
"""
132131
return _get_messaging_service(app).send(message, dry_run)
133132

134-
def send_each(messages, dry_run=False, app=None):
133+
def send_each(
134+
messages: List[Message],
135+
dry_run: bool = False,
136+
app: Optional[App] = None
137+
) -> BatchResponse:
135138
"""Sends each message in the given list via Firebase Cloud Messaging.
136139
137140
If the ``dry_run`` mode is enabled, the message will not be actually delivered to the
@@ -451,7 +454,7 @@ class _MessagingService:
451454
'UNREGISTERED': UnregisteredError,
452455
}
453456

454-
def __init__(self, app) -> None:
457+
def __init__(self, app: App) -> None:
455458
project_id = app.project_id
456459
if not project_id:
457460
raise ValueError(
@@ -476,7 +479,7 @@ def encode_message(cls, message):
476479
raise ValueError('Message must be an instance of messaging.Message class.')
477480
return cls.JSON_ENCODER.default(message)
478481

479-
def send(self, message, dry_run=False):
482+
def send(self, message: Message, dry_run: bool = False) -> str:
480483
"""Sends the given message to FCM via the FCM v1 API."""
481484
data = self._message_data(message, dry_run)
482485
try:
@@ -489,9 +492,9 @@ def send(self, message, dry_run=False):
489492
except requests.exceptions.RequestException as error:
490493
raise self._handle_fcm_error(error)
491494
else:
492-
return resp['name']
495+
return cast(str, resp['name'])
493496

494-
def send_each(self, messages, dry_run=False):
497+
def send_each(self, messages: List[Message], dry_run: bool = False) -> BatchResponse:
495498
"""Sends the given messages to FCM via the FCM v1 API."""
496499
if not isinstance(messages, list):
497500
raise ValueError('messages must be a list of messaging.Message instances.')
@@ -683,7 +686,10 @@ def _build_fcm_error_requests(cls, error, message, error_dict):
683686

684687
@classmethod
685688
def _build_fcm_error_httpx(
686-
cls, error: httpx.HTTPError, message, error_dict
689+
cls,
690+
error: httpx.HTTPError,
691+
message: str,
692+
error_dict: Optional[Dict[str, Any]]
687693
) -> Optional[exceptions.FirebaseError]:
688694
"""Parses a httpx error response from the FCM API and creates a FCM-specific exception if
689695
appropriate."""
@@ -702,7 +708,11 @@ def _build_fcm_error_googleapiclient(cls, error, message, error_dict, http_respo
702708
return exc_type(message, cause=error, http_response=http_response) if exc_type else None
703709

704710
@classmethod
705-
def _build_fcm_error(cls, error_dict) -> Optional[Callable[..., exceptions.FirebaseError]]:
711+
def _build_fcm_error(
712+
cls,
713+
error_dict: Optional[Dict[str, Any]]
714+
) -> Optional[Callable[..., exceptions.FirebaseError]]:
715+
"""Parses an error response to determine the appropriate FCM-specific error type."""
706716
if not error_dict:
707717
return None
708718
fcm_code = None

0 commit comments

Comments
 (0)