Skip to content

Faulty for-append-to-extend refactoring #279

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

Closed
2 tasks done
yairchu opened this issue Sep 14, 2022 · 3 comments
Closed
2 tasks done

Faulty for-append-to-extend refactoring #279

yairchu opened this issue Sep 14, 2022 · 3 comments
Labels
bug Something isn't working next release This will be fixed in next release

Comments

@yairchu
Copy link

yairchu commented Sep 14, 2022

Checklist

  • I have searched the Sourcery documentation for the issue, and found nothing
  • I have checked there are no open bugs referencing the same bug or problem

Description

The following snippet:

    for x in gen:
        (less if x < pivot else more).append(x)

Gets a buggy suggestion to change into:

    (less if x < pivot else more).extend(iter(gen))

The problem lies with the LHS being appended not being constant and relying on the iterated x.

Debug Information

IDE Version:
VS Code 1.71.1

Sourcery Version:
Sourcery 0.12.7

Operating system and Version:
maxOS 12.5

Appendix

Example code snippet in context:

def quick_sort(gen):
    """
    Haskell-style leet lazy evaluation quick_sort.
    It doesn't need to fully sort everything to find the N smallest elements.

    An example use case is iterating over potential dates by the order of their attractiveness,
    until one of them agrees to date you (no need to iterate over the rest after a match is found),
    but be warned that the odds of that may be lower if you're using functions like this one.

    The complexity would be O(N*log(K)) where K is the ammount iterated,
    in comparison to O(N*log(N)) for the full sorting algorithm.
    """
    gen = iter(gen)
    try:
        pivot = next(gen)
    except StopIteration:
        return
    less = []
    more = []
    for x in gen:
        (less if x < pivot else more).append(x)
    yield from quick_sort(less)
    yield pivot
    yield from quick_sort(more)
@yairchu yairchu added the bug Something isn't working label Sep 14, 2022
@Hellebore
Copy link
Collaborator

Thanks for raising! Will look into a fix.

@Hellebore Hellebore added the next release This will be fixed in next release label Sep 22, 2022
@Hellebore
Copy link
Collaborator

This fix is now merged and will be in the next release.

@Hellebore
Copy link
Collaborator

Fixed with 0.12.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working next release This will be fixed in next release
Projects
None yet
Development

No branches or pull requests

2 participants