Skip to content

Conversation

Pierre-Sassoulas
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas commented Feb 4, 2023

Type of Changes

Type
📜 Docs

Description

Part of #5953 Actual conversation with ChatGPT that led to this MR:

Pierre Sassoulas: Could you give me an example of bad code raising a 'missing-parentheses-for-call-in-test' message in pylint

Sure, here's an example of code that would raise the "missing-parentheses-for-call-in-test" message in Pylint:

def example_func():
    return "example return value"

if example_func:
    print("example_func is defined")

In this code, the line if example_func: raises the "missing-parentheses-for-call-in-test" message, because Pylint is expecting to see a function call in the test expression of an if statement, but there are no parentheses after example_func. To correct this, you can add parentheses to make it a function call:

if example_func():
    print("example_func is defined")

Pierre Sassoulas
Could you make it more lively, the name is very generic ?

Sure, here's a more lively example that also includes a more descriptive function name:

def say_hello():
    return "Hello, world!"

if say_hello:
    print("say_hello is defined")

In this code, the line if say_hello: raises the "missing-parentheses-for-test-call-in-if" message, because Pylint is expecting to see a function call in the test expression of an if statement, but there are no parentheses after say_hello. To correct this, you can add parentheses to make it a function call:

if say_hello():
    print("say_hello is defined")

Pierre Sassoulas
That's kinda expected and boring, could you be more imaginative ? Also I don't think it should return a string, it's supposed to return a boolean.

Sure, here's a more imaginative example with a function that returns a boolean:

def is_it_a_good_day():
    import random
    return random.choice([True, False])

if is_it_a_good_day:
    print("Today is a good day!")

In this code, the line if is_it_a_good_day: raises the "missing-parentheses-for-call-in-test" message, because Pylint is expecting to see a function call in the test expression of an if statement, but there are no parentheses after is_it_a_good_day. To correct this, you can add parentheses to make it a function call:

if is_it_a_good_day():
    print("Today is a good day!")

Pierre Sassoulas
You need to add a # [missing-parentheses-for-call-in-test] on the line the actual message would be raised, could you update the bad example, please ?

Of course! Here's the updated bad example with a comment indicating the location of the "missing-parentheses-for-call-in-test" message:

def is_it_a_good_day():
    import random
    return random.choice([True, False])

if is_it_a_good_day:  # [missing-parentheses-for-call-in-test]
    print("Today is a good day!")

In this code, the line if is_it_a_good_day: would raise the "missing-parentheses-for-call-in-test" message, because Pylint is expecting to see a function call in the test expression of an if statement, but there are no parentheses after is_it_a_good_day. To correct this, you can add parentheses to make it a function call, as shown in the corrected code in my previous answer.

@Pierre-Sassoulas Pierre-Sassoulas added Documentation 📗 Skip news 🔇 This change does not require a changelog entry labels Feb 4, 2023
@codecov
Copy link

codecov bot commented Feb 4, 2023

Codecov Report

Merging #8185 (69e76de) into main (8d13dbe) will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #8185      +/-   ##
==========================================
- Coverage   95.53%   95.53%   -0.01%     
==========================================
  Files         177      177              
  Lines       18622    18616       -6     
==========================================
- Hits        17791    17785       -6     
  Misses        831      831              
Impacted Files Coverage Δ
pylint/checkers/base/basic_checker.py 97.87% <100.00%> (-0.03%) ⬇️

@Pierre-Sassoulas Pierre-Sassoulas force-pushed the imaginative-chat-gpt-example branch from f8747c4 to 3fac051 Compare February 4, 2023 21:47
@Pierre-Sassoulas Pierre-Sassoulas changed the title [missing-parentheses-for-call-in-test] Generate imaginative example without any fruit Doc: Generate some imaginative example without any fruits or animals Feb 4, 2023
@Pierre-Sassoulas Pierre-Sassoulas force-pushed the imaginative-chat-gpt-example branch 2 times, most recently from 2a24c23 to 1495c40 Compare February 4, 2023 21:58
@Pierre-Sassoulas Pierre-Sassoulas force-pushed the imaginative-chat-gpt-example branch from 5507480 to d8652d8 Compare February 5, 2023 16:05
@Pierre-Sassoulas Pierre-Sassoulas marked this pull request as draft February 5, 2023 16:07
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas force-pushed the imaginative-chat-gpt-example branch from cb2ea07 to 0a3c854 Compare February 5, 2023 21:31
@Pierre-Sassoulas Pierre-Sassoulas added the False Negative 🦋 No message is emitted but something is wrong with the code label Feb 5, 2023
@Pierre-Sassoulas Pierre-Sassoulas force-pushed the imaginative-chat-gpt-example branch from 0a3c854 to e9804bd Compare February 5, 2023 21:34
@Pierre-Sassoulas Pierre-Sassoulas changed the title Doc: Generate some imaginative example without any fruits or animals Fix a false positive for 'missing-parentheses-for-call-in-test' and add doc example Feb 5, 2023
@Pierre-Sassoulas Pierre-Sassoulas removed the Skip news 🔇 This change does not require a changelog entry label Feb 5, 2023
@Pierre-Sassoulas Pierre-Sassoulas marked this pull request as ready for review February 5, 2023 21:35
@Pierre-Sassoulas
Copy link
Member Author

This started as a fun chat GPT fueled adventure and then I did not manage to make it raise like it should and went on another more painful adventure.

DanielNoord
DanielNoord previously approved these changes Feb 5, 2023
Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

Two nits! Love this adventure 😄

# Just forcing the generator to infer all elements.
# astroid.exceptions.InferenceError are false positives
# see https://github.com/PyCQA/pylint/pull/8185
list(call_inferred)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a nit, but can't we do the unpacking above where we are already catching InferenceError?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, that not a nit that's an optimization 😄

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.17.0 milestone Feb 5, 2023
@Pierre-Sassoulas Pierre-Sassoulas force-pushed the imaginative-chat-gpt-example branch from 8958d53 to 7c4381f Compare February 5, 2023 21:59
Pierre-Sassoulas and others added 2 commits February 5, 2023 23:01
…th GhatGPT

Prompt:
Could you give me an example of bad code raising a 'missing-parentheses-for-call-in-test'
message in pylint
Could you make it more lively, the name is very generic ?
That's kinda expected and boring, could you be more imaginative ? Also I don't
think it should return a string, it's supposed to return a boolean.
You need to add a ``# [missing-parentheses-for-call-in-test]`` on the line the
actual message would be raised, could you update the bad example, please ?

Final result:
    def is_it_a_good_day():
        import random
        return random.choice([True, False])

    if is_it_a_good_day:  # [missing-parentheses-for-call-in-test]
        print("Today is a good day!")

In this code, the line if is_it_a_good_day: would raise the "missing-parentheses-for-call-in-test"
message, because Pylint is expecting to see a function call in the test expression of an if statement,
but there are no parentheses after is_it_a_good_day.  To correct this, you can add parentheses to make
it a function call, as shown in the corrected code in my previous answer.
@Pierre-Sassoulas Pierre-Sassoulas force-pushed the imaginative-chat-gpt-example branch from 7c4381f to 69e76de Compare February 5, 2023 22:01
@Pierre-Sassoulas Pierre-Sassoulas enabled auto-merge (rebase) February 5, 2023 22:04
@Pierre-Sassoulas Pierre-Sassoulas merged commit cc76aa5 into pylint-dev:main Feb 5, 2023
@Pierre-Sassoulas Pierre-Sassoulas deleted the imaginative-chat-gpt-example branch February 5, 2023 22:27
@github-actions
Copy link
Contributor

github-actions bot commented Feb 5, 2023

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit 69e76de

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation 📗 False Negative 🦋 No message is emitted but something is wrong with the code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants