Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle TOML parsing errors in config reader #3671

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion marimo/_utils/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def read_toml(self, cls: Type[T], *, fallback: T) -> T:
with open(self.filepath, "r") as file:
data = tomlkit.parse(file.read())
return parse_raw(data, cls, allow_unknown_keys=True)
except FileNotFoundError:
except (FileNotFoundError, tomlkit.exceptions.TOMLKitError):
return fallback

def write_toml(self, data: Any) -> None:
Expand Down
Empty file added tests/_utils/config/__init__.py
Empty file.
40 changes: 40 additions & 0 deletions tests/_utils/config/test_config_reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations

import os
from dataclasses import dataclass
from tempfile import NamedTemporaryFile

from marimo._utils.config.config import ConfigReader


@dataclass
class TestConfig:
value: str


def test_read_toml_invalid_syntax() -> None:
with NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f:
# Write invalid TOML content (missing value after comma)
f.write("key = 'value',")
f.flush()

reader = ConfigReader(f.name)
fallback = TestConfig(value="fallback")
result = reader.read_toml(TestConfig, fallback=fallback)
assert result == fallback

os.unlink(f.name)


def test_read_toml_valid() -> None:
with NamedTemporaryFile(mode="w", suffix=".toml", delete=False) as f:
f.write('value = "test"')
f.flush()

reader = ConfigReader(f.name)
fallback = TestConfig(value="fallback")
result = reader.read_toml(TestConfig, fallback=fallback)
assert result == TestConfig(value="test")

os.unlink(f.name)
Loading