Table validation wrapped in assert — silently skipped under python -O / PYTHONOPTIMIZE=1
Description
The call to _ensure_table_and_layer_exist_in_sdata(...) is wrapped in an assert statement (utils.py:~2554). Python's -O flag strips assert statements at compile time, so the entire validation function is never called in optimized mode. Passing a nonexistent table_name under python -O does not raise a clean ValueError; instead, the process crashes later with a cryptic AttributeError or KeyError deep in the rendering path. Docker-based deployments and some CI environments commonly use PYTHONOPTIMIZE=1.
User-input validation must never use assert. assert is documented by Python as an internal developer invariant check, explicitly removed under -O.
Environment
spatialdata-plot: 0.3.4.dev (main, 5cfedc7)
spatialdata: 0.5.0
Python: 3.13
Affected Code
# utils.py — current code (~line 2554)
assert _ensure_table_and_layer_exist_in_sdata(param_dict.get("sdata"), table_name, table_layer)
# Under python -O: this line is compiled away entirely — validation is silently bypassed
Running the render pipeline under python -O with table_name="nonexistent_table" does not raise ValueError: Table 'nonexistent_table' not found. The assert is stripped, validation is skipped, and the error surfaces later as an unrelated internal error with no useful message.
Expected vs. Actual
Expected: Passing an invalid table_name always raises a clear ValueError, regardless of interpreter optimization level.
Actual: Under python -O / PYTHONOPTIMIZE=1, the validation is silently skipped and the failure manifests as a cryptic internal error.
Fix Sketch
Replace the assert with an unconditional call. The function already raises ValueError internally when validation fails, so no additional error handling is needed:
# Instead of:
assert _ensure_table_and_layer_exist_in_sdata(sdata, table_name, table_layer)
# Use:
_ensure_table_and_layer_exist_in_sdata(sdata, table_name, table_layer)
Also audit the codebase for any other assert statements guarding user-input validation — this pattern may appear elsewhere.
Labels: bug, priority: medium
Triage tier: Tier 2
Table validation wrapped in
assert— silently skipped underpython -O/PYTHONOPTIMIZE=1Description
The call to
_ensure_table_and_layer_exist_in_sdata(...)is wrapped in anassertstatement (utils.py:~2554). Python's-Oflag stripsassertstatements at compile time, so the entire validation function is never called in optimized mode. Passing a nonexistenttable_nameunderpython -Odoes not raise a cleanValueError; instead, the process crashes later with a crypticAttributeErrororKeyErrordeep in the rendering path. Docker-based deployments and some CI environments commonly usePYTHONOPTIMIZE=1.User-input validation must never use
assert.assertis documented by Python as an internal developer invariant check, explicitly removed under-O.Environment
Affected Code
Running the render pipeline under
python -Owithtable_name="nonexistent_table"does not raiseValueError: Table 'nonexistent_table' not found. The assert is stripped, validation is skipped, and the error surfaces later as an unrelated internal error with no useful message.Expected vs. Actual
Expected: Passing an invalid
table_namealways raises a clearValueError, regardless of interpreter optimization level.Actual: Under
python -O/PYTHONOPTIMIZE=1, the validation is silently skipped and the failure manifests as a cryptic internal error.Fix Sketch
Replace the
assertwith an unconditional call. The function already raisesValueErrorinternally when validation fails, so no additional error handling is needed:Also audit the codebase for any other
assertstatements guarding user-input validation — this pattern may appear elsewhere.Labels: bug, priority: medium
Triage tier: Tier 2