Unexpected Doctest result for an output including a backslash

86 Views Asked by At

This doctest fails with an output shown at the bottom. Why does Pytest change the doctest to strip_special_chars( "c:\abcDEF1 [email protected]") with a single backslash leading to the unintended expected result? How do I change its behavior?

def strip_special_chars(input_text, re_string='[^a-zA-Z0-9_\s\.\-:\\\\]'):
    """
    >>> strip_special_chars( "c:\\abcDEF1 [email protected]")
    'c:\\abcDEF1 23-_.sql'
    """
    regex = re.compile(re_string) 
    return regex.sub('', input_text)
_____________________________________________ [doctest] helpers.strip_special_chars _____________________________________________ 
1484
1485     >>> strip_special_chars( "c:\abcDEF1 [email protected]")
Expected:
    'c:\abcDEF1 23-_.sql'
Got:
    'c:bcDEF1 23-_.sql'
1

There are 1 best solutions below

0
Jason O. On

Adding r to the doctest string fixed it. Because the argument ("c:\abcDEF1 [email protected]") is inside """, it appears that \\ changes to \ (thereby having no escaping) before Pytest sees it.

def strip_special_chars(input_text, re_string='[^a-zA-Z0-9_\s\.\-:\\\\]'):
    r"""
    >>> strip_special_chars( "c:\\abcDEF1 [email protected]")
    'c:\\abcDEF1 23-_.sql'
    """
    regex = re.compile(re_string) 
    return regex.sub('', input_text)