You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2026/10/27 06:06:55 UTC
(superset) 01/01: refactor(utils): adopt remove_duplicates() instead of inline dict.fromkeys dedup
This is an automated email from the ASF dual-hosted git repository.
rusackas pushed a commit to branch refactor/adopt-remove-duplicates
in repository https://gitbox.apache.org/repos/asf/superset.git
commit 2b452e5c99a1eef9feaa570be8ef99b6dce48bd5
Author: Evan Rusackas <ev...@rusackas.com>
AuthorDate: Mon Oct 26 23:03:12 2026 -0700
refactor(utils): adopt remove_duplicates() instead of inline dict.fromkeys dedup
superset/utils/core.py's remove_duplicates() (with no key) is exactly the
list(dict.fromkeys(items)) order-preserving dedup idiom. Several call sites
reimplemented that expression inline; switch them to call the shared helper
instead.
Files changed:
- superset/utils/slack.py (parse_slack_recipient_targets)
- superset/mcp_service/chart/query_result.py (validate_query_result_envelope)
- superset/mcp_service/chart/chart_utils.py (_resolve_big_number_temporal_column,
analyze_chart_capabilities's bullet-viz branch; the list(set(...)) branch for
other viz types is intentionally left as-is, since it is not order-preserving
and converting it is out of scope)
- superset/common/form_data_query_context.py (_table_queries)
- superset/commands/deletion_retention/prune_audit.py (_delete_batch)
- superset/commands/tag/export.py (ExportTagsCommand)
- superset/sql/execution/executor.py (_records_to_dataframe)
Pure refactor, no behavior change. Skipped
superset/mcp_service/chart/validation/dataset_validator.py: the only dedup
there (_candidates_first, ~line 968) fuses dict.fromkeys with a membership
filter in the same comprehension, not a bare list(dict.fromkeys(items))
expression, so it isn't a drop-in replacement candidate.
Co-Authored-By: Evan Rusackas <ev...@rusackas.com>
Co-Authored-By: Claude Sonnet 5 <no...@anthropic.com>
---
superset/commands/deletion_retention/prune_audit.py | 3 ++-
superset/commands/tag/export.py | 5 +++--
superset/common/form_data_query_context.py | 10 ++++++++--
superset/mcp_service/chart/chart_utils.py | 6 +++---
superset/mcp_service/chart/query_result.py | 3 ++-
superset/sql/execution/executor.py | 2 +-
superset/utils/slack.py | 4 ++--
7 files changed, 21 insertions(+), 12 deletions(-)
diff --git a/superset/commands/deletion_retention/prune_audit.py b/superset/commands/deletion_retention/prune_audit.py
index 927f9eb6b90..5e1d154fe3a 100644
--- a/superset/commands/deletion_retention/prune_audit.py
+++ b/superset/commands/deletion_retention/prune_audit.py
@@ -129,6 +129,7 @@ from superset.models.purge_audit_log import (
STATUS_PENDING,
STATUS_TARGET_ABSENT,
)
+from superset.utils.core import remove_duplicates
logger: logging.Logger = logging.getLogger(__name__)
@@ -1051,7 +1052,7 @@ def _delete_batch(
# (sc-120493). Pairs are bounded by len(ids) <= MAX_BATCH_SIZE.
scope_entities: list[tuple[str, str | None]] | None = None
if needs_entity_scope:
- scope_entities = list(dict.fromkeys((row[1], row[2]) for row in hints))
+ scope_entities = remove_duplicates((row[1], row[2]) for row in hints)
db.session.rollback() # pylint: disable=consider-using-transaction
acquire_coordination_lock(db.session)
# Locked re-check as a SELECT, then a literal-id DELETE. With the lock held
diff --git a/superset/commands/tag/export.py b/superset/commands/tag/export.py
index 1d2ee6c43c4..1e2b1efabda 100644
--- a/superset/commands/tag/export.py
+++ b/superset/commands/tag/export.py
@@ -28,6 +28,7 @@ from superset.extensions import feature_flag_manager
from superset.tags.models import ObjectType, TagType
from superset.commands.export.models import ExportModelsCommand
from superset.commands.tag.exceptions import TagNotFoundError
+from superset.utils.core import remove_duplicates
class ExportTagsCommand(ExportModelsCommand):
@@ -73,8 +74,8 @@ class ExportTagsCommand(ExportModelsCommand):
elif tagged_object.object_type == ObjectType.chart:
chart_ids.append(tagged_object.object_id)
- dashboard_ids = list(dict.fromkeys(dashboard_ids))
- chart_ids = list(dict.fromkeys(chart_ids))
+ dashboard_ids = remove_duplicates(dashboard_ids)
+ chart_ids = remove_duplicates(chart_ids)
yield (
ExportTagsCommand._file_name(),
diff --git a/superset/common/form_data_query_context.py b/superset/common/form_data_query_context.py
index 54d6c29a7a7..4cd3b6d1a3f 100644
--- a/superset/common/form_data_query_context.py
+++ b/superset/common/form_data_query_context.py
@@ -43,7 +43,13 @@ from collections.abc import Mapping
from typing import Any
from superset.utils import json
-from superset.utils.core import as_list, DTTM_ALIAS, get_column_name, get_metric_name
+from superset.utils.core import (
+ as_list,
+ DTTM_ALIAS,
+ get_column_name,
+ get_metric_name,
+ remove_duplicates,
+)
# Keep this mapping identical to ``queryFieldAliases`` in
# ``superset-ui-core/src/query/extractQueryFields.ts``. It is the shared
@@ -1489,7 +1495,7 @@ def _table_queries( # noqa: C901
else [metric_label]
)
]
- labels = list(dict.fromkeys(labels))
+ labels = remove_duplicates(labels)
contribution = {
"operation": "contribution",
"options": {
diff --git a/superset/mcp_service/chart/chart_utils.py b/superset/mcp_service/chart/chart_utils.py
index 668f01b1ed2..645a0b5c2dd 100644
--- a/superset/mcp_service/chart/chart_utils.py
+++ b/superset/mcp_service/chart/chart_utils.py
@@ -75,7 +75,7 @@ from superset.mcp_service.chart.validation.dataset_validator import (
from superset.mcp_service.common.error_schemas import DatasetContext
from superset.mcp_service.utils.url_utils import get_superset_base_url
from superset.utils import json
-from superset.utils.core import DatasourceType, FilterOperator
+from superset.utils.core import DatasourceType, FilterOperator, remove_duplicates
logger = logging.getLogger(__name__)
@@ -3094,7 +3094,7 @@ def _resolve_big_number_temporal_column(
candidates.extend(
column.column_name for column in dataset.columns if column.column_name
)
- for temporal_column in dict.fromkeys(candidates):
+ for temporal_column in remove_duplicates(candidates):
if is_column_truly_temporal(temporal_column, dataset_id, dataset=dataset):
return temporal_column
return None
@@ -4363,7 +4363,7 @@ def analyze_chart_capabilities( # noqa: C901
supports_export=True, # All charts can be exported
optimal_formats=optimal_formats,
data_types=(
- list(dict.fromkeys(data_types))
+ remove_duplicates(data_types)
if viz_type == "bullet"
else list(set(data_types))
),
diff --git a/superset/mcp_service/chart/query_result.py b/superset/mcp_service/chart/query_result.py
index 975b976350e..5905ad265a1 100644
--- a/superset/mcp_service/chart/query_result.py
+++ b/superset/mcp_service/chart/query_result.py
@@ -48,6 +48,7 @@ from superset.utils.core import (
ExtraFiltersReasonType,
ExtraFiltersTimeColumnType,
GenericDataType,
+ remove_duplicates,
)
from superset.utils.dates import datetime_to_epoch, EPOCH
@@ -1715,7 +1716,7 @@ def validate_query_result_envelope( # noqa: C901
dict.__getitem__(query, "colnames") if "colnames" in query else None
)
declared_row_columns = (
- list(dict.fromkeys(declared_columns))
+ remove_duplicates(declared_columns)
if declared_columns is not None
else None
)
diff --git a/superset/sql/execution/executor.py b/superset/sql/execution/executor.py
index 12bb0883aa7..b2ba9c31705 100644
--- a/superset/sql/execution/executor.py
+++ b/superset/sql/execution/executor.py
@@ -506,7 +506,7 @@ def _records_to_dataframe(
return pd.DataFrame(records, columns=columns or None)
if not columns:
- columns = list(dict.fromkeys(name for row in records for name in row))
+ columns = utils.remove_duplicates(name for row in records for name in row)
if not columns:
return pd.DataFrame(records)
diff --git a/superset/utils/slack.py b/superset/utils/slack.py
index 2d3ca73c8f5..c2e50686d71 100644
--- a/superset/utils/slack.py
+++ b/superset/utils/slack.py
@@ -45,7 +45,7 @@ from superset.extensions import cache_manager
from superset.extensions.metastore_cache import SupersetMetastoreCache
from superset.reports.schemas import SlackChannelSchema
from superset.utils.backports import StrEnum
-from superset.utils.core import recipients_string_to_list
+from superset.utils.core import recipients_string_to_list, remove_duplicates
logger = logging.getLogger(__name__)
@@ -158,7 +158,7 @@ NO_SLACK_RECIPIENTS_MESSAGE = "No recipients saved in the report"
def parse_slack_recipient_targets(target: str) -> list[str]:
"""Parse Slack targets, removing duplicates while preserving their order."""
- return list(dict.fromkeys(recipients_string_to_list(target)))
+ return remove_duplicates(recipients_string_to_list(target))
def get_slack_api_error_data(ex: SlackApiError) -> dict[str, Any]: