-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add type hints to credentials #1605
base: main
Are you sure you want to change the base?
Changes from all commits
820f9ae
9bbcde7
daceef2
f34f67c
f205ccf
ece2b91
db4ff14
5ef5b44
ffdca44
c7aa52e
736e120
4991466
0c6abb0
9da2c78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,13 @@ | |
"""Interface for base credentials.""" | ||
|
||
import abc | ||
from typing import Optional | ||
|
||
from google.auth import _helpers | ||
from google.auth.transport.requests import Request | ||
|
||
|
||
class _BaseCredentials(metaclass=abc.ABCMeta): | ||
class BaseCredentials(metaclass=abc.ABCMeta): | ||
"""Base class for all credentials. | ||
|
||
All credentials have a :attr:`token` that is used for authentication and | ||
|
@@ -44,10 +46,10 @@ class _BaseCredentials(metaclass=abc.ABCMeta): | |
""" | ||
|
||
def __init__(self): | ||
self.token = None | ||
self.token: Optional[str] = None | ||
|
||
@abc.abstractmethod | ||
def refresh(self, request): | ||
def refresh(self, request: Request) -> None: | ||
"""Refreshes the access token. | ||
|
||
Args: | ||
|
@@ -62,14 +64,18 @@ def refresh(self, request): | |
# (pylint doesn't recognize that this is abstract) | ||
raise NotImplementedError("Refresh must be implemented") | ||
|
||
def _apply(self, headers, token=None): | ||
def _apply(self, headers: dict[str, str], token: Optional[str] = None): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This operation requires the type of headers["authorization"] = "Bearer {}".format(
_helpers.from_bytes(token or self.token)
) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want me to modify the doctring instead? |
||
"""Apply the token to the authentication header. | ||
|
||
Args: | ||
headers (Mapping): The HTTP request headers. | ||
headers (dict[str, str]): The HTTP request headers. | ||
token (Optional[str]): If specified, overrides the current access | ||
token. | ||
""" | ||
headers["authorization"] = "Bearer {}".format( | ||
_helpers.from_bytes(token or self.token) | ||
) | ||
if token is not None: | ||
value = token | ||
elif self.token is not None: | ||
value = self.token | ||
else: | ||
assert False, "token must be set" | ||
headers["authorization"] = "Bearer {}".format(_helpers.from_bytes(value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we want to change this to a public class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_BaseCredentials
is imported from outside the_credentials_base.py
file, so it's not private in that scope. You can see that it's captured as an error by running: