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

KeyError while sorting duplicate imports #194

Closed
zsol opened this issue Aug 5, 2022 · 1 comment · Fixed by #195
Closed

KeyError while sorting duplicate imports #194

zsol opened this issue Aug 5, 2022 · 1 comment · Fixed by #195
Labels
bug Something isn't working

Comments

@zsol
Copy link
Contributor

zsol commented Aug 5, 2022

On this file:

from a import x
from b import y
from b import y
from c import x

usort fails with

Traceback (most recent call last):
  File "usort/cli.py", line 80, in list_imports
    blocks = sorter.sortable_blocks(mod.body)
  File "usort/sorting.py", line 192, in sortable_blocks
    current = self.split_inplace(current, overlap)
  File "usort/sorting.py", line 169, in split_inplace
    new.imported_names[key] = block.imported_names.pop(key)
KeyError: 'y'
@zsol
Copy link
Contributor Author

zsol commented Aug 5, 2022

The problem is that this code assumes that the newly split block doesn't have shadowing. This assumption isn't satisified by the logic here.

We either need to fix name_overlap() to provide this assumption or split_inplace() to not need it.

@zsol zsol added the bug Something isn't working label Aug 5, 2022
thatch added a commit to thatch/usort that referenced this issue Aug 5, 2022
Because we do block splitting before deduplication, it could previously try to pop the same name twice.

Fixes facebook#194
thatch added a commit to thatch/usort that referenced this issue Aug 5, 2022
Because we do block splitting before deduplication, it could previously try to pop the same name twice.

Fixes facebook#194
amyreese pushed a commit to thatch/usort that referenced this issue Sep 2, 2022
Because we do block splitting before deduplication, it could previously try to pop the same name twice.

Fixes facebook#194
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant