1919from enum import Enum
2020import logging
2121import os
22- from typing import List
22+ from typing import List , Optional , Sequence , Union
2323
2424from google .auth import _helpers , environment_vars
2525from google .auth import exceptions
@@ -579,12 +579,18 @@ class Scoped(ReadOnlyScoped):
579579 """
580580
581581 @abc .abstractmethod
582- def with_scopes (self , scopes , default_scopes = None ):
582+ def with_scopes (
583+ self ,
584+ scopes : Optional [Sequence [str ]],
585+ default_scopes : Optional [Sequence [str ]] = None ,
586+ ):
583587 """Create a copy of these credentials with the specified scopes.
584588
585589 Args:
586- scopes (Sequence[str]): The list of scopes to attach to the
590+ scopes (Optional[ Sequence[str] ]): The list of scopes to attach to the
587591 current credentials.
592+ default_scopes (Optional[Sequence[str]]): Default scopes passed by a
593+ Google client library. Use 'scopes' for user-defined scopes.
588594
589595 Raises:
590596 NotImplementedError: If the credentials' scopes can not be changed.
@@ -594,7 +600,11 @@ def with_scopes(self, scopes, default_scopes=None):
594600 raise NotImplementedError ("This class does not require scoping." )
595601
596602
597- def with_scopes_if_required (credentials , scopes , default_scopes = None ):
603+ def with_scopes_if_required (
604+ credentials ,
605+ scopes : Optional [Union [str , Sequence [str ]]],
606+ default_scopes : Optional [Sequence [str ]] = None ,
607+ ):
598608 """Creates a copy of the credentials with scopes if scoping is required.
599609
600610 This helper function is useful when you do not know (or care to know) the
@@ -607,17 +617,21 @@ def with_scopes_if_required(credentials, scopes, default_scopes=None):
607617 Args:
608618 credentials (google.auth.credentials.Credentials): The credentials to
609619 scope if necessary.
610- scopes (Sequence[str]): The list of scopes to use.
611- default_scopes (Sequence[str]): Default scopes passed by a
620+ scopes (Optional[ Sequence[str] | str]): The list of scopes to use.
621+ default_scopes (Optional[ Sequence[str] ]): Default scopes passed by a
612622 Google client library. Use 'scopes' for user-defined scopes.
613623
614624 Returns:
615625 google.auth.credentials.Credentials: Either a new set of scoped
616626 credentials, or the passed in credentials instance if no scoping
617627 was required.
618628 """
629+ # wrap single-string scopes in a list
630+ scopes_seq : Optional [Sequence [str ]] = (
631+ [scopes ] if isinstance (scopes , str ) else scopes
632+ )
619633 if isinstance (credentials , Scoped ) and credentials .requires_scopes :
620- return credentials .with_scopes (scopes , default_scopes = default_scopes )
634+ return credentials .with_scopes (scopes_seq , default_scopes = default_scopes )
621635 else :
622636 return credentials
623637
0 commit comments