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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Add fallback parameter to QueryParams and CLIArgs .get() method (#3479)
devin-ai-integration[bot]03bc4b17023b
diff --git a/marimo/_runtime/params.py b/marimo/_runtime/params.pyindex 895f1c89bc..9f52e992a1 100644--- a/marimo/_runtime/params.py+++ b/marimo/_runtime/params.py@@ -1,7 +1,7 @@# Copyright 2024 Marimo. All rights reserved.from __future__ import annotations-from typing import Dict, Iterator, List, Optional, Union+from typing import Dict, Iterator, List, Optional, TypeVar, Union, overloadfrom marimo._messaging.mimetypes import KnownMimeTypefrom marimo._messaging.ops import (@@ -37,13 +37,29 @@ class QueryParams(State[SerializedQueryParams]):self._params = paramsself._stream = stream- def get(self, key: str) -> Optional[Union[str, List[str]]]:+ T = TypeVar("T", str, List[str])++ @overload+ def get(self, key: str) -> Optional[Union[str, List[str]]]: ...++ @overload+ def get(self, key: str, default: T) -> Union[str, List[str], T]: ...++ def get(+ self, key: str, default: Optional[Union[str, List[str]]] = None+ ) -> Optional[Union[str, List[str]]]:"""Get the value of the query parameter.- Returns a str if there is only one item, a list of str otherwise.+ Args:+ key: The key to look up+ default: Value to return if key is not found++ Returns:+ A str if there is only one item, a list of str otherwise.+ If key is not found and default is provided, returns default."""if key not in self._params:- return None+ return defaultreturn self._params[key]def get_all(self, key: str) -> List[str]:@@ -153,14 +169,32 @@ class CLIArgs:):self._params = params- def get(self, key: str) -> Optional[ListOrValue[Primitive]]:+ T = TypeVar("T", Primitive, List[Primitive])++ @overload+ def get(self, key: str) -> Optional[ListOrValue[Primitive]]: ...++ @overload+ def get(+ self, key: str, default: T+ ) -> Union[ListOrValue[Primitive], T]: ...++ def get(+ self, key: str, default: Optional[ListOrValue[Primitive]] = None+ ) -> Optional[ListOrValue[Primitive]]:"""Get the value of the CLI arg.- Returns a singleton value if there is only one item,- a list of values otherwise.+ Args:+ key: The key to look up+ default: Value to return if key is not found++ Returns:+ A singleton value if there is only one item,+ a list of values otherwise.+ If key is not found and default is provided, returns default."""if key not in self._params:- return None+ return defaultreturn self._params[key]def get_all(self, key: str) -> List[Primitive]:diff --git a/tests/_runtime/test_query_params.py b/tests/_runtime/test_query_params.pyindex 35c7aaf39e..5213a40cc6 100644--- a/tests/_runtime/test_query_params.py+++ b/tests/_runtime/test_query_params.py@@ -20,6 +20,13 @@ class TestQueryParams(unittest.TestCase):def test_get(self) -> None:assert self.params.get("key1") == "value1"assert self.params.get("key2") == ["value2", "value3"]+ # Test with fallback parameter+ assert self.params.get("non_existent", "fallback") == "fallback"+ assert self.params.get("non_existent", ["fallback"]) == ["fallback"]+ assert self.params.get("non_existent", None) is None+ # Test existing keys with fallback (should return original value)+ assert self.params.get("key1", "fallback") == "value1"+ assert self.params.get("key2", "fallback") == ["value2", "value3"]def test_get_all(self) -> None:assert self.params.get_all("key1") == ["value1"]@@ -195,6 +202,13 @@ class TestCLIArgs(unittest.TestCase):def test_get(self):assert self.params.get("key1") == "value1"assert self.params.get("key2") == ["value2", "value3"]+ # Test with fallback parameter+ assert self.params.get("non_existent", "fallback") == "fallback"+ assert self.params.get("non_existent", ["fallback"]) == ["fallback"]+ assert self.params.get("non_existent", None) is None+ # Test existing keys with fallback (should return original value)+ assert self.params.get("key1", "fallback") == "value1"+ assert self.params.get("key2", "fallback") == ["value2", "value3"]def test_get_all(self):assert self.params.get_all("key1") == ["value1"]