Skip to content

Commit

Permalink
refactor(lint): enable add-trailing-comma
Browse files Browse the repository at this point in the history
flake8-commas is deprecated
  • Loading branch information
TheKevJames committed Dec 23, 2022
1 parent 64fbd7f commit 597ca5b
Show file tree
Hide file tree
Showing 55 changed files with 2,799 additions and 1,597 deletions.
43 changes: 23 additions & 20 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ repos:
- id: double-quote-string-fixer
- id: name-tests-test
- id: requirements-txt-fixer
- repo: https://github.com/asottile/yesqa
rev: v1.4.0
hooks:
- id: yesqa
- repo: https://github.com/PyCQA/pylint
rev: v2.15.9
hooks:
Expand Down Expand Up @@ -73,6 +77,25 @@ repos:
hooks:
- id: reorder-python-imports
args: [--py26-plus]
- repo: https://github.com/asottile/add-trailing-comma
rev: v2.4.0
hooks:
- id: add-trailing-comma
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
- id: flake8
additional_dependencies:
- flake8-2020==1.7.0
- flake8-broken-line==0.6.0
- flake8-comprehensions==3.10.1
- importlib-metadata<5 # TODO: nuke once we upgrade past py3.7
args:
- --ignore=A003,E501,W503,F401,F811
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v2.0.1
hooks:
- id: autopep8
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.991
hooks:
Expand Down Expand Up @@ -151,26 +174,6 @@ repos:
- types-pkg_resources
- types-requests
files: taskqueue/
- repo: https://github.com/asottile/yesqa
rev: v1.4.0
hooks:
- id: yesqa
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: v2.0.1
hooks:
- id: autopep8
- repo: https://github.com/PyCQA/flake8
rev: 5.0.4
hooks:
- id: flake8
additional_dependencies:
- flake8-2020==1.7.0
- flake8-broken-line==0.6.0
# - flake8-commas==2.1.0 # TODO: enable me
- flake8-comprehensions==3.10.1
- importlib-metadata<5 # TODO: nuke once we upgrade past py3.7
args:
- --ignore=A003,E501,W503,F401,F811
- repo: local
hooks:
- &poetry-check
Expand Down
96 changes: 61 additions & 35 deletions auth/gcloud/aio/auth/iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,25 @@


class IamClient:
def __init__(self, service_file: Optional[Union[str, IO[AnyStr]]] = None,
session: Optional[Session] = None,
token: Optional[Token] = None) -> None:
def __init__(
self, service_file: Optional[Union[str, IO[AnyStr]]] = None,
session: Optional[Session] = None,
token: Optional[Token] = None,
) -> None:
self.session = AioSession(session)
self.token = token or Token(
service_file=service_file, scopes=SCOPES,
session=self.session.session) # type: ignore[arg-type]

if self.token.token_type not in {Type.GCE_METADATA,
Type.SERVICE_ACCOUNT}:
raise TypeError('IAM Credentials Client is only valid for use '
'with Service Accounts or GCE Metadata')
session=self.session.session, # type: ignore[arg-type]
)

if self.token.token_type not in {
Type.GCE_METADATA,
Type.SERVICE_ACCOUNT,
}:
raise TypeError(
'IAM Credentials Client is only valid for use '
'with Service Accounts or GCE Metadata',
)

async def headers(self) -> Dict[str, str]:
token = await self.token.get()
Expand All @@ -49,22 +56,28 @@ def service_account_email(self) -> Optional[str]:
return self.token.service_data.get('client_email')

# https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts.keys/get
async def get_public_key(self, key_id: Optional[str] = None,
key: Optional[str] = None,
service_account_email: Optional[str] = None,
project: Optional[str] = None,
session: Optional[Session] = None,
timeout: int = 10) -> Dict[str, str]:
service_account_email = (service_account_email
or self.service_account_email)
async def get_public_key(
self, key_id: Optional[str] = None,
key: Optional[str] = None,
service_account_email: Optional[str] = None,
project: Optional[str] = None,
session: Optional[Session] = None,
timeout: int = 10,
) -> Dict[str, str]:
service_account_email = (
service_account_email
or self.service_account_email
)
project = project or await self.token.get_project()

if not key_id and not key:
raise ValueError('get_public_key must have either key_id or key')

if not key:
key = (f'projects/{project}/serviceAccounts/'
f'{service_account_email}/keys/{key_id}')
key = (
f'projects/{project}/serviceAccounts/'
f'{service_account_email}/keys/{key_id}'
)

url = f'{API_ROOT_IAM}/{key}?publicKeyType=TYPE_X509_PEM_FILE'
headers = await self.headers()
Expand All @@ -81,13 +94,18 @@ async def list_public_keys(
self, service_account_email: Optional[str] = None,
project: Optional[str] = None,
session: Optional[Session] = None,
timeout: int = 10) -> List[Dict[str, str]]:
service_account_email = (service_account_email
or self.service_account_email)
timeout: int = 10,
) -> List[Dict[str, str]]:
service_account_email = (
service_account_email
or self.service_account_email
)
project = project or await self.token.get_project()

url = (f'{API_ROOT_IAM}/projects/{project}/'
f'serviceAccounts/{service_account_email}/keys')
url = (
f'{API_ROOT_IAM}/projects/{project}/'
f'serviceAccounts/{service_account_email}/keys'
)

headers = await self.headers()

Expand All @@ -99,16 +117,22 @@ async def list_public_keys(
return data

# https://cloud.google.com/iam/credentials/reference/rest/v1/projects.serviceAccounts/signBlob
async def sign_blob(self, payload: Optional[Union[str, bytes]],
service_account_email: Optional[str] = None,
delegates: Optional[List[str]] = None,
session: Optional[Session] = None,
timeout: int = 10) -> Dict[str, str]:
service_account_email = (service_account_email
or self.service_account_email)
async def sign_blob(
self, payload: Optional[Union[str, bytes]],
service_account_email: Optional[str] = None,
delegates: Optional[List[str]] = None,
session: Optional[Session] = None,
timeout: int = 10,
) -> Dict[str, str]:
service_account_email = (
service_account_email
or self.service_account_email
)
if not service_account_email:
raise TypeError('sign_blob must have a valid '
'service_account_email')
raise TypeError(
'sign_blob must have a valid '
'service_account_email',
)

resource_name = f'projects/-/serviceAccounts/{service_account_email}'
url = f'{API_ROOT_IAM_CREDENTIALS}/{resource_name}:signBlob'
Expand All @@ -126,8 +150,10 @@ async def sign_blob(self, payload: Optional[Union[str, bytes]],

s = AioSession(session) if session else self.session

resp = await s.post(url=url, data=json_str, headers=headers,
timeout=timeout)
resp = await s.post(
url=url, data=json_str, headers=headers,
timeout=timeout,
)
data: Dict[str, Any] = await resp.json()
return data

Expand Down
Loading

0 comments on commit 597ca5b

Please sign in to comment.