-
Notifications
You must be signed in to change notification settings - Fork 1
feat: added stringify_unsupported() #203
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
Open
SiddhantSadangi
wants to merge
17
commits into
main
Choose a base branch
from
ss/stringify_unsupported
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1564698
feat: added stringify_unsupported()
SiddhantSadangi 91b29b6
tests: updated docstrings for doctests
SiddhantSadangi f1a61c0
tests: removed example from docstrings
SiddhantSadangi a08a8e6
Merge branch 'main' into ss/stringify_unsupported
SiddhantSadangi b0c1742
chore: applied code review suggestions
SiddhantSadangi 2fb1ed7
Merge branch 'ss/stringify_unsupported' of https://github.com/neptune…
SiddhantSadangi 1170483
chore: removed unecessary `noqa` directive
SiddhantSadangi eff02c6
Update src/neptune_scale/utils.py
SiddhantSadangi 503b823
Revert "Update src/neptune_scale/utils.py"
SiddhantSadangi a36cdac
feat: expanded to all collections
SiddhantSadangi add2fcb
tests: updated tests
SiddhantSadangi 004e4fb
Merge branch 'main' into ss/stringify_unsupported
SiddhantSadangi fdfb2ce
Code review suggestions
SiddhantSadangi 1bf23aa
Merge branch 'ss/stringify_unsupported' of https://github.com/neptune…
SiddhantSadangi b58e6a3
Merge branch 'main' into ss/stringify_unsupported
SiddhantSadangi 87f83da
Merge branch 'main' into ss/stringify_unsupported
SiddhantSadangi 07ed540
Update src/neptune_scale/utils.py
SiddhantSadangi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from datetime import datetime | ||
from typing import Any | ||
|
||
|
||
def stringify_unsupported(d: dict[str, Any], **kwargs: Any) -> dict[str, Any]: | ||
""" | ||
A helper function that flattens a nested dictionary structure and casts unsupported values to strings to be logged in Neptune. | ||
Note: | ||
- Collections (list, set, tuple) are converted to strings. | ||
- None values are ignored. | ||
|
||
Args: | ||
d: Dictionary to flatten | ||
**kwargs: Additional arguments for backward compatibility | ||
|
||
Returns: | ||
dict: Flattened dictionary with string keys and cast values | ||
|
||
For more details, see https://docs.neptune.ai/api/utils/#stringify_unsupported | ||
SiddhantSadangi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
if not isinstance(d, dict): | ||
raise TypeError("Input must be a dictionary") | ||
|
||
allowed_datatypes = [int, float, str, datetime, bool, list, set, tuple] | ||
|
||
flattened = {} | ||
|
||
def _stringify_unsupported(d: dict[str, Any], prefix: str = "") -> None: | ||
for key, value in d.items(): | ||
new_key = f"{prefix}/{key}" if prefix else key | ||
if isinstance(value, dict): | ||
_stringify_unsupported(d=value, prefix=new_key) | ||
elif isinstance(value, (list, set, tuple)): | ||
flattened[new_key] = str(value) | ||
elif type(value) in allowed_datatypes: | ||
SiddhantSadangi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
flattened[new_key] = value | ||
elif value is not None: | ||
flattened[new_key] = str(value) | ||
|
||
_stringify_unsupported(d) | ||
return flattened |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
from datetime import datetime | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
from neptune_scale.utils import stringify_unsupported | ||
|
||
|
||
def test_stringify_unsupported_basic_types(): | ||
"""Test stringify_unsupported with basic Python types.""" | ||
# Test with basic types | ||
test_cases: dict[str, Any] = { | ||
"string": "hello", | ||
"integer": 42, | ||
"float": 3.14, | ||
"boolean": True, | ||
"none_value": None, | ||
"datetime": datetime.now(), | ||
} | ||
|
||
result = stringify_unsupported(test_cases) | ||
|
||
assert result["string"] == "hello" | ||
assert result["integer"] == 42 | ||
assert result["float"] == 3.14 | ||
assert result["boolean"] is True | ||
assert "none_value" not in result | ||
assert isinstance(result["datetime"], datetime) | ||
|
||
|
||
def test_stringify_unsupported_collections(): | ||
"""Test stringify_unsupported with collection types.""" | ||
test_cases = {"list": ["a", "b", "c"], "tuple": (1, 2, 3), "set": {1, 2, 3}} | ||
|
||
result = stringify_unsupported(test_cases) | ||
|
||
assert result["list"] == "['a', 'b', 'c']" | ||
assert result["tuple"] == "(1, 2, 3)" | ||
assert result["set"] == "{1, 2, 3}" | ||
|
||
|
||
def test_stringify_unsupported_nested(): | ||
"""Test stringify_unsupported with nested dictionaries.""" | ||
test_cases = {"top": {"middle": {"bottom": "value"}}} | ||
|
||
result = stringify_unsupported(test_cases) | ||
|
||
assert result["top/middle/bottom"] == "value" | ||
|
||
|
||
def test_stringify_unsupported_mixed_types(): | ||
"""Test stringify_unsupported with mixed types in collections.""" | ||
test_cases = { | ||
"mixed_list": [1, "two", 3.0, True, None], | ||
"mixed_dict": {"str": "value", "int": 123, "list": [1, 2, 3], "none": None}, | ||
} | ||
|
||
result = stringify_unsupported(test_cases) | ||
|
||
assert result["mixed_list"] == "[1, 'two', 3.0, True, None]" | ||
assert result["mixed_dict/str"] == "value" | ||
assert result["mixed_dict/int"] == 123 | ||
assert result["mixed_dict/list"] == "[1, 2, 3]" | ||
assert "mixed_dict/none" not in result | ||
|
||
|
||
def test_stringify_unsupported_edge_cases(): | ||
"""Test stringify_unsupported with edge cases.""" | ||
test_cases = {"empty_list": [], "empty_dict": {}, "empty_string": "", "zero": 0, "false": False} | ||
|
||
result = stringify_unsupported(test_cases) | ||
|
||
assert result["empty_list"] == "[]" | ||
assert "empty_dict" not in result | ||
assert result["empty_string"] == "" | ||
assert result["zero"] == 0 | ||
assert result["false"] is False | ||
|
||
|
||
def test_stringify_unsupported_complex_nested(): | ||
"""Test stringify_unsupported with complex nested structures.""" | ||
test_cases = { | ||
"complex": { | ||
"list_of_dicts": [{"id": 1, "name": "one"}, {"id": 2, "name": "two"}], | ||
"mixed_nested": {"list": [1, {"a": 2}, 3, None], "dict": {"a": [1, 2], "b": {"c": 3}}}, | ||
} | ||
} | ||
|
||
result = stringify_unsupported(test_cases) | ||
|
||
assert result["complex/list_of_dicts"] == "[{'id': 1, 'name': 'one'}, {'id': 2, 'name': 'two'}]" | ||
assert result["complex/mixed_nested/list"] == "[1, {'a': 2}, 3, None]" | ||
assert result["complex/mixed_nested/dict/a"] == "[1, 2]" | ||
assert result["complex/mixed_nested/dict/b/c"] == 3 | ||
|
||
|
||
def test_stringify_unsupported_custom_objects(): | ||
"""Test stringify_unsupported with custom objects.""" | ||
|
||
class CustomObject: | ||
def __str__(self): | ||
return "custom_object" | ||
|
||
test_cases = {"custom": CustomObject()} | ||
|
||
result = stringify_unsupported(test_cases) | ||
|
||
assert result["custom"] == "custom_object" | ||
|
||
|
||
def test_stringify_unsupported_none_values(): | ||
"""Test stringify_unsupported with None values in collections.""" | ||
test_cases = {"list_with_none": [1, None, 3], "dict_with_none": {"a": 1, "b": None, "c": 3}} | ||
|
||
result = stringify_unsupported(test_cases) | ||
|
||
assert result["list_with_none"] == "[1, None, 3]" | ||
assert result["dict_with_none/a"] == 1 | ||
assert "dict_with_none/b" not in result | ||
assert result["dict_with_none/c"] == 3 | ||
|
||
|
||
def test_stringify_unsupported_empty_input(): | ||
"""Test stringify_unsupported with empty input.""" | ||
result = stringify_unsupported({}) | ||
assert result == {} | ||
|
||
|
||
def test_stringify_unsupported_invalid_input(): | ||
"""Test stringify_unsupported with invalid input.""" | ||
with pytest.raises(TypeError): | ||
stringify_unsupported(None) # type: ignore | ||
|
||
with pytest.raises(TypeError): | ||
stringify_unsupported("not a dict") # type: ignore |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.