-
-
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
invalid-repr-returned
false positive with match statement
#7645
Comments
If nothing is matched this function would return None. This is hard to understand for pylint but also for human. Even for human you can make a mistake and not realize something can end up unmatched and get an error. I would suggest using a default. |
In this particular case, every possibility is covered and all branches return the correct type. Under no circumstances will this function return And generally, I understand that humans make mistakes, which is why I also employ static analysis tools like Pyright, which do have exhaustiveness checking for match statements. I don't believe it's correct to require a default value for an unreachable branch, at best that's just adding dead code for no reason. It may even be detrimental in that it may cause one to forget to handle new cases, an error that would have otherwise been caught by static analysis tooling, but which would be wrongfully silenced/ignored by adding a default That said, it's my understanding that exhaustiveness checking is a difficult problem. If this particular case were marked as wontfix, that would be understandable. On the other hand, maybe this lint shouldn't be the responsibility of pylint, but that of a typechecker, since this is fundamentally about the type system and not code style. |
The reason would be that you know within 10ms when reading the code that it can't return That being said, this is a false positive that should be fixed, but this rely on control flow (#4795 ?). Control flow understanding is one of the weak point of pylint. |
I "know within 10ms when reading the code that it can't return
I'd like to take a stronger stance on this and say the proper solution to this issue is to simply remove this lint and let a typechecker handle it instead. |
We have a lot of legacy checkers that half-ass checks that should be done by a type-checker in a very particular scenario. On the top of my head this is one of the worst offender : https://pylint.pycqa.org/en/latest/user_guide/messages/error/invalid-envvar-value.html. This is a lot of cleanup and/or documentation to do if it come to it. |
Another example of this issue is with enum:
The point is that I do not want to have a default |
Verified OP and #7645 (comment) are fixed with pylint-dev/astroid#2042, so closing as a duplicate of #5288, which we'll close with a regression test when updating pylint to use the next astroid release. |
Bug description
It looks like pylint isn't checking exhaustiveness before triggering this lint.
pylint_e0306_false_positive.py
:Likely related: #7470 (comment)
Configuration
No response
Command used
Pylint output
Expected behavior
No error
Pylint version
OS / Environment
NixOS 22.05
Additional dependencies
No response
The text was updated successfully, but these errors were encountered: