Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
davidx33 committed Nov 22, 2024
1 parent efae89e commit 20de1f7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
16 changes: 10 additions & 6 deletions python/langsmith/anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,16 @@ def __init__(self, rules: List[StringNodeRule]):
"""Initialize the processor with a list of rules."""
self.rules = [
{
"pattern": rule["pattern"]
if isinstance(rule["pattern"], re.Pattern)
else re.compile(rule["pattern"]),
"replace": rule["replace"]
if isinstance(rule.get("replace"), str)
else "[redacted]",
"pattern": (
rule["pattern"]
if isinstance(rule["pattern"], re.Pattern)
else re.compile(rule["pattern"])
),
"replace": (
rule["replace"]
if isinstance(rule.get("replace"), str)
else "[redacted]"
),
}
for rule in rules
]
Expand Down
10 changes: 6 additions & 4 deletions python/tests/unit_tests/test_anonymizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,17 @@ def my_func(body: str, from_: MyInput) -> MyOutput:
assert patched_data["inputs"] == expected_inputs
assert patched_data["outputs"] == expected_outputs


def test_rule_node_processor_scrub_sensitive_info():
rules = [
StringNodeRule(pattern=re.compile(r"\b\d{3}-\d{2}-\d{4}\b"), replace="[ssn]"),
StringNodeRule(pattern=re.compile(r"\b\d{3}-\d{2}-\d{4}\b"), replace="[ssn]"),
StringNodeRule(
pattern=re.compile(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"),
replace="[email]",
),
),
StringNodeRule(
pattern=re.compile(r"\b\d{3}[-.\s]?\d{3}[-.\s]?\d{4}\b"), replace="[phone]"
),
),
]
processor = RuleNodeProcessor(rules)

Expand All @@ -169,6 +170,7 @@ def test_rule_node_processor_scrub_sensitive_info():

assert result == expected


def test_rule_node_processor_default_replace():
rules = [
StringNodeRule(pattern=re.compile(r"sensitive")),
Expand All @@ -184,4 +186,4 @@ def test_rule_node_processor_default_replace():
]

result = processor.mask_nodes(nodes)
assert result == expected
assert result == expected

0 comments on commit 20de1f7

Please sign in to comment.