This repository was archived by the owner on Mar 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 351
fix: add type hints to credentials #1605
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
820f9ae
Update
rinarakaki 9bbcde7
Update
rinarakaki daceef2
Update
rinarakaki f34f67c
Update
rinarakaki f205ccf
Update
rinarakaki ece2b91
Update _refresh_worker.py
rinarakaki db4ff14
Update credentials.py
rinarakaki 5ef5b44
Update
rinarakaki ffdca44
Update _credentials_base.py
rinarakaki c7aa52e
Update _default.py
rinarakaki 736e120
Update
rinarakaki 4991466
Update metrics.py
rinarakaki 0c6abb0
Update _default.py
rinarakaki 9da2c78
Update _credentials_base.py
rinarakaki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): | ||
|
Contributor
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
Author
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)
)
Author
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)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
_BaseCredentialsis imported from outside the_credentials_base.pyfile, so it's not private in that scope. You can see that it's captured as an error by running: