-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Better example for W0640: cell-var-from-loop #8960
Conversation
Add example to use functools.partial
for more information, see https://pre-commit.ci
I felt this is trivial, so left the news fragment untouched. |
There was a problem hiding this 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.
Foo/bar do not help beginners, it's one more concept to understand before diving in the problem
for more information, see https://pre-commit.ci
There was a problem hiding this 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.
Just updated to keep consistency |
Locally I get a cell-var-from-loop from the good example, do you also have that ? ( |
My bad, a quick look does seem that my code is wrong XD |
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? |
I'm going to fix the template to be able to have one bad and two good, it's still very new :) |
d111ab8
to
889a2b3
Compare
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. |
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]>
Sorry, closed by accident |
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 Also, |
I think these examples are very good indeed! You could consider using those? |
@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? |
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. |
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 π |
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", ... |
And using 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. |
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. |
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? |
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. |
Ok, I've updated the pr. |
for more information, see https://pre-commit.ci
There was a problem hiding this 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/good/functools.partial.py
Outdated
Show resolved
Hide resolved
Co-authored-by: Pierre Sassoulas <[email protected]>
β¦e good files" This reverts commit 353d90e.
There was a problem hiding this 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 π )
Congratulation on becoming a contributor, next time you won't have to wait for approval to launch a CI job :) |
No, thank you all for the patience! |
Type of Changes
Description
Add another example for W0640: cell-var-from-loop.
Example to use
functools.partial