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

Better example for W0640: cell-var-from-loop #8960

Merged
merged 22 commits into from
Sep 4, 2023

Conversation

SubaruArai
Copy link
Contributor

Type of Changes

Type
βœ“ πŸ“œ Docs

Description

Add another example for W0640: cell-var-from-loop.
Example to use functools.partial

SubaruArai and others added 2 commits August 16, 2023 14:29
@codecov
Copy link

codecov bot commented Aug 16, 2023

Codecov Report

Merging #8960 (f960a1f) into main (7b72c39) will not change coverage.
Report is 2 commits behind head on main.
The diff coverage is n/a.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #8960   +/-   ##
=======================================
  Coverage   95.76%   95.76%           
=======================================
  Files         173      173           
  Lines       18639    18639           
=======================================
  Hits        17850    17850           
  Misses        789      789           

@SubaruArai
Copy link
Contributor Author

I felt this is trivial, so left the news fragment untouched.

@SubaruArai SubaruArai marked this pull request as ready for review August 16, 2023 05:54
@Pierre-Sassoulas Pierre-Sassoulas added Documentation πŸ“— Skip news πŸ”‡ This change does not require a changelog entry labels Aug 17, 2023
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution :) Could you create a good/ directory with two aptly named files inside instead please ? The final result will be more readable. You can look at bad-chained-comparison for an example of what to do.

SubaruArai and others added 5 commits August 25, 2023 14:54
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great first contribution :) ! I'm going to remove the foo/bar in the example directly in this MR, sorry for the noise.

@SubaruArai
Copy link
Contributor Author

Just updated to keep consistency

@Pierre-Sassoulas
Copy link
Member

Locally I get a cell-var-from-loop from the good example, do you also have that ? (cd doc/; pytest -k cell-var)

@SubaruArai
Copy link
Contributor Author

My bad, a quick look does seem that my code is wrong XD

@SubaruArai SubaruArai marked this pull request as draft August 28, 2023 00:06
@SubaruArai
Copy link
Contributor Author

Fixed the example, but it seems the "bad" directory needs 2 examples.

@Pierre-Sassoulas Any idea to circumvent? Maybe I need to come up with another bad case?

@Pierre-Sassoulas
Copy link
Member

I'm going to fix the template to be able to have one bad and two good, it's still very new :)

@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Sep 1, 2023

Hello, the problem do not come from the helper for the templates with directory, the good.py example actually raises cell-var-from-loop, do you mind having a look ? I'm going to fix the error message from the test template but it's not super easy to have an accurate message considering we do not have the information of the file raising a message with the current structure of the code.

@Pierre-Sassoulas Pierre-Sassoulas added the Waiting on author Indicate that maintainers are waiting for a message of the author label Sep 1, 2023
SubaruArai and others added 2 commits September 1, 2023 16:26
Add example to use functools.partial

restructure doc of W0640

Rename new_func.py to new_function.py

Update cell-var-from-loop example to remove foo/bar

Foo/bar do not help beginners, it's one more concept to understand before
diving in the problem example.

Co-authored-by: Pierre Sassoulas <[email protected]>
@SubaruArai SubaruArai reopened this Sep 4, 2023
@SubaruArai
Copy link
Contributor Author

Sorry, closed by accident

@SubaruArai
Copy link
Contributor Author

SubaruArai commented Sep 4, 2023

If the intend of the example was:

def greet_teachers(names):
    for name in names:
        def greet():
            # do something
            print(f"Hello, {name}!")
        greet()

Shouldn't the good example be like:

def greet_teachers(names):
    def greet(name):
        # do something
        print(f"Hello, {name}!")

    for name in names:
        greet(name)

Why make the greet function global?

Also, functools.partial will be inappropriate for this purpose.

@DanielNoord
Copy link
Collaborator

If the intend of the example was:

def greet_teachers(names):
    for name in names:
        def greet():
            # do something
            print(f"Hello, {name}!")
        greet()

Shouldn't the good example be like:

def greet_teachers(names):
    def greet(name):
        # do something
        print(f"Hello, {name}!")

    for name in names:
        greet(name)

Why make the greet function global?

Also, functools.partial will be inappropriate for this purpose.

I think these examples are very good indeed! You could consider using those?

@SubaruArai
Copy link
Contributor Author

@DanielNoord While I definitely can, I don't like the fact that the "bad" example works just as fine - albeit less efficient.

What would be a good "bad" example?

@DanielNoord
Copy link
Collaborator

I think the bad example is also fine? It's the same as what we currently have in the documentation and it is the code that raises the warning. It's just bad practice to write Python code like that.

@Pierre-Sassoulas
Copy link
Member

I noticed that the bad example produce a warning but does not actually produce a problem in what is printed (python bad.py produce the expected list of names). Ideally we would use something that is not a false positive in the doc πŸ˜„

@SubaruArai
Copy link
Contributor Author

It will be quite a lot of change, but what about this:

Bad:

def greet_teachers(names):
    greetings = []
    for name in names:

        def greet():
            # do stuff
            print(f"hello, {name}")

        if name.isalpha():
            greetings.append(greet)

    for greet in greetings:
        greet()


greet_teachers(["Alice", "Bob", "Charlie", "Not-A-Name"])
# "hello, Not-A-Name" * 3

Good:

def greet_teachers(names):
    names_to_greet = []

    def greet(name):
        # do something
        print(f"hello, {name}")

    for name in names:
        if name.isalpha():
            names_to_greet.append(name)

    for name in names_to_greet:
        greet(name)


greet_teachers(["Alice", "Bob", "Charlie", "Not-A-Name"])
# "hello, Alice", ...

@SubaruArai
Copy link
Contributor Author

SubaruArai commented Sep 4, 2023

And using functools.partial:

import functools


def greet_teachers(names):
    greetings = []
    for name in names:
        if name.isalpha():
            greetings.append(functools.partial(print, f"hello, {name}"))

    for greet in greetings:
        greet()


greet_teachers(["Alice", "Bob", "Charlie", "Not-A-Name"])
# "hello, Alice", ...

But then again, this isn't doing lazy evaluation of string formatting.

@Pierre-Sassoulas
Copy link
Member

Sound good ! I was hoping something simpler was possible, but I did not have the time to find a simple example and a working example is paramount.

@SubaruArai
Copy link
Contributor Author

SubaruArai commented Sep 4, 2023

While this does lazy formatting, pylint will complain to use f-strings:

import functools


def greet_teachers(names):
    greeting_msgs = []
    for name in names:
        if name.isalpha():
            greeting_msgs.append(functools.partial("hello, {}".format, name))

    for greeting_msg in greeting_msgs:
        print(greeting_msg())


greet_teachers(["Alice", "Bob", "Charlie", "Not-A-Name"])
# "hello, Alice", ...

Maybe a candidate for false positive?

@SubaruArai
Copy link
Contributor Author

I was hoping something simpler was possible

Yeah, I feel that too. The thing is, having this warning mostly means you're abusing lazy evaluation - which is kind of complicated for newcomers anyways.

@Pierre-Sassoulas Pierre-Sassoulas removed the Waiting on author Indicate that maintainers are waiting for a message of the author label Sep 4, 2023
@SubaruArai
Copy link
Contributor Author

Ok, I've updated the pr.

@SubaruArai SubaruArai changed the title Add another example for W0640: cell-var-from-loop Better example for W0640: cell-var-from-loop Sep 4, 2023
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, it's blocked by #8991 though, let's rebase on it when it's merged.

doc/data/messages/c/cell-var-from-loop/bad.py Outdated Show resolved Hide resolved
@Pierre-Sassoulas Pierre-Sassoulas added the Blocked 🚧 Blocked by a particular issue label Sep 4, 2023
@Pierre-Sassoulas Pierre-Sassoulas marked this pull request as ready for review September 4, 2023 13:54
@Pierre-Sassoulas Pierre-Sassoulas removed the Blocked 🚧 Blocked by a particular issue label Sep 4, 2023
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking the time to make the doc about this message great ! (Also permitted to make the template better in the process πŸ‘Œ )

@Pierre-Sassoulas Pierre-Sassoulas merged commit f28ffe3 into pylint-dev:main Sep 4, 2023
@Pierre-Sassoulas
Copy link
Member

Congratulation on becoming a contributor, next time you won't have to wait for approval to launch a CI job :)

@SubaruArai
Copy link
Contributor Author

No, thank you all for the patience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation πŸ“— Skip news πŸ”‡ This change does not require a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants