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

False positive expression-not-assigned when calling functions with no return inside ternary expression #8129

Open
da-dada opened this issue Jan 29, 2023 · 18 comments
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation

Comments

@da-dada
Copy link

da-dada commented Jan 29, 2023

Bug description

def f1():
    pass


def f2():
    pass


test = True
f1() if test else f2()

Configuration

No response

Command used

pylint test.py

Pylint output

test.py:9: [W0106(expression-not-assigned), ] Expression "f1() if test else f2()" is assigned to nothing

Expected behavior

no warning (according to documentation of pylint)

Pylint version

2.16.0.b1

OS / Environment

windows

@da-dada da-dada added the Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling label Jan 29, 2023
@mbyrnepr2

This comment was marked as resolved.

@mbyrnepr2 mbyrnepr2 added Invalid Not a bug, already exists or already fixed and removed Needs triage 📥 Just created, needs acknowledgment, triage, and proper labelling labels Jan 30, 2023
@mbyrnepr2 mbyrnepr2 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 30, 2023
@mbyrnepr2 mbyrnepr2 removed the Invalid Not a bug, already exists or already fixed label Jan 30, 2023
@mbyrnepr2 mbyrnepr2 reopened this Jan 30, 2023
@mbyrnepr2

This comment was marked as resolved.

@mbyrnepr2 mbyrnepr2 added False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation labels Jan 30, 2023
@nickdrozd

This comment was marked as resolved.

@mbyrnepr2
Copy link
Member

If it is re-written as the following, the message goes away; so I think it makes sense for consistency to consider the one-line version a false positive:

if test:
    f1()
else:
    f2()

@nickdrozd

This comment was marked as off-topic.

@da-dada

This comment was marked as off-topic.

@Pierre-Sassoulas

This comment was marked as off-topic.

@Pierre-Sassoulas Pierre-Sassoulas changed the title expression-not-assigned / W0106 False positive expression-not-assigned when calling functions with no return inside ternary expression Jan 30, 2023
@da-dada

This comment was marked as off-topic.

@Pierre-Sassoulas

This comment was marked as off-topic.

@nickdrozd

This comment was marked as off-topic.

@da-dada

This comment was marked as off-topic.

@mbyrnepr2
Copy link
Member

Looking into the origins of the conditional expression and it looks like y if x else z was introduced as a replacement to this syntax:
x and y or z.

I don't see anything specifically stating that it must be assigned to a name; although the real-world code examples in the link mostly are assigning to a name.

PEP308
Motivating use case (from PEP308)
real-world code fragments (from PEP308)

@nickdrozd
Copy link
Collaborator

There are at least four ways to deal with this warning.

  1. Convert the conditional expression to an if / else block:
# if / else blocks
if test:
    f1()
else:
    f2()
  1. Explicitly discard the value of the conditional expression:
_ = f1() if test else f2()
  1. Use a conditional expression to determine the function, then call the result:
(f1 if test else f2)()
  1. Just disable the warning:
# pylint: disable = expression-not-assigned
f1() if test else f2()

If a one-liner is desired, I suggest going with option 3. But in any case I don't think any changes ought to be made to Pylint to accommodate the original. One coder's false positive is another's false negative.

@Pierre-Sassoulas
Copy link
Member

1) is the code almost everyone use There's only one example of actual function in one line (print 'succeed' if pred(x) else 'failed', ':', x) in the link provided by @mbyrnepr2. and it's the recommended snippet since python 3.0 (I recommend that people leave existing and/or code alone but start writing if/else expressions in new code only.). But nonetheless it's still valid python code and if the function always return None then 2) and 3) is just dancing around to appease the linter. Discouraging it should be another message like function-call-without-assignement-in-ternary and seeing how it's very seldom seen, probably not worth it (imo).

@da-dada

This comment was marked as off-topic.

@da-dada

This comment was marked as off-topic.

@nickdrozd

This comment was marked as off-topic.

@da-dada

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False Positive 🦟 A message is emitted but nothing is wrong with the code Needs PR This issue is accepted, sufficiently specified and now needs an implementation
Projects
None yet
Development

No branches or pull requests

4 participants