Skip to content

Commit 59a5f58

Browse files
fix: add types to default and verify_token and Request __init__ based on comments in the source code. (#1588)
Recently, I've been using this library with mypy strict mode, which doesn't like the fact that these functions are unannotated (they're the only functions I use, and thus the only ones mypy complains about to me). I'm happy to see that this project has py.typed, so these annotations should fix the problem!
1 parent e2135fb commit 59a5f58

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

google/auth/_default.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,22 @@
1717
Implements application default credentials and project ID detection.
1818
"""
1919

20+
from collections.abc import Sequence
2021
import io
2122
import json
2223
import logging
2324
import os
25+
from typing import Optional, TYPE_CHECKING
2426
import warnings
2527

2628
from google.auth import environment_vars
2729
from google.auth import exceptions
2830
import google.auth.transport._http_client
2931

32+
if TYPE_CHECKING: # pragma: NO COVER
33+
from google.auth.credentials import Credentials # noqa: F401
34+
from google.auth.transport import Request # noqa: F401
35+
3036
_LOGGER = logging.getLogger(__name__)
3137

3238
# Valid types accepted for file-based credentials.
@@ -588,7 +594,12 @@ def _apply_quota_project_id(credentials, quota_project_id):
588594
return credentials
589595

590596

591-
def default(scopes=None, request=None, quota_project_id=None, default_scopes=None):
597+
def default(
598+
scopes: Optional[Sequence[str]] = None,
599+
request: Optional["google.auth.transport.Request"] = None,
600+
quota_project_id: Optional[str] = None,
601+
default_scopes: Optional[Sequence[str]] = None,
602+
) -> tuple["google.auth.credentials.Credentials", Optional[str]]:
592603
"""Gets the default credentials for the current environment.
593604
594605
`Application Default Credentials`_ provides an easy way to obtain

google/auth/transport/requests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import logging
2222
import numbers
2323
import time
24+
from typing import Optional
2425

2526
try:
2627
import requests
@@ -137,7 +138,7 @@ class Request(transport.Request):
137138
.. automethod:: __call__
138139
"""
139140

140-
def __init__(self, session=None):
141+
def __init__(self, session: Optional[requests.Session] = None) -> None:
141142
if not session:
142143
session = requests.Session()
143144

google/oauth2/id_token.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@
5858
import http.client as http_client
5959
import json
6060
import os
61+
from typing import Any, Mapping, Union
6162

6263
from google.auth import environment_vars
6364
from google.auth import exceptions
6465
from google.auth import jwt
66+
from google.auth import transport
6567

6668

6769
# The URL that provides public certificates for verifying ID tokens issued
@@ -105,12 +107,12 @@ def _fetch_certs(request, certs_url):
105107

106108

107109
def verify_token(
108-
id_token,
109-
request,
110-
audience=None,
111-
certs_url=_GOOGLE_OAUTH2_CERTS_URL,
112-
clock_skew_in_seconds=0,
113-
):
110+
id_token: Union[str, bytes],
111+
request: transport.Request,
112+
audience: Union[str, list[str], None] = None,
113+
certs_url: str = _GOOGLE_OAUTH2_CERTS_URL,
114+
clock_skew_in_seconds: int = 0,
115+
) -> Mapping[str, Any]:
114116
"""Verifies an ID token and returns the decoded token.
115117
116118
Args:

0 commit comments

Comments
 (0)