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

set-comprehension false positive on attribute #271

Closed
2 tasks done
ronnodas opened this issue Aug 29, 2022 · 4 comments
Closed
2 tasks done

set-comprehension false positive on attribute #271

ronnodas opened this issue Aug 29, 2022 · 4 comments
Labels
bug Something isn't working next release This will be fixed in next release

Comments

@ronnodas
Copy link

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

When defining a set attribute by using add in a loop, set-comprehension triggers even when there are conditionals involving the set being added to. The equivalent refactoring does not appear for defining variables. Here is some code sample:

class Symmetrizer:
    def __init__(self, pairs):
        self.unordered_pairs = set()
        for x, y in pairs:
            if (y, x) not in self.unordered_pairs:
                self.unordered_pairs.add((x,y))

Symmetrizer({(1, 2), (3, 4), (2, 1)})

The suggested refactoring is to replace the __init__ body by:

self.unordered_pairs = {(x, y) for x, y in pairs if (y, x) not in self.unordered_pairs}

which raises an AttributeError.

Debug Information

IDE Version:
PyCharm Professional Edition 2022.2.1

Sourcery Version:
Sourcery 0.12.7

Operating system and Version:
Linux, Fedora 36

@ronnodas ronnodas added the bug Something isn't working label Aug 29, 2022
@ruancomelli
Copy link
Contributor

Hello, @ronnodas! Thank you for reporting this bug. I was able to reproduce it and will work on a fix. We will let you know once it is fixed.

@ruancomelli ruancomelli added the next release This will be fixed in next release label Aug 31, 2022
@ruancomelli
Copy link
Contributor

Hi, @ronnodas! A fix for this bug has just been merged. It will be available in the next Sourcery release. Thanks again for reporting this issue!

@Hellebore
Copy link
Collaborator

Resolved in 0.12.8

@Hellebore
Copy link
Collaborator

Resolved in 0.12.8

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

3 participants