Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(postgres): stop optional config unions retaining unparseable dicts (#67169)

Tom Owers81de25dd7467
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/config.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/config.py
index 11aeee13b6d1..ecc24006f594 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/config.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/config.py
@@ -245,6 +245,14 @@ def to_config(
for config_type in config_types:
if not is_config(config_type):
+ if config_type is type(None):
+ # The `None` arm of an `Optional[Config]` union must not claim the
+ # value meant for the config arm. When the config arm fails to parse
+ # a present mapping, falling through to here would assign the raw
+ # dict, leaving a typed field holding an untyped dict and crashing
+ # downstream (e.g. `config.ssh_tunnel.enabled`). Skip so the field
+ # falls back to its default instead.
+ continue
try:
value = d[field_key]
except KeyError:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_config.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_config.py
index 784deffbf98c..eb00fd2c3818 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_config.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test/test_config.py
@@ -486,6 +486,24 @@ def test_from_dict_with_non_mapping_raises_clear_error(bad_input):
SourceConfig.from_dict(bad_input)
+def test_to_config_optional_config_does_not_retain_unparseable_dict():
+ # A present-but-unparseable mapping under an `Optional[Config]` field must not leak
+ # through the `None` arm of the union as a raw dict. Doing so left a typed field
+ # holding an untyped dict and crashed downstream with `'dict' object has no
+ # attribute '<field>'`; the field must fall back to its default (None) instead.
+ @config.config
+ class AuthConfig:
+ type: str # required, so a payload that can't fill it makes the config arm fail
+
+ @config.config
+ class SourceConfig(config.Config):
+ auth: AuthConfig | None = None
+
+ cfg = SourceConfig.from_dict({"auth": {"unrecognized_key": "value"}})
+
+ assert cfg.auth is None
+
+
@config.config
class _SecretFieldConfig(config.Config):
password: str | None = None