Skip to content

Commit ece8b8b

Browse files
eschuthoclaude
andauthored
fix(import): avoid UnboundLocalError when load_yaml fails during load_configs (SC-121288) (#44390)
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 3f7d01a commit ece8b8b

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

‎superset/commands/importers/v1/utils.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ def load_configs(
196196
prefix = file_name.split("/")[0]
197197
schema = schemas.get(f"{prefix}/")
198198
if schema:
199+
# Bind ``config`` up front so the ``except ValidationError``
200+
# diagnostic below stays valid even when ``load_yaml`` raises
201+
# before the assignment completes (unparseable YAML).
202+
config: Any = None
199203
try:
200204
config = load_yaml(file_name, content)
201205
if not isinstance(config, dict):

‎tests/unit_tests/commands/importers/v1/utils_test.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,40 @@ def test_top_level_scalar_is_reported_as_validation_error(
510510
assert len(exceptions) == 1
511511
assert "databases/malformed.yaml" in exceptions[0].messages
512512

513+
def test_unparseable_yaml_is_reported_as_validation_error(
514+
self, session: Session
515+
) -> None:
516+
"""YAML that ``load_yaml`` cannot parse raises a ValidationError
517+
*before* ``config`` is assigned. The ``except ValidationError``
518+
handler's ``isinstance(config, dict)`` diagnostic must not then crash
519+
with an UnboundLocalError (regression for SUPERSET-PYTHON-176W)."""
520+
from superset.commands.importers.v1.utils import load_configs
521+
from superset.databases.schemas import ImportV1DatabaseSchema
522+
from superset.models.core import Database
523+
524+
engine = session.get_bind()
525+
Database.metadata.create_all(engine) # pylint: disable=no-member
526+
527+
# An unterminated quoted scalar raises yaml.scanner.ScannerError,
528+
# mirroring the malformed chart YAML seen in the Sentry report.
529+
contents = {"databases/malformed.yaml": 'key: "unterminated string'}
530+
exceptions: list[ValidationError] = []
531+
532+
configs = load_configs(
533+
contents,
534+
{"databases/": ImportV1DatabaseSchema()},
535+
{},
536+
exceptions,
537+
{},
538+
{},
539+
{},
540+
{},
541+
)
542+
543+
assert configs == {}
544+
assert len(exceptions) == 1
545+
assert "databases/malformed.yaml" in exceptions[0].messages
546+
513547

514548
class TestDatabaseConnectionIdentityUnchanged:
515549
"""Stored database secrets (password, SSH tunnel key) may only be

0 commit comments

Comments
 (0)