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

Forbid assigning lambda to object properties #1733

Open
sobolevn opened this issue Nov 19, 2020 · 7 comments · May be fixed by #1911
Open

Forbid assigning lambda to object properties #1733

sobolevn opened this issue Nov 19, 2020 · 7 comments · May be fixed by #1911
Labels
help wanted Extra attention is needed level:starter Good for newcomers pr-available rule request Adding a new rule

Comments

@sobolevn
Copy link
Member

Rule request

Thesis

We should forbid code like:

class Example(object):
    def __init__(self):
        self.callback = lambda arg: arg + 1

Reasoning

This will be very tricky to understand for users: example.callback(1) will look like a method, but it is not a method.

@sobolevn sobolevn added help wanted Extra attention is needed level:starter Good for newcomers rule request Adding a new rule labels Nov 19, 2020
@sobolevn sobolevn added this to the Version 0.15 aka Python3.9 milestone Nov 19, 2020
@abhi-krish
Copy link

Hi! I'd like to work on this.

@sobolevn
Copy link
Member Author

Awesome! Thanks a lot!

@abhi-krish
Copy link

Hey @sobolevn! This is my first time contributing and am not sure where I should start with this problem. Would you be able to point me in the right direction?

@abhi-krish abhi-krish removed their assignment Nov 30, 2020
@sobolevn
Copy link
Member Author

@willtkwon Please feel free! And thank you 🙂

@AlexandrKhabarov
Copy link
Contributor

Is it correct that this check should find situations like this?

create_object.attr = lambda: print('Lambda!')

I mean that it should not distinguish lambda assignemnts inside init or anywehere.

@sobolevn
Copy link
Member Author

We only treat self, cls, and mcs as self-like objects. So, we only check them. See https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/constants.py#L150

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed level:starter Good for newcomers pr-available rule request Adding a new rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants