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:59 UTC

(superset) 01/01: refactor(utils): add is_truthy_str() helper, dedupe repeated boolean-flag parsing

This is an automated email from the ASF dual-hosted git repository.

rusackas pushed a commit to branch refactor/add-is-truthy-str-util
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 361a2e8ca6e63bb7925dae8ef624dfd9f45b157f
Author: Evan Rusackas <ev...@rusackas.com>
AuthorDate: Mon Oct 26 23:03:14 2026 -0700

    refactor(utils): add is_truthy_str() helper, dedupe repeated boolean-flag parsing
    
    Nine call sites across extensions/storage/api.py, dashboards/api.py, and
    databend.py hand-rolled the identical strict pattern
    `value.lower() == "true"`. superset/utils/core.py already has
    parse_boolean_string(), but it additionally treats "yes"/"y"/"t"/"on"/"1"
    as true, so swapping it in would silently widen what each call site
    accepts (e.g. ?shared=1 would newly become truthy). Add a new, narrower
    is_truthy_str() helper that preserves the exact existing semantics
    (None -> default, otherwise strict case-insensitive "true" match only)
    and adopt it at all nine sites without changing behavior.
    
    Co-Authored-By: Evan Rusackas <ev...@rusackas.com>
    Co-Authored-By: Claude Sonnet 5 <no...@anthropic.com>
---
 superset/dashboards/api.py           |  3 ++-
 superset/db_engine_specs/databend.py |  6 ++----
 superset/extensions/storage/api.py   | 15 ++++++++-------
 superset/utils/core.py               | 35 +++++++++++++++++++++++++++++++++++
 tests/unit_tests/utils/test_core.py  | 28 ++++++++++++++++++++++++++++
 5 files changed, 75 insertions(+), 12 deletions(-)

diff --git a/superset/dashboards/api.py b/superset/dashboards/api.py
index e2335c2d868..54c19a31c7f 100644
--- a/superset/dashboards/api.py
+++ b/superset/dashboards/api.py
@@ -213,6 +213,7 @@ from superset.tasks.thumbnails import (
 from superset.tasks.utils import get_current_user
 from superset.utils.core import (
     get_user_id,
+    is_truthy_str,
     parse_boolean_string,
     send_export_zip,
     write_zip_entry,
@@ -1826,7 +1827,7 @@ class DashboardRestApi(
             return self.response_403()
 
         # Get optional query params
-        export_data = request.args.get("export_data", "true").lower() == "true"
+        export_data = is_truthy_str(request.args.get("export_data", "true"))
         sample_rows = request.args.get("sample_rows", type=int)
 
         # Build ZIP from command output
diff --git a/superset/db_engine_specs/databend.py b/superset/db_engine_specs/databend.py
index 016822ae08c..f42e1ab4aac 100644
--- a/superset/db_engine_specs/databend.py
+++ b/superset/db_engine_specs/databend.py
@@ -39,7 +39,7 @@ from superset.db_engine_specs.base import (
 )
 from superset.db_engine_specs.exceptions import SupersetDBAPIDatabaseError
 from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
-from superset.utils.core import GenericDataType
+from superset.utils.core import GenericDataType, is_truthy_str
 from superset.utils.hashing import hash_from_str
 from superset.utils.network import is_hostname_valid, is_port_open
 
@@ -308,9 +308,7 @@ class DatabendEngineSpec(BasicParametersMixin, DatabendBaseEngineSpec):
         """
         if sslmode is not None:
             return sslmode.lower() in cls.encryption_sslmodes
-        if secure is not None:
-            return secure.lower() == "true"
-        return False
+        return is_truthy_str(secure)
 
     @classmethod
     def get_parameters_from_uri(
diff --git a/superset/extensions/storage/api.py b/superset/extensions/storage/api.py
index 8bee0e55a36..956e3821fb3 100644
--- a/superset/extensions/storage/api.py
+++ b/superset/extensions/storage/api.py
@@ -49,6 +49,7 @@ from superset.extensions.storage.persistent_dao import (
 )
 from superset.extensions.storage.utils import get_extension_or_404, parse_ttl
 from superset.key_value.exceptions import KeyValueCodecEncodeException
+from superset.utils.core import is_truthy_str
 from superset.utils.decorators import transaction
 from superset.views.base_api import BaseSupersetApi
 
@@ -187,7 +188,7 @@ class ExtensionStorageRestApi(BaseSupersetApi):
         if not extension:
             return self.response_404("Extension not found")
 
-        shared = request.args.get("shared", "false").lower() == "true"
+        shared = is_truthy_str(request.args.get("shared", "false"))
         raw = ExtensionEphemeralDAO.get_raw(extension_id, key, shared=shared)
         if raw is None:
             return self.response(200, result=None)
@@ -299,7 +300,7 @@ class ExtensionStorageRestApi(BaseSupersetApi):
         if error:
             return self.response_400(error)
 
-        shared = request.args.get("shared", "false").lower() == "true"
+        shared = is_truthy_str(request.args.get("shared", "false"))
         try:
             ExtensionEphemeralDAO.set(
                 extension_id, key, value, ttl, codec=codec, shared=shared
@@ -359,7 +360,7 @@ class ExtensionStorageRestApi(BaseSupersetApi):
         if not extension:
             return self.response_404("Extension not found")
 
-        shared = request.args.get("shared", "false").lower() == "true"
+        shared = is_truthy_str(request.args.get("shared", "false"))
         ExtensionEphemeralDAO.delete(extension_id, key, shared=shared)
 
         return self.response(200, message="Value deleted successfully")
@@ -460,7 +461,7 @@ class ExtensionStorageRestApi(BaseSupersetApi):
         if not extension:
             return self.response_404("Extension not found")
 
-        shared = request.args.get("shared", "false").lower() == "true"
+        shared = is_truthy_str(request.args.get("shared", "false"))
         user_fk = None if shared else g.user.id
         resource_type = request.args.get("resource_type")
         resource_uuid = request.args.get("resource_uuid")
@@ -565,7 +566,7 @@ class ExtensionStorageRestApi(BaseSupersetApi):
         if not extension:
             return self.response_404("Extension not found")
 
-        shared = request.args.get("shared", "false").lower() == "true"
+        shared = is_truthy_str(request.args.get("shared", "false"))
         user_fk = None if shared else g.user.id
         entry = ExtensionStorageDAO.get(extension_id, key, user_fk=user_fk)
         if entry is None:
@@ -671,7 +672,7 @@ class ExtensionStorageRestApi(BaseSupersetApi):
 
         is_binary = bool(body.get("isBinary", False))
         encrypt = bool(body.get("encrypt", False))
-        shared = request.args.get("shared", "false").lower() == "true"
+        shared = is_truthy_str(request.args.get("shared", "false"))
         user_fk = None if shared else g.user.id
         try:
             wire_value = _wire_value_for_request(body["value"], is_binary)
@@ -750,7 +751,7 @@ class ExtensionStorageRestApi(BaseSupersetApi):
         if not extension:
             return self.response_404("Extension not found")
 
-        shared = request.args.get("shared", "false").lower() == "true"
+        shared = is_truthy_str(request.args.get("shared", "false"))
         user_fk = None if shared else g.user.id
         ExtensionStorageDAO.delete_by_key(extension_id, key, user_fk=user_fk)
 
diff --git a/superset/utils/core.py b/superset/utils/core.py
index 08fac34b6a5..f1a74c87d33 100644
--- a/superset/utils/core.py
+++ b/superset/utils/core.py
@@ -2245,6 +2245,41 @@ def parse_boolean_string(bool_str: str | None) -> bool:
     return bool_str.lower() in ("y", "Y", "yes", "True", "t", "true", "On", "on", "1")
 
 
+def is_truthy_str(value: str | None, default: bool = False) -> bool:
+    """
+    Case-insensitive match against the literal string "true", nothing else.
+
+    Unlike `parse_boolean_string`, this does not treat "yes"/"y"/"t"/"on"/"1"
+    as true -- it exists to de-duplicate several call sites that already had
+    this exact narrow semantics (e.g. `value.lower() == "true"`), without
+    silently widening what they accept.
+
+    >>> is_truthy_str(None)
+    False
+    >>> is_truthy_str(None, default=True)
+    True
+    >>> is_truthy_str('true')
+    True
+    >>> is_truthy_str('True')
+    True
+    >>> is_truthy_str('TRUE')
+    True
+    >>> is_truthy_str('false')
+    False
+    >>> is_truthy_str('1')
+    False
+    >>> is_truthy_str('yes')
+    False
+
+    :param value: string representation of a value assumed to be boolean
+    :param default: value to return when `value` is `None`
+    :return: strictly parsed boolean value
+    """
+    if value is None:
+        return default
+    return value.strip().lower() == "true"
+
+
 def apply_max_row_limit(
     limit: int,
     server_pagination: bool | None = None,
diff --git a/tests/unit_tests/utils/test_core.py b/tests/unit_tests/utils/test_core.py
index d7b849dd5cc..93480d4b56e 100644
--- a/tests/unit_tests/utils/test_core.py
+++ b/tests/unit_tests/utils/test_core.py
@@ -43,6 +43,7 @@ from superset.utils.core import (
     get_stacktrace,
     get_user_agent,
     is_test,
+    is_truthy_str,
     markdown,
     merge_extra_filters,
     merge_extra_form_data,
@@ -210,6 +211,33 @@ def test_parse_boolean_string(test_input: Optional[str], expected: bool):
     assert parse_boolean_string(test_input) == expected
 
 
+@pytest.mark.parametrize(
+    "test_input,expected",
+    [
+        ("true", True),
+        ("True", True),
+        ("TRUE", True),
+        ("false", False),
+        ("False", False),
+        ("foo", False),
+        # Unlike `parse_boolean_string`, these must NOT be treated as true.
+        ("1", False),
+        ("yes", False),
+        ("y", False),
+        ("t", False),
+        ("on", False),
+    ],
+)
+def test_is_truthy_str(test_input: Optional[str], expected: bool):
+    assert is_truthy_str(test_input) is expected
+
+
+def test_is_truthy_str_none_uses_default():
+    assert is_truthy_str(None) is False
+    assert is_truthy_str(None, default=False) is False
+    assert is_truthy_str(None, default=True) is True
+
+
 def test_int_values():
     assert cast_to_boolean(1) is True
     assert cast_to_boolean(0) is False