Skip to content
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

Python - Cross Services: Flake8 Compliance #6859

Merged
merged 20 commits into from
Sep 19, 2024
2 changes: 1 addition & 1 deletion .github/workflows/lint-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

- name: Install dependencies
run: |
pip install black pylint
pip install black==23.9.1 pylint

- name: Run Black & Pylint
if: steps.changed-files.outputs.any_changed == 'true'
Expand Down
1 change: 1 addition & 0 deletions python/cross_service/apigateway_covid-19_tracker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import json
import logging
import urllib.parse

import chalice
import chalicelib.covid_data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import datetime
import os
import random

import boto3
from boto3.dynamodb.conditions import Key

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pprint
import random
import urllib.parse

import boto3
import requests

Expand Down
14 changes: 12 additions & 2 deletions python/cross_service/apigateway_covid-19_tracker/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@
import sys

# This is needed so Python can find test_tools on the path.
sys.path.append("../..")
from test_tools.fixtures.common import *
sys.path.append("../..") # noqa

from test_tools.fixtures.common import (
pytest_configure,
fixture_make_stubber,
fixture_make_unique_name,
fixture_make_bucket,
StubRunner,
stub_runner,
InputMocker,
input_mocker,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import datetime
import json
import unittest.mock
import chalice
import pytest

import app
import chalicelib
import chalice
import pytest


def test_list_states():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
import datetime

import boto3
import pytest
from boto3.dynamodb.conditions import Key
from botocore.exceptions import ClientError
import pytest

from chalicelib.covid_data import Storage


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import json
import logging
import os

import boto3
from botocore.exceptions import ClientError

Expand Down Expand Up @@ -101,7 +102,7 @@ def handle_message(table, connection_id, event_body, apig_management_client):
logger.exception("Couldn't get connections.")
status_code = 404

message = f"{user_name}: {event_body['msg']}".encode("utf-8")
ford-at-aws marked this conversation as resolved.
Show resolved Hide resolved
message = f"{user_name}: {event_body['msg']}".encode() # utf-8
logger.info("Message: %s", message)

for other_conn_id in connection_ids:
Expand Down
14 changes: 12 additions & 2 deletions python/cross_service/apigateway_websocket_chat/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@
import sys

# This is needed so Python can find test_tools on the path.
sys.path.append("../..")
from test_tools.fixtures.common import *
ford-at-aws marked this conversation as resolved.
Show resolved Hide resolved
sys.path.append("../..") # noqa

from test_tools.fixtures.common import (
pytest_configure,
fixture_make_stubber,
fixture_make_unique_name,
fixture_make_bucket,
StubRunner,
stub_runner,
InputMocker,
input_mocker,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"""

import json
import boto3
import pytest

import boto3
import lambda_chat
import pytest


@pytest.mark.parametrize(
Expand Down Expand Up @@ -87,7 +87,7 @@ def test_handle_message(
)
if error_method != "stub_scan":
apig_management_stubber.stub_post_to_connection(
f"{user_name}: {msg}".encode("utf-8"),
ford-at-aws marked this conversation as resolved.
Show resolved Hide resolved
f"{user_name}: {msg}".encode(), # utf-8
other_connection_id,
error_code=error_code
if error_method == "stub_post_to_connection"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
"""

import boto3
from botocore.exceptions import ClientError
import pytest

from botocore.exceptions import ClientError
from websocket_chat import ApiGatewayWebsocket


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
import io
import json
import logging
import websockets
import zipfile

import boto3
import websockets
from botocore.exceptions import ClientError

logger = logging.getLogger(__name__)
Expand Down
1 change: 0 additions & 1 deletion python/cross_service/aurora_item_tracker/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import boto3
from flask import Flask
from flask_cors import CORS

from item_list import ItemList
from report import Report
from storage import Storage
Expand Down
3 changes: 2 additions & 1 deletion python/cross_service/aurora_item_tracker/item_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
"""

import logging

from flask import jsonify
from flask.views import MethodView
from marshmallow import Schema
from storage import DataServiceNotReadyException, StorageError
from webargs import fields
from webargs.flaskparser import use_args, use_kwargs
from storage import DataServiceNotReadyException, StorageError

logger = logging.getLogger(__name__)

Expand Down
12 changes: 6 additions & 6 deletions python/cross_service/aurora_item_tracker/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
"""

import csv
import logging
from datetime import datetime
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import logging
from io import StringIO

from botocore.exceptions import ClientError
from flask import jsonify, render_template
from flask.views import MethodView
from storage import StorageError
from webargs import fields
from webargs.flaskparser import use_kwargs

from storage import StorageError

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -106,7 +106,7 @@ def post(self, email):
try:
work_items = self.storage.get_work_items(archived=False)
snap_time = datetime.now()
logger.info(f"Sending report of %s items to %s.", len(work_items), email)
logger.info(f"Sending report of {len(work_items)} items to {email}.")
html_report = render_template(
"report.html",
work_items=work_items,
Expand Down Expand Up @@ -134,7 +134,7 @@ def post(self, email):
Source=self.email_sender,
Destination={"ToAddresses": [email]},
Message={
"Subject": {"Data": f"Work items"},
"Subject": {"Data": "Work items"},
"Body": {
"Html": {"Data": html_report},
"Text": {"Data": text_report},
Expand Down
7 changes: 4 additions & 3 deletions python/cross_service/aurora_item_tracker/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import logging

from botocore.exceptions import ClientError

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -128,10 +129,10 @@ def add_work_item(self, work_item):
:return: The generated ID of the new work item.
"""
sql = (
f"WITH t1 AS ( "
"WITH t1 AS ( "
f"INSERT INTO {self._table_name} (description, guide, status, username) "
f" VALUES (:description, :guide, :status, :username) RETURNING iditem "
f") SELECT iditem FROM t1"
" VALUES (:description, :guide, :status, :username) RETURNING iditem "
") SELECT iditem FROM t1"
)
sql_params = [
{"name": "description", "value": {"stringValue": work_item["description"]}},
Expand Down
16 changes: 13 additions & 3 deletions python/cross_service/aurora_item_tracker/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

import sys

# This is needed so Python can find test_tools in the path.
ford-at-aws marked this conversation as resolved.
Show resolved Hide resolved
sys.path.append("../..")
from test_tools.fixtures.common import *
# This is needed so Python can find test_tools on the path.
sys.path.append("../..") # noqa

from test_tools.fixtures.common import (
pytest_configure,
fixture_make_stubber,
fixture_make_unique_name,
fixture_make_bucket,
StubRunner,
stub_runner,
InputMocker,
input_mocker,
)
19 changes: 9 additions & 10 deletions python/cross_service/aurora_item_tracker/test/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
"""

import boto3
from botocore.stub import ANY
import pytest

from app import create_app
from app import create_app # pylint: disable=E0611
from botocore.stub import ANY


class MockManager:
Expand Down Expand Up @@ -82,7 +81,7 @@ def setup_stubs(self, err, stop_on, sql, sql_params, report=None, **kwargs):
self.ses_stubber.stub_send_email,
self.sender,
{"ToAddresses": [self.recipient]},
f"Work items",
"Work items",
ANY,
ANY,
"test-msg-id",
Expand All @@ -101,22 +100,22 @@ def make_query(self, kind, data):
if kind == "SELECT":
if data is not None:
sql = (
f"SELECT iditem, description, guide, status, username, archived "
"SELECT iditem, description, guide, status, username, archived "
f"FROM {self.table_name} WHERE archived=:archived"
)
sql_params = [
{"name": "archived", "value": {"booleanValue": data == "true"}}
]
else:
sql = (
f"SELECT iditem, description, guide, status, username, archived "
"SELECT iditem, description, guide, status, username, archived "
f"FROM {self.table_name} "
)
sql_params = None
elif kind == "INSERT":
sql = (
f"INSERT INTO {self.table_name} (description, guide, status, username) "
f" VALUES (:description, :guide, :status, :username)"
" VALUES (:description, :guide, :status, :username)"
)
sql_params = [
{"name": "description", "value": {"stringValue": data["description"]}},
Expand Down Expand Up @@ -283,7 +282,7 @@ def test_report_small(mock_mgr):
mock_mgr.setup_stubs(None, None, sql, sql_params, report="small")

with mock_mgr.app.test_client() as client:
rte = f"/api/items:report"
rte = "/api/items:report"
rv = client.post(rte, json={"email": mock_mgr.recipient})
assert rv.status_code == 200

Expand All @@ -293,7 +292,7 @@ def test_report_large(mock_mgr):
mock_mgr.setup_stubs(None, None, sql, sql_params, report="large")

with mock_mgr.app.test_client() as client:
rte = f"/api/items:report"
rte = "/api/items:report"
rv = client.post(rte, json={"email": mock_mgr.recipient})
assert rv.status_code == 200

Expand All @@ -310,7 +309,7 @@ def test_report_error(mock_mgr, err, stop_on, msg):
mock_mgr.setup_stubs(err, stop_on, sql, sql_params, report="small")

with mock_mgr.app.test_client() as client:
rte = f"/api/items:report"
rte = "/api/items:report"
rv = client.post(rte, json={"email": mock_mgr.recipient})
assert rv.status_code == 500
assert msg in rv.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@

import logging
import urllib.parse

import chalicelib.library_data
from chalice import Chalice
from chalice.app import RequestTimeoutError
import chalicelib.library_data

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
Expand Down Expand Up @@ -138,7 +139,7 @@ def add_patron():
logger.exception(
f"Got exception in add_patron() inside library_api/app.py: {str(err)}"
)
logger.exception(f"Returning None instead of patron_id.")
logger.exception("Returning None instead of patron_id.")
return None


Expand Down Expand Up @@ -169,7 +170,7 @@ def list_borrowed_books():
logger.exception(
f"Exception while calling get_storage().get_borrowed_books(): {str(err)}"
)
logger.exception(f"Continuing with blank list of borrowed books...")
logger.exception("Continuing with blank list of borrowed books...")
return {"books": []}


Expand Down
Loading
Loading