wemake-services / wemake-python-styleguide

The strictest and most opinionated python linter ever!
https://wemake-python-styleguide.rtfd.io
MIT License
2.51k stars 378 forks source link

Forbid assigning `lambda` to object properties #1733

Open sobolevn opened 3 years ago

sobolevn commented 3 years ago

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.

abhi-krish commented 3 years ago

Hi! I'd like to work on this.

sobolevn commented 3 years ago

Awesome! Thanks a lot!

abhi-krish commented 3 years ago

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?

sobolevn commented 3 years ago

Sure!

Start with reading this file: https://github.com/wemake-services/wemake-python-styleguide/blob/master/CONTRIBUTING.md Then look at: https://github.com/wemake-services/wemake-python-styleguide/blob/master/wemake_python_styleguide/visitors/ast/classes.py

sobolevn commented 3 years ago

@willtkwon Please feel free! And thank you 🙂

AlexandrKhabarov commented 3 years ago

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 commented 3 years ago

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