Skip to content
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

os_sorted fails to sort current directory #142

Closed
xymy opened this issue Dec 14, 2021 · 3 comments · Fixed by #143
Closed

os_sorted fails to sort current directory #142

xymy opened this issue Dec 14, 2021 · 3 comments · Fixed by #143
Labels

Comments

@xymy
Copy link

xymy commented Dec 14, 2021

Describe the bug
ValueError is raised when sorting current directory '.' or './' or '.\\'.

Expected behavior
The current directory should be sorted correctly without error.

Environment (please complete the following information):

  • Python Version: 3.9
  • OS: both Windows and Ubuntu
  • If the bug involves LOCALE or humansorted:
    • Is PyICU installed? No
    • Do you have a locale set? If so, to what? No

To Reproduce

Python 3.9.7 (tags/v3.9.7:1016ef3, Aug 30 2021, 20:19:38) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from natsort import os_sorted
>>> os_sorted(['.'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Software\Python39\lib\site-packages\natsort\natsort.py", line 929, in os_sorted
    return sorted(seq, key=os_sort_keygen(key), reverse=reverse)
  File "D:\Software\Python39\lib\site-packages\natsort\natsort.py", line 786, in <lambda>
    OSSortKeyType, lambda x: tuple(map(_winsort_key, _split_apply(x, key)))
  File "D:\Software\Python39\lib\site-packages\natsort\natsort.py", line 770, in _split_apply
    return utils.path_splitter(str(v))
  File "D:\Software\Python39\lib\site-packages\natsort\utils.py", line 890, in path_splitter
    *path_parts, base = s.parts
ValueError: not enough values to unpack (expected at least 1, got 0)
@SethMMorton
Copy link
Owner

I can reproduce.

I am blown away that this has not been caught before - this functionality actually predates os_sorted by many years.

@SethMMorton
Copy link
Owner

This is originating from behavior from pathlib that I would call surprising

In [6]: import pathlib

In [7]: pathlib.Path("a").parts
Out[7]: ('a',)

In [8]: pathlib.Path("a.").parts
Out[8]: ('a.',)

In [9]: pathlib.Path(".a").parts
Out[9]: ('.a',)

In [10]: pathlib.Path(".").parts
Out[10]: ()

In [11]: pathlib.Path("./").parts
Out[11]: ()

In [12]: pathlib.Path("././").parts
Out[12]: ()

In [13]: pathlib.Path("..").parts
Out[13]: ('..',)

I would not have expected pathlib.Path(".").parts to return an empty tuple, and this is the root of the problem.

@SethMMorton
Copy link
Owner

Released as 8.0.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants