Skip to content

Commit

Permalink
Fix flake8-comprehensions warnings
Browse files Browse the repository at this point in the history
Warnings fixed:

 * C401 Unnecessary generator (rewrite as a `set` comprehension)
 * C402 Unnecessary generator (rewrite as a `dict` comprehension)
 * C403 Unnecessary `list` comprehension (rewrite as a `set` comprehension)
 * C405 Unnecessary `list` literal (rewrite as a `set` literal)
 * C408 Unnecessary `dict` call (rewrite as a literal)
 * C416 Unnecessary `set` comprehension (rewrite using `set()`)
 * C414 Unnecessary `list` call within `set()`
 * C419 Unnecessary list comprehension

Statistics:

23      C402    [*] unnecessary-generator-dict
16      C403    [*] unnecessary-list-comprehension-set
11      C408    [*] unnecessary-collection-call
 9      C401    [*] unnecessary-generator-set
 4      C405    [*] unnecessary-literal-set
 4      C416    [*] unnecessary-comprehension
 4      C419    [*] unnecessary-comprehension-in-call
 1      C414    [*] unnecessary-double-cast-or-process
  • Loading branch information
cutwater committed Oct 16, 2024
1 parent a05fd62 commit 17bb2d9
Show file tree
Hide file tree
Showing 40 changed files with 108 additions and 110 deletions.
2 changes: 1 addition & 1 deletion galaxy_ng/app/access_control/access_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ def require_requirements_yaml(self, request, view, action):
return True

if not remote.requirements_file and any(
[domain in remote.url for domain in COMMUNITY_DOMAINS]
domain in remote.url for domain in COMMUNITY_DOMAINS
):
raise ValidationError(
detail={
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/app/access_control/statements/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def _process_permissions(role, roles):


def _process_locked_roles(roles):
locked = dict()
locked = {}
for role in roles:
locked[role] = {"permissions": list(_process_permissions(role, roles))}
return locked
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/app/api/ui/v1/serializers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def validate_groups(self, groups):
group_set = set(groups)
instance_group_set = set()
if self.instance:
instance_group_set = set(list(self.instance.groups.all()))
instance_group_set = set(self.instance.groups.all())

group_difference = instance_group_set.symmetric_difference(group_set)

Expand Down
4 changes: 3 additions & 1 deletion galaxy_ng/app/api/v1/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,9 @@ def do_git_checkout(clone_url, checkout_path, github_reference):
logger.error(f'{cmd} failed: {error}')
raise Exception(f'{cmd} failed')

last_commit = [x for x in gitrepo.iter_commits()][0]
# REVIEW(cutwater): Why this iterates over all commits to get first one and doesn't use
# gitrepo.head.commit as the code below?
last_commit = list(gitrepo.iter_commits())[0]

else:
# use the default branch ...
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/app/api/v3/serializers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def get_write_only_fields(self, obj):

def validate(self, data):
if not data.get('requirements_file') and any(
[domain in data['url'] for domain in COMMUNITY_DOMAINS]
domain in data['url'] for domain in COMMUNITY_DOMAINS
):
raise serializers.ValidationError(
detail={
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/app/api/v3/views/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def post(self, request: Request, *args, **kwargs) -> Response:
remote = distro.repository.remote.ansible_collectionremote

if not remote.requirements_file and any(
[domain in remote.url for domain in COMMUNITY_DOMAINS]
domain in remote.url for domain in COMMUNITY_DOMAINS
):
raise ValidationError(
detail={
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/app/management/commands/create-remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def add_arguments(self, parser):

def validate(self, data):
if not data["requirements_file"] and any(
[domain in data["url"] for domain in COMMUNITY_DOMAINS]
domain in data["url"] for domain in COMMUNITY_DOMAINS
):
raise CommandError(
'Syncing content from community domains without specifying a '
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/app/migrations/0029_move_perms_to_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def get_roles_from_permissions(permission_iterable, translator, Role, Permission
super_permissions = {}

# Use set comparisons instead of querysets to avoid unnecesary trips to the DB
permissions = set(((p.content_type.app_label, p.codename) for p in permission_iterable))
permissions = {(p.content_type.app_label, p.codename) for p in permission_iterable}

# Iterate through each locked role, apply any roles that match the group's permission
# set and remove any permissions that are applied via roles
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/app/migrations/0038_namespace_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def add_pulp_ansible_namespace_metadata_objects(apps, schema_editor):
# skip metadata creation for namespaces with no data.
create_metadata = False
for f in metadata:
if metadata[f] not in ('', dict(), None):
if metadata[f] not in ('', {}, None):
create_metadata = True
break

Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/app/migrations/_dab_rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


def pulp_role_to_single_content_type_or_none(pulprole):
content_types = set(perm.content_type for perm in pulprole.permissions.all())
content_types = {perm.content_type for perm in pulprole.permissions.all()}
if len(list(content_types)) == 1:
return list(content_types)[0]
return None
Expand Down
14 changes: 7 additions & 7 deletions galaxy_ng/app/signals/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def rbac_signal_in_progress():


def pulp_role_to_single_content_type_or_none(pulprole):
content_types = set(perm.content_type for perm in pulprole.permissions.all())
content_types = {perm.content_type for perm in pulprole.permissions.all()}
if len(list(content_types)) == 1:
return list(content_types)[0]
return None
Expand All @@ -189,8 +189,8 @@ def copy_permissions_role_to_role(roleA, roleB):
"""
permissionsA = list(roleA.permissions.prefetch_related("content_type"))
permissionsB = list(roleB.permissions.prefetch_related("content_type"))
fullnamesA = set(f"{perm.content_type.app_label}.{perm.codename}" for perm in permissionsA)
fullnamesB = set(f"{perm.content_type.app_label}.{perm.codename}" for perm in permissionsB)
fullnamesA = {f"{perm.content_type.app_label}.{perm.codename}" for perm in permissionsA}
fullnamesB = {f"{perm.content_type.app_label}.{perm.codename}" for perm in permissionsB}
fullnames_to_add = fullnamesA - fullnamesB
fullnames_to_remove = fullnamesB - fullnamesA
concat_exp = Concat("content_type__app_label", Value("."), "codename", output_field=CharField())
Expand Down Expand Up @@ -572,16 +572,16 @@ def copy_dab_group_to_role(instance, action, model, pk_set, reverse, **kwargs):
# are changed to match the users in the pulp group
for group in groups:
team = Team.objects.get(group_id=group.pk)
current_dab_members = set(
current_dab_members = {
assignment.user for assignment in RoleUserAssignment.objects.filter(
role_definition=member_rd, object_id=team.pk
)
)
current_dab_shared_members = set(
}
current_dab_shared_members = {
assignment.user for assignment in RoleUserAssignment.objects.filter(
role_definition=shared_member_rd, object_id=team.pk
)
)
}
current_pulp_members = set(group.user_set.all())
not_allowed = current_dab_shared_members - current_pulp_members
if not_allowed:
Expand Down
20 changes: 10 additions & 10 deletions galaxy_ng/app/tasks/promotion.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ def call_auto_approve_task(collection_version, repo, ns_pk):
auto_approve,
exclusive_resources=[repo],
task_group=task_group,
kwargs=dict(
cv_pk=collection_version.pk,
src_repo_pk=repo.pk,
ns_pk=ns_pk,
),
kwargs={
'cv_pk': collection_version.pk,
'src_repo_pk': repo.pk,
'ns_pk': ns_pk,
},
)

task_group.finish()
Expand All @@ -90,9 +90,9 @@ def call_move_content_task(collection_version, source_repo, dest_repo):
return dispatch(
move_collection,
exclusive_resources=[source_repo, dest_repo],
kwargs=dict(
cv_pk_list=[collection_version.pk],
src_repo_pk=source_repo.pk,
dest_repo_list=[dest_repo.pk],
),
kwargs={
'cv_pk_list': [collection_version.pk],
'src_repo_pk': source_repo.pk,
'dest_repo_list': [dest_repo.pk],
},
)
10 changes: 5 additions & 5 deletions galaxy_ng/app/tasks/publishing.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ def import_to_staging(username, **kwargs):
dispatch(
add_and_remove,
exclusive_resources=[repo],
kwargs=dict(
add_content_units=add,
repository_pk=repo.pk,
remove_content_units=[],
),
kwargs={
"add_content_units": add,
"repository_pk": repo.pk,
"remove_content_units": []
},
)

if settings.GALAXY_ENABLE_API_ACCESS_LOG:
Expand Down
24 changes: 12 additions & 12 deletions galaxy_ng/app/tasks/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ def call_sign_and_move_task(signing_service, collection_version, source_repo, de
return dispatch(
sign_and_move,
exclusive_resources=[source_repo, dest_repo],
kwargs=dict(
signing_service_pk=signing_service.pk,
collection_version_pk=collection_version.pk,
source_repo_pk=source_repo.pk,
dest_repo_pk=dest_repo.pk,
)
kwargs={
"signing_service_pk": signing_service.pk,
"collection_version_pk": collection_version.pk,
"source_repo_pk": source_repo.pk,
"dest_repo_pk": dest_repo.pk,
}
)


Expand All @@ -45,7 +45,7 @@ def sign_and_move(signing_service_pk, collection_version_pk, source_repo_pk, des

# Move content from source to destination
move_collection(
cv_pk_list=[collection_version_pk, ],
cv_pk_list=[collection_version_pk],
src_repo_pk=source_repo_pk,
dest_repo_list=[dest_repo_pk],
)
Expand All @@ -68,9 +68,9 @@ def call_sign_task(signing_service, repository, content_units):
return dispatch(
sign,
exclusive_resources=[repository],
kwargs=dict(
repository_href=repository.pk,
content_hrefs=content_units,
signing_service_href=signing_service.pk
)
kwargs={
"repository_href": repository.pk,
"content_hrefs": content_units,
"signing_service_href": signing_service.pk,
}
)
2 changes: 1 addition & 1 deletion galaxy_ng/app/utils/rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def get_v3_namespace_owners(namespace: Namespace) -> list:
"""
Return a list of users that own a v3 namespace.
"""
owners = list()
owners = []
current_groups = get_groups_with_perms_attached_roles(
namespace,
include_model_permissions=False
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/openapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ def dedupe_operationIds(self, schema):

def flatten_path(self, path):
"""Assemble a new operationId prefix from a given path"""
paths = [x for x in path.split('/')]
paths = path.split('/')
return '_'.join(paths[:-1])
6 changes: 3 additions & 3 deletions galaxy_ng/social/pipeline/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def create_user(strategy, details, backend, user=None, *args, **kwargs):
logger.info(f'create_user(2): returning user-kwarg {user}:{user.id}')
return {'is_new': False}

fields = dict(
(name, kwargs.get(name, details.get(name)))
fields = {
name: kwargs.get(name, details.get(name))
for name in backend.setting('USER_FIELDS', USER_FIELDS)
)
}

if not fields:
logger.info(f'create_user(3): no fields for {user}:{user.id}')
Expand Down
9 changes: 5 additions & 4 deletions galaxy_ng/tests/integration/aap/test_aap_rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ def test_aap_service_index_and_claims_processing(

# get all of gateway's roledefs ...
gateway_roledefs = ga.get('/api/gateway/v1/role_definitions/')
gateway_roledefs = dict((x['name'], x) for x in gateway_roledefs['results'])
gateway_roledefs = {x['name']: x for x in gateway_roledefs['results']}

# get all of galaxy's roledefs ...
galaxy_roledefs = ga.get('/api/galaxy/_ui/v2/role_definitions/')
galaxy_roledefs = dict((x['name'], x) for x in galaxy_roledefs['results'])
# REVIEW(cutwater): Unused variable
galaxy_roledefs = {x['name']: x for x in galaxy_roledefs['results']}

# make the user a team member in the gateway ...
team_assignment = ga.post(
Expand Down Expand Up @@ -165,11 +166,11 @@ def test_aap_platform_auditor_claims_processing(

# get all of gateway's roledefs ...
gateway_roledefs = ga.get('/api/gateway/v1/role_definitions/')
gateway_roledefs = dict((x['name'], x) for x in gateway_roledefs['results'])
gateway_roledefs = {x['name']: x for x in gateway_roledefs['results']}

# get all of galaxy's roledefs ...
galaxy_roledefs = ga.get('_ui/v2/role_definitions/')
galaxy_roledefs = dict((x['name'], x) for x in galaxy_roledefs['results'])
galaxy_roledefs = {x['name']: x for x in galaxy_roledefs['results']}

#########################################################
# ASSIGN
Expand Down
22 changes: 11 additions & 11 deletions galaxy_ng/tests/integration/api/test_certified_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,35 @@ def _assert_sync(manifest, client):
deprecated_collections.add(collection)

# test that all the expected namespaces are created
all_namespaces = set([x["name"] for x in iterate_all(client, "v3/namespaces/")])
all_namespaces = {x["name"] for x in iterate_all(client, "v3/namespaces/")}
assert namespaces.issubset(all_namespaces)

# test that all the synced collections are on the v3 API
synced_collections = set([(x["namespace"], x["name"]) for x in iterate_all(
synced_collections = {(x["namespace"], x["name"]) for x in iterate_all(
client,
"v3/plugin/ansible/content/rh-certified/collections/index/"
)])
)}
assert synced_collections == collections

# Test that the _ui/v1/repo/ api returns all the synced collections
synced_collections = set([(x["namespace"]["name"], x["name"]) for x in iterate_all(
synced_collections = {(x["namespace"]["name"], x["name"]) for x in iterate_all(
client,
"_ui/v1/repo/rh-certified/"
)])
)}
assert synced_collections == collections

# Test that the _ui/v1/repo/ api returns all the synced signed collections
synced_collections = set([(x["namespace"]["name"], x["name"]) for x in iterate_all(
synced_collections = {(x["namespace"]["name"], x["name"]) for x in iterate_all(
client,
"_ui/v1/repo/rh-certified/?sign_state=signed"
)])
)}
assert synced_collections == signed_collections

# Test that the deprecated status syncs correctly
synced_collections = set([(x["namespace"]["name"], x["name"]) for x in iterate_all(
synced_collections = {(x["namespace"]["name"], x["name"]) for x in iterate_all(
client,
"_ui/v1/repo/rh-certified/?deprecated=false"
)])
)}
assert synced_collections == collections.difference(deprecated_collections)

# Test that the _ui/v1/collection-versions/ API shows the correct collections
Expand All @@ -92,8 +92,8 @@ def _assert_sync(manifest, client):

if version in signed_versions:
assert c["sign_state"] == "signed"
local_sigs = set([x["signature"] for x in c["metadata"]["signatures"]])
manifest_sigs = set([x["signature"] for x in signatures[version]])
local_sigs = {x["signature"] for x in c["metadata"]["signatures"]}
manifest_sigs = {x["signature"] for x in signatures[version]}
assert local_sigs == manifest_sigs
else:
assert c["sign_state"] == "unsigned"
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/tests/integration/api/test_collection_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def flags(galaxy_client):
def namespace(galaxy_client):
# ensure namespace exists
gc = galaxy_client("admin")
existing = dict((x["name"], x) for x in get_all_namespaces(gc))
existing = {x["name"]: x for x in get_all_namespaces(gc)}
if NAMESPACE not in existing:
payload = {"name": NAMESPACE, "groups": []}
gc.post("v3/namespaces/", body=payload)
Expand Down
4 changes: 2 additions & 2 deletions galaxy_ng/tests/integration/api/test_ui_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def test_api_ui_v1_collection_versions_version_range(ansible_config, uncertified
ds = resp.json()

assert len(ds['data']) == 2
assert set([v["version"] for v in ds['data']]) == set([c1.version, c2.version])
assert {v["version"] for v in ds['data']} == {c1.version, c2.version}

# test range exclusive
resp = uclient.get(f'{v_path}&version_range=>{c1.version}')
Expand Down Expand Up @@ -943,7 +943,7 @@ def _populate_tags_cmd():
assert resp.json()["meta"]["count"] > 0

# test correct count sorting
tags = [tag for tag in uclient.get('_ui/v1/tags/roles').json()["data"]]
tags = uclient.get('_ui/v1/tags/roles').json()["data"]

assert sorted(tags, key=lambda r: r["count"], reverse=True)[:2] == resp.json()["data"][:2]
assert resp.json()["data"][0]["name"] == "docker"
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/tests/integration/api/test_ui_paths_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_gw_api_ui_v1_collection_versions_version_range(galaxy_client, uncertifi
# test range
ds = gc.get(f'{v_path}&version_range>={c1.version}')
assert len(ds['data']) == 2
assert set([v["version"] for v in ds['data']]) == set([c1.version, c2.version])
assert {v["version"] for v in ds['data']} == {c1.version, c2.version}

# test range exclusive
ds = gc.get(f'{v_path}&version_range=>{c1.version}')
Expand Down
4 changes: 2 additions & 2 deletions galaxy_ng/tests/integration/cli/test_community_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def test_community_collection_download_count_sync(ansible_config):

# configure the remote
resp = api_client.request('/api/pulp/api/v3/remotes/ansible/collection/')
remotes = dict((x['name'], x) for x in resp['results'])
remotes = {x['name']: x for x in resp['results']}
community_remote_config = {
'name': 'community',
'url': 'https://old-galaxy.ansible.com/',
Expand All @@ -71,7 +71,7 @@ def test_community_collection_download_count_sync(ansible_config):

# start the sync
resp = api_client.request('/api/pulp/api/v3/repositories/ansible/ansible/')
repos = dict((x['name'], x) for x in resp['results'])
repos = {x['name']: x for x in resp['results']}
sync_payload = {'mirror': False, 'optimize': False, 'remote': remotes['community']['pulp_href']}
sync_task = api_client.request(
repos['community']['pulp_href'] + 'sync/',
Expand Down
Loading

0 comments on commit 17bb2d9

Please sign in to comment.