Skip to content

Commit

Permalink
Simplify string expressions (#12123)
Browse files Browse the repository at this point in the history
Black has trouble formatting strings that are too long and produces unusual sring expressions.
  • Loading branch information
kaxil authored Nov 6, 2020
1 parent f097ae3 commit cf9437d
Show file tree
Hide file tree
Showing 15 changed files with 36 additions and 56 deletions.
10 changes: 4 additions & 6 deletions airflow/cli/cli_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,7 @@ def positive_int(value):
)
ARG_SAVE_DAGRUN = Arg(
("--save-dagrun",),
help=(
"After completing the backfill, saves the diagram for current DAG Run to the indicated file.\n" "\n"
),
help=("After completing the backfill, saves the diagram for current DAG Run to the indicated file.\n\n"),
)

# list_tasks
Expand Down Expand Up @@ -453,12 +451,12 @@ def positive_int(value):
ARG_ACCESS_LOGFILE = Arg(
("-A", "--access-logfile"),
default=conf.get('webserver', 'ACCESS_LOGFILE'),
help="The logfile to store the webserver access log. Use '-' to print to " "stderr",
help="The logfile to store the webserver access log. Use '-' to print to stderr",
)
ARG_ERROR_LOGFILE = Arg(
("-E", "--error-logfile"),
default=conf.get('webserver', 'ERROR_LOGFILE'),
help="The logfile to store the webserver error log. Use '-' to print to " "stderr",
help="The logfile to store the webserver error log. Use '-' to print to stderr",
)

# scheduler
Expand Down Expand Up @@ -493,7 +491,7 @@ def positive_int(value):
)
ARG_CELERY_HOSTNAME = Arg(
("-H", "--celery-hostname"),
help=("Set the hostname of celery worker " "if you have multiple workers on a single machine"),
help=("Set the hostname of celery worker if you have multiple workers on a single machine"),
)
ARG_UMASK = Arg(
("-u", "--umask"),
Expand Down
4 changes: 1 addition & 3 deletions airflow/cli/commands/dag_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ def dag_delete(args):
api_client = get_current_api_client()
if (
args.yes
or input(
"This will drop all existing records related to the specified DAG. " "Proceed? (y/n)"
).upper()
or input("This will drop all existing records related to the specified DAG. Proceed? (y/n)").upper()
== "Y"
):
try:
Expand Down
2 changes: 1 addition & 1 deletion airflow/cli/commands/db_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initdb(args):
def resetdb(args):
"""Resets the metadata database"""
print("DB: " + repr(settings.engine.url))
if args.yes or input("This will drop existing tables " "if they exist. Proceed? " "(y/n)").upper() == "Y":
if args.yes or input("This will drop existing tables if they exist. Proceed? (y/n)").upper() == "Y":
db.resetdb()
else:
print("Cancelled")
Expand Down
2 changes: 1 addition & 1 deletion airflow/cli/commands/user_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def _import_users(users_list): # pylint: disable=redefined-outer-name
required_fields = ['username', 'firstname', 'lastname', 'email', 'roles']
for field in required_fields:
if not user.get(field):
print("Error: '{}' is a required field, but was not " "specified".format(field))
print(f"Error: '{field}' is a required field, but was not specified")
sys.exit(1)

existing_user = appbuilder.sm.find_user(email=user['email'])
Expand Down
2 changes: 1 addition & 1 deletion airflow/cli/commands/webserver_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def _check_workers(self) -> None:
# to increase number of workers
if num_workers_running < self.num_workers_expected:
self.log.error(
"[%d / %d] Some workers seem to have died and gunicorn did not restart " "them as expected",
"[%d / %d] Some workers seem to have died and gunicorn did not restart them as expected",
num_ready_workers_running,
num_workers_running,
)
Expand Down
2 changes: 1 addition & 1 deletion airflow/providers/amazon/aws/hooks/sagemaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def check_s3_url(self, s3url: str) -> bool:
# or if s3 prefix exists in the case user provides multiple files in
# a prefix
raise AirflowException(
"The input S3 Key " "or Prefix {} does not exist in the Bucket {}".format(s3url, bucket)
f"The input S3 Key or Prefix {s3url} does not exist in the Bucket {bucket}"
)
return True

Expand Down
4 changes: 2 additions & 2 deletions airflow/providers/google/cloud/hooks/bigquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ def run_query(
_validate_value(param_name, configuration['query'][param_name], param_type)

if param_name == 'schemaUpdateOptions' and param:
self.log.info("Adding experimental 'schemaUpdateOptions': " "%s", schema_update_options)
self.log.info("Adding experimental 'schemaUpdateOptions': %s", schema_update_options)

if param_name != 'destinationTable':
continue
Expand Down Expand Up @@ -2167,7 +2167,7 @@ def run_query(
and configuration['query']['useLegacySql']
and 'queryParameters' in configuration['query']
):
raise ValueError("Query parameters are not allowed " "when using legacy SQL")
raise ValueError("Query parameters are not allowed when using legacy SQL")

if labels:
_api_resource_configs_duplication_check('labels', labels, configuration)
Expand Down
4 changes: 2 additions & 2 deletions airflow/providers/google/cloud/hooks/cloud_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,10 +646,10 @@ def get_socket_path(self) -> str:
"mysql": {
"proxy": {
"tcp": "mysql://{user}:{password}@127.0.0.1:{proxy_port}/{database}",
"socket": "mysql://{user}:{password}@localhost/{database}?" "unix_socket={socket_path}",
"socket": "mysql://{user}:{password}@localhost/{database}?unix_socket={socket_path}",
},
"public": {
"ssl": "mysql://{user}:{password}@{public_ip}:{public_port}/{database}?" "ssl={ssl_spec}",
"ssl": "mysql://{user}:{password}@{public_ip}:{public_port}/{database}?ssl={ssl_spec}",
"non-ssl": "mysql://{user}:{password}@{public_ip}:{public_port}/{database}",
},
},
Expand Down
2 changes: 1 addition & 1 deletion airflow/providers/google/cloud/hooks/gcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ def upload(
blob.upload_from_string(data, content_type=mime_type)
self.log.info('Data stream uploaded to %s in %s bucket', object_name, bucket_name)
else:
raise ValueError("'filename' and 'data' parameter missing. " "One is required to upload to gcs.")
raise ValueError("'filename' and 'data' parameter missing. One is required to upload to gcs.")

def exists(self, bucket_name: str, object_name: str) -> bool:
"""
Expand Down
4 changes: 1 addition & 3 deletions airflow/providers/google/cloud/operators/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,7 @@ def _update_function(self, hook) -> None:
def _check_if_function_exists(self, hook) -> bool:
name = self.body.get('name')
if not name:
raise GcpFieldValidationException(
"The 'name' field should be present in " "body: '{}'.".format(self.body)
)
raise GcpFieldValidationException(f"The 'name' field should be present in body: '{self.body}'.")
try:
hook.get_function(name)
except HttpError as e:
Expand Down
2 changes: 1 addition & 1 deletion tests/providers/google/cloud/operators/test_compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ def test_missing_name(self, mock_hook):
)
op.execute(None)
err = cm.exception
self.assertIn("should contain at least name for the new operator " "in the 'name' field", str(err))
self.assertIn("should contain at least name for the new operator in the 'name' field", str(err))
mock_hook.assert_not_called()


Expand Down
22 changes: 10 additions & 12 deletions tests/providers/google/marketing_platform/hooks/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def test_init(self, mock_base_init):
)
self.assertEqual(hook.api_version, API_VERSION)

@mock.patch(
"airflow.providers.google.marketing_platform.hooks." "analytics.GoogleAnalyticsHook._authorize"
)
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.GoogleAnalyticsHook._authorize")
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.build")
def test_gen_conn(self, mock_build, mock_authorize):
result = self.hook.get_conn()
Expand All @@ -68,7 +66,7 @@ def test_gen_conn(self, mock_build, mock_authorize):
)
self.assertEqual(mock_build.return_value, result)

@mock.patch("airflow.providers.google.marketing_platform.hooks." "analytics.GoogleAnalyticsHook.get_conn")
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.GoogleAnalyticsHook.get_conn")
def test_list_accounts(self, get_conn_mock):
mock_accounts = get_conn_mock.return_value.management.return_value.accounts
mock_list = mock_accounts.return_value.list
Expand All @@ -77,7 +75,7 @@ def test_list_accounts(self, get_conn_mock):
list_accounts = self.hook.list_accounts()
self.assertEqual(list_accounts, ["a", "b"])

@mock.patch("airflow.providers.google.marketing_platform.hooks." "analytics.GoogleAnalyticsHook.get_conn")
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.GoogleAnalyticsHook.get_conn")
def test_list_accounts_for_multiple_pages(self, get_conn_mock):
mock_accounts = get_conn_mock.return_value.management.return_value.accounts
mock_list = mock_accounts.return_value.list
Expand All @@ -89,7 +87,7 @@ def test_list_accounts_for_multiple_pages(self, get_conn_mock):
list_accounts = self.hook.list_accounts()
self.assertEqual(list_accounts, ["a", "b"])

@mock.patch("airflow.providers.google.marketing_platform.hooks." "analytics.GoogleAnalyticsHook.get_conn")
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.GoogleAnalyticsHook.get_conn")
def test_get_ad_words_links_call(self, get_conn_mock):
num_retries = 5
self.hook.get_ad_words_link(
Expand All @@ -111,7 +109,7 @@ def test_get_ad_words_links_call(self, get_conn_mock):
)
# fmt: on

@mock.patch("airflow.providers.google.marketing_platform.hooks." "analytics.GoogleAnalyticsHook.get_conn")
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.GoogleAnalyticsHook.get_conn")
def test_list_ad_words_links(self, get_conn_mock):
mock_ads_links = get_conn_mock.return_value.management.return_value.webPropertyAdWordsLinks
mock_list = mock_ads_links.return_value.list
Expand All @@ -120,7 +118,7 @@ def test_list_ad_words_links(self, get_conn_mock):
list_ads_links = self.hook.list_ad_words_links(account_id=ACCOUNT_ID, web_property_id=WEB_PROPERTY_ID)
self.assertEqual(list_ads_links, ["a", "b"])

@mock.patch("airflow.providers.google.marketing_platform.hooks." "analytics.GoogleAnalyticsHook.get_conn")
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.GoogleAnalyticsHook.get_conn")
def test_list_ad_words_links_for_multiple_pages(self, get_conn_mock):
mock_ads_links = get_conn_mock.return_value.management.return_value.webPropertyAdWordsLinks
mock_list = mock_ads_links.return_value.list
Expand All @@ -132,8 +130,8 @@ def test_list_ad_words_links_for_multiple_pages(self, get_conn_mock):
list_ads_links = self.hook.list_ad_words_links(account_id=ACCOUNT_ID, web_property_id=WEB_PROPERTY_ID)
self.assertEqual(list_ads_links, ["a", "b"])

@mock.patch("airflow.providers.google.marketing_platform.hooks." "analytics.GoogleAnalyticsHook.get_conn")
@mock.patch("airflow.providers.google.marketing_platform.hooks." "analytics.MediaFileUpload")
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.GoogleAnalyticsHook.get_conn")
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.MediaFileUpload")
def test_upload_data(self, media_mock, get_conn_mock):
temp_name = "temp/file"
self.hook.upload_data(
Expand All @@ -155,7 +153,7 @@ def test_upload_data(self, media_mock, get_conn_mock):
)
# fmt: on

@mock.patch("airflow.providers.google.marketing_platform.hooks." "analytics.GoogleAnalyticsHook.get_conn")
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.GoogleAnalyticsHook.get_conn")
def test_delete_upload_data(self, get_conn_mock):
body = {"key": "temp/file"}
self.hook.delete_upload_data(
Expand All @@ -174,7 +172,7 @@ def test_delete_upload_data(self, get_conn_mock):
)
# fmt: on

@mock.patch("airflow.providers.google.marketing_platform.hooks." "analytics.GoogleAnalyticsHook.get_conn")
@mock.patch("airflow.providers.google.marketing_platform.hooks.analytics.GoogleAnalyticsHook.get_conn")
def test_list_upload(self, get_conn_mock):
uploads = get_conn_mock.return_value.management.return_value.uploads.return_value
uploads.list.return_value.execute.return_value = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def setUp(self):
"airflow.providers.google.marketing_platform.hooks."
"display_video.GoogleDisplayVideo360Hook._authorize"
)
@mock.patch("airflow.providers.google.marketing_platform.hooks." "display_video.build")
@mock.patch("airflow.providers.google.marketing_platform.hooks.display_video.build")
def test_gen_conn(self, mock_build, mock_authorize):
result = self.hook.get_conn()
mock_build.assert_called_once_with(
Expand All @@ -51,7 +51,7 @@ def test_gen_conn(self, mock_build, mock_authorize):
"airflow.providers.google.marketing_platform.hooks."
"display_video.GoogleDisplayVideo360Hook._authorize"
)
@mock.patch("airflow.providers.google.marketing_platform.hooks." "display_video.build")
@mock.patch("airflow.providers.google.marketing_platform.hooks.display_video.build")
def test_get_conn_to_display_video(self, mock_build, mock_authorize):
result = self.hook.get_conn_to_display_video()
mock_build.assert_called_once_with(
Expand Down
16 changes: 4 additions & 12 deletions tests/providers/google/marketing_platform/hooks/test_search_ads.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def setUp(self):
):
self.hook = GoogleSearchAdsHook(gcp_conn_id=GCP_CONN_ID)

@mock.patch(
"airflow.providers.google.marketing_platform.hooks." "search_ads.GoogleSearchAdsHook._authorize"
)
@mock.patch("airflow.providers.google.marketing_platform.hooks.search_ads.GoogleSearchAdsHook._authorize")
@mock.patch("airflow.providers.google.marketing_platform.hooks.search_ads.build")
def test_gen_conn(self, mock_build, mock_authorize):
result = self.hook.get_conn()
Expand All @@ -46,9 +44,7 @@ def test_gen_conn(self, mock_build, mock_authorize):
)
self.assertEqual(mock_build.return_value, result)

@mock.patch(
"airflow.providers.google.marketing_platform.hooks." "search_ads.GoogleSearchAdsHook.get_conn"
)
@mock.patch("airflow.providers.google.marketing_platform.hooks.search_ads.GoogleSearchAdsHook.get_conn")
def test_insert(self, get_conn_mock):
report = {"report": "test"}

Expand All @@ -63,9 +59,7 @@ def test_insert(self, get_conn_mock):

self.assertEqual(return_value, result)

@mock.patch(
"airflow.providers.google.marketing_platform.hooks." "search_ads.GoogleSearchAdsHook.get_conn"
)
@mock.patch("airflow.providers.google.marketing_platform.hooks.search_ads.GoogleSearchAdsHook.get_conn")
def test_get(self, get_conn_mock):
report_id = "REPORT_ID"

Expand All @@ -78,9 +72,7 @@ def test_get(self, get_conn_mock):

self.assertEqual(return_value, result)

@mock.patch(
"airflow.providers.google.marketing_platform.hooks." "search_ads.GoogleSearchAdsHook.get_conn"
)
@mock.patch("airflow.providers.google.marketing_platform.hooks.search_ads.GoogleSearchAdsHook.get_conn")
def test_get_file(self, get_conn_mock):
report_fragment = 42
report_id = "REPORT_ID"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@


class TestGoogleDisplayVideo360ReportSensor(TestCase):
@mock.patch(
"airflow.providers.google.marketing_platform.sensors." "display_video.GoogleDisplayVideo360Hook"
)
@mock.patch("airflow.providers.google.marketing_platform.sensors." "display_video.BaseSensorOperator")
@mock.patch("airflow.providers.google.marketing_platform.sensors.display_video.GoogleDisplayVideo360Hook")
@mock.patch("airflow.providers.google.marketing_platform.sensors.display_video.BaseSensorOperator")
def test_poke(self, mock_base_op, hook_mock):
report_id = "REPORT_ID"
op = GoogleDisplayVideo360ReportSensor(
Expand All @@ -48,10 +46,8 @@ def test_poke(self, mock_base_op, hook_mock):


class TestGoogleDisplayVideo360Sensor(TestCase):
@mock.patch(
"airflow.providers.google.marketing_platform.sensors." "display_video.GoogleDisplayVideo360Hook"
)
@mock.patch("airflow.providers.google.marketing_platform.sensors." "display_video.BaseSensorOperator")
@mock.patch("airflow.providers.google.marketing_platform.sensors.display_video.GoogleDisplayVideo360Hook")
@mock.patch("airflow.providers.google.marketing_platform.sensors.display_video.BaseSensorOperator")
def test_poke(self, mock_base_op, hook_mock):
operation_name = "operation_name"
op = GoogleDisplayVideo360GetSDFDownloadOperationSensor(
Expand Down

0 comments on commit cf9437d

Please sign in to comment.
  翻译: