Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: preserve leading underscores in to_camel_case (#10170)

eeshsaxenae10df08aa92d
diff --git a/marimo/_utils/case.py b/marimo/_utils/case.py
index 636c02080a..89e4a91b3d 100644
--- a/marimo/_utils/case.py
+++ b/marimo/_utils/case.py
@@ -13,8 +13,19 @@ def to_camel_case(snake_str: str) -> str:
if "_" not in snake_str:
return snake_str
- pascal_case = "".join(x.capitalize() for x in snake_str.lower().split("_"))
- return snake_str[0].lower() + pascal_case[1:]
+ # Preserve leading underscores (e.g. "_private_key" -> "_privateKey").
+ # Building the camelCase name from `snake_str[0] + pascal_case[1:]` assumed
+ # the first character was the start of the first word; for a leading
+ # underscore it dropped the real first letter ("_foo" -> "_oo").
+ stripped = snake_str.lstrip("_")
+ leading_underscores = snake_str[: len(snake_str) - len(stripped)]
+
+ pascal_case = "".join(x.capitalize() for x in stripped.lower().split("_"))
+ if not pascal_case:
+ # snake_str was all underscores; leave it unchanged.
+ return snake_str
+ camel_case = pascal_case[0].lower() + pascal_case[1:]
+ return leading_underscores + camel_case
def to_snake_case(string: str) -> str:
diff --git a/tests/_utils/test_case.py b/tests/_utils/test_case.py
index 29a85e28d1..aa4e9cd7e6 100644
--- a/tests/_utils/test_case.py
+++ b/tests/_utils/test_case.py
@@ -16,6 +16,11 @@ def test_camel_case() -> None:
assert to_camel_case("") == ""
assert to_camel_case("alreadyCamelCase") == "alreadyCamelCase"
assert to_camel_case("With_Some_CAPS") == "withSomeCaps"
+ # Leading underscores are preserved and the first real letter is kept.
+ assert to_camel_case("_private_key") == "_privateKey"
+ assert to_camel_case("_id") == "_id"
+ assert to_camel_case("__dunder_name") == "__dunderName"
+ assert to_camel_case("_") == "_"
def test_deep_to_camel_case() -> None: