Hotfix: Allow other types in remove_null_unicode_character (#1988)

* fix: allow any types in `remove_null_unicode_character`

* chore: ver

* chore: changelog

* feat: tests
This commit is contained in:
Matt Kaye
2025-07-14 13:24:34 -04:00
committed by GitHub
parent 7c4685d48d
commit 271dc2b243
4 changed files with 62 additions and 11 deletions

View File

@@ -5,6 +5,12 @@ All notable changes to Hatchet's Python SDK will be documented in this changelog
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [1.15.3] - 2025-07-14
### Changed
- `remove_null_unicode_character` now accepts any type of data, not just strings, dictionaries, lists, and tuples. If the data is not one of these types, it's returned as-is.
## [1.15.2] - 2025-07-12
### Changed

View File

@@ -2,10 +2,7 @@ from typing import Any, TypeVar, cast, overload
T = TypeVar("T")
K = TypeVar("K")
@overload
def remove_null_unicode_character(data: str, replacement: str = "") -> str: ...
R = TypeVar("R")
@overload
@@ -24,9 +21,13 @@ def remove_null_unicode_character(
) -> tuple[T, ...]: ...
@overload
def remove_null_unicode_character(data: R, replacement: str = "") -> R: ...
def remove_null_unicode_character(
data: str | dict[K, T] | list[T] | tuple[T, ...], replacement: str = ""
) -> str | dict[K, T] | list[T] | tuple[T, ...]:
data: dict[K, T] | list[T] | tuple[T, ...] | R, replacement: str = ""
) -> str | dict[K, T] | list[T] | tuple[T, ...] | R:
"""
Recursively traverse a dictionary (a task's output) and remove the unicode escape sequence \\u0000 which will cause unexpected behavior in Hatchet.
@@ -36,7 +37,6 @@ def remove_null_unicode_character(
:param replacement: The string to replace \\u0000 with.
:return: The same dictionary with all \\u0000 characters removed from strings, and nested dictionaries/lists processed recursively.
:raises TypeError: If the input is not a string, dictionary, list, or tuple.
"""
if isinstance(data, str):
return data.replace("\u0000", replacement)
@@ -57,6 +57,4 @@ def remove_null_unicode_character(
remove_null_unicode_character(cast(Any, item), replacement) for item in data
)
raise TypeError(
f"Unsupported type {type(data)}. Expected str, dict, list, or tuple."
)
return data

View File

@@ -1,6 +1,6 @@
[tool.poetry]
name = "hatchet-sdk"
version = "1.15.2"
version = "1.15.3"
description = ""
authors = ["Alexander Belanger <alexander@hatchet.run>"]
readme = "README.md"

View File

@@ -0,0 +1,47 @@
from hatchet_sdk import remove_null_unicode_character
def test_remove_null_unicode() -> None:
assert remove_null_unicode_character(
{"message": "Hello\x00World", "user": "test\0user"},
replacement=" ",
) == {
"message": "Hello World",
"user": "test user",
}
assert remove_null_unicode_character(
["Hello\x00World", "test\0user"], replacement=" "
) == [
"Hello World",
"test user",
]
assert remove_null_unicode_character(
("Hello\x00World", "test\0user"), replacement=" "
) == (
"Hello World",
"test user",
)
assert (
remove_null_unicode_character("Hello\x00World", replacement=" ")
== "Hello World"
)
assert remove_null_unicode_character(
{"key": "value", "nested": {"inner": "text\0with\u0000"}},
replacement=" ",
) == {
"key": "value",
"nested": {"inner": "text with "},
}
assert remove_null_unicode_character(1) == 1
assert remove_null_unicode_character(None) is None
assert remove_null_unicode_character(True) is True
assert remove_null_unicode_character(3.14) == 3.14
assert remove_null_unicode_character(
{"int": 1, "float": 2.5, "string": "test\0user"},
replacement=" ",
) == {"int": 1, "float": 2.5, "string": "test user"}