-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 negative expression-not-assigned
on fonction calls that return values
#7935
Comments
It seem like it could be considered like a false negative of |
expression-not-assigned
on fonction calls that return values
This is an option. The thing is, I'm really sure this should be enabled on a per-function basis, otherwise it's going to be 99% noise. But annotations for a few key functions in popular libraries (like the @_generative decorator in the sqlalchemy) will make a huge difference. And from that perspective it makes sense to have a separate warning ID, just not to mess with defaults for |
Sounds like an option to list the calls that should definitely be assigned to something would be nice ( |
This feature would also be very valuable when using libraries with return codes/bools where you need to remember to check them all the time 😓 |
Very much in favour of this! No discard is quite a common warning in other languages: https://www.avanderlee.com/swift/discardableresult/ |
Any implementation hints on how to do this? Glancing through the code I wonder if it would require inference? |
Yes, infering the return of the function would work and false negatives might be fixed by making inference better in astroid directly. But there's an associated cost that can be very high, see this comment around the current implementation: pylint/pylint/checkers/base/basic_checker.py Lines 465 to 467 in 15a5ac0
The solution is probably going to be balancing better inference in astroid (in astroid), properly using/reusing the result we do infer (in pylint) and balancing acceptable performance and acceptable false negatives (in pylint and astroid). |
This doesn't look like the sort of thing that's going to be an easy first contribution?
|
Not really no 😄 That would be "good first issues" (https://github.com/pylint-dev/pylint/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22Good%20first%20issue%22) |
How might I contribute to help this get added?
…On Sun, 3 Nov 2024, 16:16 Pierre Sassoulas, ***@***.***> wrote:
Not really no 😄 That would be "good first issues" (
https://github.com/pylint-dev/pylint/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22Good%20first%20issue%22
)
—
Reply to this email directly, view it on GitHub
<#7935 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAJWOW2FRJZFPPOBCDYJXK3Z6ZD7NAVCNFSM6AAAAABRAPYUA6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINJTGQ4DENRZHA>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
Probably contributing to this issue yourself even if this is hard for a first contribution, or offering a bounty somewhere so someone else does it (issue hunt, here, others I don't know.. ?). Creating comprehensive failing functional tests should not be too hard and is going to make the life of an implementer easier too. |
Current problem
This is a request to implement a check similar to the
[[nodiscard]]
function attribute in C++. A typical mistake is when people assume that the method modifies the current object while it actually creates and returns a copy of self. Calling this method without using the return value doesn't make sense and could be easily caught.An example with sqlalchemy. Instead of:
You should write:
The problem is the first version looks legit: query.filter reads as "filter the query", so it's hard to spot this mistake. I've seen even experienced sqlalchemy users fall into this trap once in a while.
Desired solution
I'd suggest something like
# pylint: enable=unused-return-value
pragma for functions with a subsequent analysis of the call site.There's already a very similar warning
expression-not-assigned / W0106
, but it specifically ignores the function calls. Which makes sense because in many cases functions have side effects besides just the return value. In some cases though they're limited to the return value and it would be nice to catch these.Additional context
https://stackoverflow.com/questions/51858215/how-to-make-pylint-report-unused-return-values
https://stackoverflow.com/questions/74785720/python-prevent-discarding-of-the-function-return-value
The text was updated successfully, but these errors were encountered: