Clarification
Doctests are alluring because of their simplicity, but it's a misleading simplicity. You expect the test line to represent an expression that doctest will evaluate against the result of the last expression, but it's not; it's actually just doing a simple, basic string comparison.
#doctesttest.py
"""
>>> "test"
"test"
python -m doctest doctesttest.py
Gives
...
Expected:
"test"
Got:
'test'
Although - in pythonic terms - "test" == 'test'
, even "test" is 'test'
, str(""" 'test' """)
does not match str(""" "test" """)
.
Armed with this awareness...
Solution
The following will fail on all systems:
def unique_paths(path_list):
""" Returns a list of normalized-unique paths based on path_list
>>> unique_paths(["first/path", ".\\first/path", "second/path"])
['first/path', 'second/path']
"""
return set(os.path.normpath(p) for p in path_list)
- We got a set, not a list,
- Converting the set into a list needs to provide a consistent order,
- doctest uses eval so the "\" in ".\first" will be converted into "\".
We're looking for a simple string match, so we need to look for an easily matchable result string. You don't care about the separator, so either eliminate it or replace it, or test around it:
def unique_paths(path_list):
""" Returns a list of normalized-unique paths based on path_list
>>> paths = unique_paths(["first/path", ".\\\\first/path", "second/path"])
>>> len(paths)
2
>>> [os.path.split(path) for path in sorted(list(paths))]
[('first', 'path'), ('second', 'path')]
# or heck, even
>>> sorted(list(paths[0])).replace('\\\\', '/')
'first/path'
"""
return set(os.path.normpath(p) for p in path_list)