This is an automated email from the ASF dual-hosted git repository.
potiuk pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new ba0bab0114 Refactor: Use random.choices (#33631)
ba0bab0114 is described below
commit ba0bab0114a430ef0ac776980f7e29b34d48b726
Author: Miroslav Šedivý <[email protected]>
AuthorDate: Thu Aug 24 23:09:01 2023 +0000
Refactor: Use random.choices (#33631)
---
airflow/auth/managers/fab/cli_commands/user_command.py | 2 +-
airflow/cli/commands/standalone_command.py | 5 ++---
airflow/providers/google/cloud/hooks/cloud_sql.py | 2 +-
airflow/providers/google/cloud/hooks/mlengine.py | 4 ++--
tests/providers/elasticsearch/log/elasticmock/utilities/__init__.py | 2 +-
tests/providers/ssh/hooks/test_ssh.py | 2 +-
tests/system/providers/apache/kafka/example_dag_event_listener.py | 3 +--
tests/test_utils/azure_system_helpers.py | 2 +-
8 files changed, 10 insertions(+), 12 deletions(-)
diff --git a/airflow/auth/managers/fab/cli_commands/user_command.py
b/airflow/auth/managers/fab/cli_commands/user_command.py
index 84e6318e40..e82ece05d5 100644
--- a/airflow/auth/managers/fab/cli_commands/user_command.py
+++ b/airflow/auth/managers/fab/cli_commands/user_command.py
@@ -70,7 +70,7 @@ def users_create(args):
valid_roles = appbuilder.sm.get_all_roles()
raise SystemExit(f"{args.role} is not a valid role. Valid roles
are: {valid_roles}")
if args.use_random_password:
- password = "".join(random.choice(string.printable) for _ in
range(16))
+ password = "".join(random.choices(string.printable, k=16))
elif args.password:
password = args.password
else:
diff --git a/airflow/cli/commands/standalone_command.py
b/airflow/cli/commands/standalone_command.py
index 28632abf6a..51d107869d 100644
--- a/airflow/cli/commands/standalone_command.py
+++ b/airflow/cli/commands/standalone_command.py
@@ -193,9 +193,8 @@ class StandaloneCommand:
self.print_output("standalone", "Creating admin user")
role = appbuilder.sm.find_role("Admin")
assert role is not None
- password = "".join(
-
random.choice("abcdefghkmnpqrstuvwxyzABCDEFGHKMNPQRSTUVWXYZ23456789") for i in
range(16)
- )
+ # password does not contain visually similar characters: ijlIJL1oO0
+ password =
"".join(random.choices("abcdefghkmnpqrstuvwxyzABCDEFGHKMNPQRSTUVWXYZ23456789",
k=16))
with open(password_path, "w") as file:
file.write(password)
make_group_other_inaccessible(password_path)
diff --git a/airflow/providers/google/cloud/hooks/cloud_sql.py
b/airflow/providers/google/cloud/hooks/cloud_sql.py
index ebd03b69bb..f89b460c0e 100644
--- a/airflow/providers/google/cloud/hooks/cloud_sql.py
+++ b/airflow/providers/google/cloud/hooks/cloud_sql.py
@@ -887,7 +887,7 @@ class CloudSQLDatabaseHook(BaseHook):
random.seed()
while True:
candidate = os.path.join(
- gettempdir(), "".join(random.choice(string.ascii_lowercase +
string.digits) for _ in range(8))
+ gettempdir(), "".join(random.choices(string.ascii_lowercase +
string.digits, k=8))
)
if not os.path.exists(candidate):
return candidate
diff --git a/airflow/providers/google/cloud/hooks/mlengine.py
b/airflow/providers/google/cloud/hooks/mlengine.py
index ff13546685..aabe2b88ae 100644
--- a/airflow/providers/google/cloud/hooks/mlengine.py
+++ b/airflow/providers/google/cloud/hooks/mlengine.py
@@ -65,13 +65,13 @@ def _poll_with_exponential_delay(
log.info("Operation is done: %s", response)
return response
- time.sleep((2**i) + (random.randint(0, 1000) / 1000))
+ time.sleep((2**i) + random.random())
except HttpError as e:
if e.resp.status != 429:
log.info("Something went wrong. Not retrying: %s", format(e))
raise
else:
- time.sleep((2**i) + (random.randint(0, 1000) / 1000))
+ time.sleep((2**i) + random.random())
raise ValueError(f"Connection could not be established after {max_n}
retries.")
diff --git
a/tests/providers/elasticsearch/log/elasticmock/utilities/__init__.py
b/tests/providers/elasticsearch/log/elasticmock/utilities/__init__.py
index cb2d91f4ce..e145b81818 100644
--- a/tests/providers/elasticsearch/log/elasticmock/utilities/__init__.py
+++ b/tests/providers/elasticsearch/log/elasticmock/utilities/__init__.py
@@ -54,7 +54,7 @@ GLOBAL_PARAMS = ("pretty", "human", "error_trace", "format",
"filter_path")
def get_random_id(size=DEFAULT_ELASTICSEARCH_ID_SIZE):
"""Returns random if for elasticsearch"""
- return "".join(random.choice(CHARSET_FOR_ELASTICSEARCH_ID) for _ in
range(size))
+ return "".join(random.choices(CHARSET_FOR_ELASTICSEARCH_ID, k=size))
def query_params(*es_query_params, **kwargs):
diff --git a/tests/providers/ssh/hooks/test_ssh.py
b/tests/providers/ssh/hooks/test_ssh.py
index d735092bbe..352b6692da 100644
--- a/tests/providers/ssh/hooks/test_ssh.py
+++ b/tests/providers/ssh/hooks/test_ssh.py
@@ -77,7 +77,7 @@ TEST_CMD_TIMEOUT = 5
TEST_CMD_TIMEOUT_NOT_SET = "NOT SET"
TEST_CMD_TIMEOUT_EXTRA = 15
-PASSPHRASE = "".join(random.choice(string.ascii_letters) for i in range(10))
+PASSPHRASE = "".join(random.choices(string.ascii_letters, k=10))
TEST_ENCRYPTED_PRIVATE_KEY = generate_key_string(pkey=TEST_PKEY,
passphrase=PASSPHRASE)
TEST_DISABLED_ALGORITHMS = {"pubkeys": ["rsa-sha2-256", "rsa-sha2-512"]}
diff --git a/tests/system/providers/apache/kafka/example_dag_event_listener.py
b/tests/system/providers/apache/kafka/example_dag_event_listener.py
index f7023ec4e2..e42f28eb72 100644
--- a/tests/system/providers/apache/kafka/example_dag_event_listener.py
+++ b/tests/system/providers/apache/kafka/example_dag_event_listener.py
@@ -69,8 +69,7 @@ def _producer_function():
def _generate_uuid():
- letters = string.ascii_lowercase
- return "".join(random.choice(letters) for i in range(6))
+ return "".join(random.choices(string.ascii_lowercase, k=6))
with DAG(
diff --git a/tests/test_utils/azure_system_helpers.py
b/tests/test_utils/azure_system_helpers.py
index a4d0dfa05f..85c700537a 100644
--- a/tests/test_utils/azure_system_helpers.py
+++ b/tests/test_utils/azure_system_helpers.py
@@ -146,7 +146,7 @@ class AzureSystemTest(SystemTest):
cls.create_directory(
share_name=share_name,
azure_fileshare_conn_id=azure_fileshare_conn_id, directory=directory
)
- string_data = "".join(random.choice(string.ascii_letters) for _ in
range(1024))
+ string_data = "".join(random.choices(string.ascii_letters, k=1024))
cls.upload_file_from_string(
string_data=string_data,
share_name=share_name,