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(data-imports): handle numeric input in str_to_optional_int (#63819)

Tom Owerse7fe2b684742
diff --git a/posthog/temporal/data_imports/sources/common/config.py b/posthog/temporal/data_imports/sources/common/config.py
index f3c9d40e669d..f4bbdf09f04d 100644
--- a/posthog/temporal/data_imports/sources/common/config.py
+++ b/posthog/temporal/data_imports/sources/common/config.py
@@ -581,11 +581,14 @@ def str_to_int(s: str | int) -> int:
return int(s)
-def str_to_optional_int(s: str | int | None) -> int | None:
+def str_to_optional_int(s: str | int | float | None) -> int | None:
"""A converter to return a str to optional int."""
- if isinstance(s, int):
- return s
- elif s is None or s.strip() == "":
+ if s is None:
+ return None
+ elif isinstance(s, int | float):
+ # Covers ints, bools, and floats (e.g. numeric JSON values from the frontend).
+ return int(s)
+ elif s.strip() == "":
return None
else:
return int(s)
diff --git a/posthog/temporal/data_imports/sources/common/test/test_config.py b/posthog/temporal/data_imports/sources/common/test/test_config.py
index 5eb225820017..494a1e0f3544 100644
--- a/posthog/temporal/data_imports/sources/common/test/test_config.py
+++ b/posthog/temporal/data_imports/sources/common/test/test_config.py
@@ -59,6 +59,44 @@ def test_basic_to_config_converters():
assert cfg.c is False
+ "value,expected",
+ [
+ (None, None),
+ ("", None),
+ (" ", None),
+ ("5432", 5432),
+ (5432, 5432),
+ # Numeric JSON values arrive as int/float, not str, and must not crash on `.strip()`.
+ (5432.0, 5432),
+ (158.220123243, 158),
+ ],
+)
+def test_str_to_optional_int(value, expected):
+ """`str_to_optional_int` must handle numeric (int/float) inputs, not only strings."""
+ assert config.str_to_optional_int(value) == expected
+
+
+ "value,expected",
+ [
+ (None, None),
+ ("5432", 5432),
+ (5432.0, 5432),
+ ],
+)
+def test_optional_int_converter_handles_numeric_via_from_dict(value, expected):
+ """A NUMBER field fed through `Config.from_dict` must not crash and must coerce correctly."""
+
+ @config.config
+ class TestConfig(config.Config):
+ port: int | None = config.value(converter=config.str_to_optional_int, default_factory=lambda: None)
+
+ cfg = TestConfig.from_dict({"port": value})
+
+ assert cfg.port == expected
+
+
def test_nested_to_config_with_flat_dict():
"""Test `config.to_config` with a nested set of classes.